implement bookmark delete
This commit is contained in:
28
packages/web/src/bookmark/api.ts
Normal file
28
packages/web/src/bookmark/api.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
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 }
|
Reference in New Issue
Block a user