From 6febe13b3f4ad4eff3f205ece445b3577255bf41 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Fri, 16 Feb 2024 20:33:00 +0000 Subject: Migrating to react query for client side queries --- .../app/dashboard/bookmarks/components/AddLink.tsx | 26 +++--- .../bookmarks/components/BookmarkOptions.tsx | 92 +++++++++++----------- 2 files changed, 59 insertions(+), 59 deletions(-) (limited to 'packages/web/app/dashboard/bookmarks/components') diff --git a/packages/web/app/dashboard/bookmarks/components/AddLink.tsx b/packages/web/app/dashboard/bookmarks/components/AddLink.tsx index e8ecec35..34f043e7 100644 --- a/packages/web/app/dashboard/bookmarks/components/AddLink.tsx +++ b/packages/web/app/dashboard/bookmarks/components/AddLink.tsx @@ -17,21 +17,19 @@ const formSchema = z.object({ export default function AddLink() { const router = useRouter(); + const bookmarkLinkMutator = api.bookmarks.bookmarkLink.useMutation({ + onSuccess: () => { + router.refresh(); + }, + onError: () => { + toast({ description: "Something went wrong", variant: "destructive" }); + }, + }); const form = useForm>({ resolver: zodResolver(formSchema), }); - async function onSubmit(value: z.infer) { - try { - await api.bookmarks.bookmarkLink.mutate({ url: value.url, type: "link" }); - } catch (e) { - toast({ description: "Something went wrong", variant: "destructive" }); - return; - } - router.refresh(); - } - const onError: SubmitErrorHandler> = (errors) => { toast({ description: Object.values(errors) @@ -43,7 +41,13 @@ export default function AddLink() { return (
- + + bookmarkLinkMutator.mutate({ url: value.url, type: "link" }), + onError, + )} + >
{ - try { - await api.bookmarks.deleteBookmark.mutate({ - bookmarkId: linkId, - }); - + const onError = () => { + toast({ + variant: "destructive", + title: "Something went wrong", + description: "There was a problem with your request.", + }); + }; + const onSettled = () => { + router.refresh(); + }; + const deleteBookmarkMutator = api.bookmarks.deleteBookmark.useMutation({ + onSuccess: () => { toast({ description: "The bookmark has been deleted!", }); - } catch (e) { - toast({ - variant: "destructive", - title: "Something went wrong", - description: "There was a problem with your request.", - }); - } - - router.refresh(); - }; + }, + onError, + onSettled, + }); - const updateBookmark = async (req: ZUpdateBookmarksRequest) => { - try { - await api.bookmarks.updateBookmark.mutate(req); + const updateBookmarkMutator = api.bookmarks.updateBookmark.useMutation({ + onSuccess: () => { toast({ description: "The bookmark has been updated!", }); - } catch (e) { - toast({ - variant: "destructive", - title: "Something went wrong", - description: "There was a problem with your request.", - }); - } + }, + onError, + onSettled, + }); - router.refresh(); - }; - - const crawlBookmark = async () => { - try { - await api.bookmarks.recrawlBookmark.mutate({ - bookmarkId: linkId, - }); + const crawlBookmarkMutator = api.bookmarks.recrawlBookmark.useMutation({ + onSuccess: () => { toast({ description: "Re-fetch has been enqueued!", }); - } catch (e) { - toast({ - variant: "destructive", - title: "Something went wrong", - description: "There was a problem with your request.", - }); - } - - router.refresh(); - }; + }, + onError, + onSettled, + }); return ( @@ -84,7 +68,7 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) { - updateBookmark({ + updateBookmarkMutator.mutate({ bookmarkId: linkId, favourited: !bookmark.favourited, }) @@ -95,17 +79,29 @@ export default function BookmarkOptions({ bookmark }: { bookmark: ZBookmark }) { - updateBookmark({ bookmarkId: linkId, archived: !bookmark.archived }) + updateBookmarkMutator.mutate({ + bookmarkId: linkId, + archived: !bookmark.archived, + }) } > {bookmark.archived ? "Un-archive" : "Archive"} - + + crawlBookmarkMutator.mutate({ bookmarkId: bookmark.id }) + } + > Refresh - + + deleteBookmarkMutator.mutate({ bookmarkId: bookmark.id }) + } + > Delete -- cgit v1.2.3-70-g09d2