implement bookmark tagging
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { Bookmark } from "@markone/core/bookmark"
|
||||
import type { Bookmark, BookmarkTag } from "@markone/core/bookmark"
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
||||
import { useNavigate } from "@tanstack/react-router"
|
||||
import { UnauthenticatedError, fetchApi, useAuthenticatedQuery } from "~/api"
|
||||
@@ -11,6 +11,16 @@ function useBookmark(id: string) {
|
||||
)
|
||||
}
|
||||
|
||||
function useTags() {
|
||||
return useAuthenticatedQuery(["tags"], () => fetchApi("/tags").then((res): Promise<BookmarkTag[]> => res.json()))
|
||||
}
|
||||
|
||||
function useBookmarkTags(bookmark: Bookmark) {
|
||||
return useAuthenticatedQuery(["bookmarks", bookmark.id, "tags"], () =>
|
||||
fetchApi(`/bookmarks/${bookmark.id}/tags`).then((res): Promise<BookmarkTag[]> => res.json()),
|
||||
)
|
||||
}
|
||||
|
||||
function useDeleteBookmark() {
|
||||
const navigate = useNavigate()
|
||||
const queryClient = useQueryClient()
|
||||
@@ -38,15 +48,10 @@ function useCreateBookmark() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({ url, force = false }: { url: string; force?: boolean }) =>
|
||||
mutationFn: (body: { url: string; tags: string[]; force?: boolean }) =>
|
||||
fetchApi("/bookmarks", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
url,
|
||||
force,
|
||||
kind: "link",
|
||||
tags: [],
|
||||
}),
|
||||
body: JSON.stringify(body),
|
||||
}).then((res) => (res.status === 204 ? Promise.resolve() : res.json())),
|
||||
onError: (error) => {
|
||||
if (error instanceof UnauthenticatedError) {
|
||||
@@ -54,11 +59,15 @@ function useCreateBookmark() {
|
||||
}
|
||||
},
|
||||
onSuccess: (bookmark: Bookmark | undefined) => {
|
||||
console.log("on success bookmark", bookmark)
|
||||
if (bookmark) {
|
||||
queryClient.setQueryData(["bookmarks"], (bookmarks: Bookmark[]) => [bookmark, ...bookmarks])
|
||||
queryClient.setQueryData(["bookmarks"], (bookmarks: Bookmark[]) =>
|
||||
bookmarks ? [bookmark, ...bookmarks] : [bookmark],
|
||||
)
|
||||
console.log("query data updated")
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export { useBookmark, useDeleteBookmark, useCreateBookmark }
|
||||
export { useBookmark, useDeleteBookmark, useCreateBookmark, useTags, useBookmarkTags }
|
||||
|
Reference in New Issue
Block a user