Skip to content

Commit 74245c3

Browse files
feat: migrate artist delete to dedicated api (#1665)
* feat: migrate artist delete to dedicated api * fix: avoid rollback after successful artist delete * refactor: share artist delete flow in hook
1 parent 5a4fe8e commit 74245c3

5 files changed

Lines changed: 91 additions & 37 deletions

File tree

app/api/artist/remove/route.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

components/ArtistSetting/DeleteModal.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import { useArtistProvider } from "@/providers/ArtistProvider";
2-
import { ArtistRecord } from "@/types/Artist";
2+
import useDeleteArtist from "@/hooks/useDeleteArtist";
33

44
interface DeleteModalProps {
55
toggleModal: () => void;
66
}
77

88
const DeleteModal = ({ toggleModal }: DeleteModalProps) => {
9-
const { editableArtist, artists, setArtists, toggleSettingModal } =
10-
useArtistProvider();
9+
const { editableArtist, toggleSettingModal } = useArtistProvider();
10+
const { deleteArtist } = useDeleteArtist();
1111

1212
const handleDelete = async () => {
13-
const temp = artists.filter(
14-
(artistEle: ArtistRecord) =>
15-
artistEle.account_id !== editableArtist?.account_id,
16-
);
17-
setArtists([...temp]);
13+
const artistId = editableArtist?.account_id;
14+
if (!artistId) {
15+
return;
16+
}
1817
toggleModal();
1918
toggleSettingModal();
20-
await fetch(`/api/artist/remove?artistId=${editableArtist?.account_id}`);
19+
await deleteArtist(artistId);
2120
};
2221

2322
return (

components/Artists/DropDown.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@ import { ArtistRecord } from "@/types/Artist";
33
import { Trash2 } from "lucide-react";
44
import { containerPatterns, textPatterns } from "@/lib/styles/patterns";
55
import { cn } from "@/lib/utils";
6+
import useDeleteArtist from "@/hooks/useDeleteArtist";
67

78
const DropDown = ({ artist }: { artist: ArtistRecord }) => {
8-
const { setArtists, artists, setMenuVisibleArtistId, getArtists } =
9-
useArtistProvider();
9+
const { setMenuVisibleArtistId } = useArtistProvider();
10+
const { deleteArtist } = useDeleteArtist();
1011

1112
const handleDelete = async () => {
12-
const temp = artists.filter(
13-
(artistEle: ArtistRecord) => artistEle.account_id !== artist.account_id,
14-
);
15-
setArtists([...temp]);
16-
setMenuVisibleArtistId(null);
17-
await fetch(`/api/artist/remove?artistId=${artist.account_id}`);
18-
getArtists();
13+
await deleteArtist(artist.account_id, {
14+
onSuccess: () => setMenuVisibleArtistId(null),
15+
});
1916
};
2017

2118
return (

hooks/useDeleteArtist.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { usePrivy } from "@privy-io/react-auth";
2+
import { toast } from "sonner";
3+
import { deleteArtist } from "@/lib/artists/deleteArtist";
4+
import { useArtistProvider } from "@/providers/ArtistProvider";
5+
6+
interface DeleteArtistOptions {
7+
onSuccess?: () => void;
8+
}
9+
10+
interface UseDeleteArtistReturn {
11+
deleteArtist: (artistId: string, options?: DeleteArtistOptions) => Promise<void>;
12+
}
13+
14+
export default function useDeleteArtist(): UseDeleteArtistReturn {
15+
const { getAccessToken } = usePrivy();
16+
const { artists, setArtists, getArtists } = useArtistProvider();
17+
18+
const deleteArtistHandler = async (
19+
artistId: string,
20+
options?: DeleteArtistOptions,
21+
): Promise<void> => {
22+
const previousArtists = artists;
23+
const nextArtists = artists.filter(artist => artist.account_id !== artistId);
24+
setArtists(nextArtists);
25+
26+
try {
27+
const accessToken = await getAccessToken();
28+
if (!accessToken) {
29+
throw new Error("Please sign in to delete an artist");
30+
}
31+
32+
await deleteArtist(accessToken, artistId);
33+
options?.onSuccess?.();
34+
} catch (error) {
35+
setArtists(previousArtists);
36+
toast.error(error instanceof Error ? error.message : "Failed to delete artist");
37+
return;
38+
}
39+
40+
try {
41+
await getArtists();
42+
} catch {
43+
toast.error("Artist deleted, but failed to refresh the artist list");
44+
}
45+
};
46+
47+
return {
48+
deleteArtist: deleteArtistHandler,
49+
};
50+
}

lib/artists/deleteArtist.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { getClientApiBaseUrl } from "@/lib/api/getClientApiBaseUrl";
2+
3+
interface DeleteArtistResponse {
4+
success?: boolean;
5+
error?: string;
6+
}
7+
8+
/**
9+
* Deletes an artist through the dedicated API.
10+
*
11+
* @param accessToken - Privy access token for Bearer auth
12+
* @param artistId - Artist account ID to delete
13+
*/
14+
export async function deleteArtist(accessToken: string, artistId: string): Promise<void> {
15+
const response = await fetch(`${getClientApiBaseUrl()}/api/artists/${artistId}`, {
16+
method: "DELETE",
17+
headers: {
18+
Authorization: `Bearer ${accessToken}`,
19+
},
20+
});
21+
22+
const data: DeleteArtistResponse = await response.json();
23+
24+
if (!response.ok || !data.success) {
25+
throw new Error(data.error || "Failed to delete artist");
26+
}
27+
}

0 commit comments

Comments
 (0)