29 lines
816 B
TypeScript
29 lines
816 B
TypeScript
import type { Bookmark } from "@markone/core/bookmark"
|
|
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
|
import { useNavigate } from "@tanstack/react-router"
|
|
import { UnauthenticatedError, fetchApi } from "~/api"
|
|
|
|
function useDeleteBookmark() {
|
|
const navigate = useNavigate()
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: ({ bookmark }: { bookmark: Bookmark }) =>
|
|
fetchApi(`/bookmark/${bookmark.id}`, {
|
|
method: "DELETE",
|
|
}),
|
|
onError: (error) => {
|
|
if (error instanceof UnauthenticatedError) {
|
|
navigate({ to: "/login", replace: true })
|
|
}
|
|
},
|
|
onSuccess: (_, { bookmark }) => {
|
|
queryClient.setQueryData(["bookmarks"], (bookmarks: Bookmark[]) =>
|
|
bookmarks.filter((it) => it.id !== bookmark.id),
|
|
)
|
|
},
|
|
})
|
|
}
|
|
|
|
export { useDeleteBookmark }
|