diff --git a/src/api/operations.ts b/src/api/operations.ts index 28e007e..e0a277f 100644 --- a/src/api/operations.ts +++ b/src/api/operations.ts @@ -6,6 +6,7 @@ import { opCreateLinkURL, opDeleteLinkURL, opFetchGraphURL, + opPruneChainURL, opPruneLinkURL, opUpdateUserDataURL, } from "./paths.ts"; @@ -48,11 +49,7 @@ type backfillChainBodyType = { root: string; // playlistID }; -interface backfillChainDataType extends apiRespBaseType { - toAddNum: number; - addedNum: number; - localNum: number; -} +interface backfillChainDataType extends backfillLinkDataType {} type pruneLinkBodyType = createLinkBodyType; @@ -61,6 +58,10 @@ interface pruneLinkDataType extends apiRespBaseType { deletedNum: number; } +type pruneChainBodyType = backfillChainBodyType; + +interface pruneChainDataType extends pruneLinkDataType {} + export const apiFetchGraph = async (): Promise< AxiosResponse > => { @@ -137,3 +138,14 @@ export const apiPruneLink = async ( return error.response; } }; + +export const apiPruneChain = async ( + data: pruneChainBodyType +): Promise> => { + try { + const response = await axiosInstance.put(opPruneChainURL, data); + return response; + } catch (error: any) { + return error.response; + } +}; diff --git a/src/api/paths.ts b/src/api/paths.ts index 15e77df..02292e4 100644 --- a/src/api/paths.ts +++ b/src/api/paths.ts @@ -17,3 +17,4 @@ export const opDeleteLinkURL: "api/operations/link" = opCreateLinkURL; export const opBackfillLinkURL = "api/operations/populate/link"; export const opBackfillChainURL = "api/operations/populate/chain"; export const opPruneLinkURL = "api/operations/prune/link"; +export const opPruneChainURL = "api/operations/prune/chain"; diff --git a/src/pages/Graph/index.tsx b/src/pages/Graph/index.tsx index 7d9095e..06e7cd6 100644 --- a/src/pages/Graph/index.tsx +++ b/src/pages/Graph/index.tsx @@ -48,6 +48,7 @@ import { apiCreateLink, apiDeleteLink, apiFetchGraph, + apiPruneChain, apiPruneLink, apiUpdateUserData, } from "../../api/operations.ts"; @@ -354,6 +355,34 @@ const Graph = (): React.ReactNode => { return; }; + const pruneChain = async () => { + if (selectedNodeID === "") { + showWarnToastNotification("Select a playlist!"); + return; + } + const selectedNode = playlistNodes.filter( + (nd) => nd.id === selectedNodeID + )[0]; + if (!selectedNode) throw new ReferenceError("no playlist selected"); + setLoading(true); + const resp = await APIWrapper({ + apiFn: apiPruneChain, + data: { + root: spotifyPlaylistLinkPrefix + selectedNodeID, + }, + refreshAuth, + }); + setLoading(false); + + if (resp?.status === 200) { + if (resp?.data.deletedNum < resp?.data.toDelNum) + showWarnToastNotification(resp?.data.message); + else showSuccessToastNotification(resp?.data.message); + return; + } + return; + }; + type getLayoutedElementsOpts = { direction: rankdirType; }; @@ -570,9 +599,9 @@ const Graph = (): React.ReactNode => { Prune Link -