From 1d76c05844b3a99415330636261131f2d41b255b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sat, 9 Nov 2024 23:34:45 +0100 Subject: [PATCH] pl-fe: Migrate bookmark folders to tanstack query MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../api/hooks/statuses/use-bookmark-folder.ts | 29 +------------------ .../hooks/statuses/use-bookmark-folders.ts | 26 +++++++---------- .../statuses/use-create-bookmark-folder.ts | 20 +++++-------- .../statuses/use-delete-bookmark-folder.ts | 19 +++++------- .../statuses/use-update-bookmark-folder.ts | 20 +++++-------- .../components/new-folder-form.tsx | 6 ++-- .../src/features/bookmark-folders/index.tsx | 2 +- .../pl-fe/src/features/bookmarks/index.tsx | 4 +-- .../modals/edit-bookmark-folder-modal.tsx | 6 ++-- .../modals/select-bookmark-folder-modal.tsx | 4 +-- packages/pl-fe/tailwind/colors.ts | 2 +- 11 files changed, 48 insertions(+), 90 deletions(-) diff --git a/packages/pl-fe/src/api/hooks/statuses/use-bookmark-folder.ts b/packages/pl-fe/src/api/hooks/statuses/use-bookmark-folder.ts index 397517553..79f145aaa 100644 --- a/packages/pl-fe/src/api/hooks/statuses/use-bookmark-folder.ts +++ b/packages/pl-fe/src/api/hooks/statuses/use-bookmark-folder.ts @@ -1,32 +1,5 @@ -import { Entities } from 'pl-fe/entity-store/entities'; -import { selectEntity } from 'pl-fe/entity-store/selectors'; -import { useAppSelector } from 'pl-fe/hooks/use-app-selector'; - import { useBookmarkFolders } from './use-bookmark-folders'; -import type{ BookmarkFolder } from 'pl-api'; - -const useBookmarkFolder = (folderId?: string) => { - const { - isError, - isFetched, - isFetching, - isLoading, - invalidate, - } = useBookmarkFolders(); - - const bookmarkFolder = useAppSelector(state => folderId - ? selectEntity(state, Entities.BOOKMARK_FOLDERS, folderId) - : undefined); - - return { - bookmarkFolder, - isError, - isFetched, - isFetching, - isLoading, - invalidate, - }; -}; +const useBookmarkFolder = (folderId?: string) => useBookmarkFolders((data) => folderId ? data.find(folder => folder.id === folderId) : undefined); export { useBookmarkFolder }; diff --git a/packages/pl-fe/src/api/hooks/statuses/use-bookmark-folders.ts b/packages/pl-fe/src/api/hooks/statuses/use-bookmark-folders.ts index f936e4f19..7e857b1ac 100644 --- a/packages/pl-fe/src/api/hooks/statuses/use-bookmark-folders.ts +++ b/packages/pl-fe/src/api/hooks/statuses/use-bookmark-folders.ts @@ -1,26 +1,22 @@ -import { Entities } from 'pl-fe/entity-store/entities'; -import { useEntities } from 'pl-fe/entity-store/hooks/use-entities'; +import { useQuery } from '@tanstack/react-query'; + import { useClient } from 'pl-fe/hooks/use-client'; import { useFeatures } from 'pl-fe/hooks/use-features'; import type { BookmarkFolder } from 'pl-api'; -const useBookmarkFolders = () => { +const useBookmarkFolders = ( + select?: ((data: Array) => T), +) => { const client = useClient(); const features = useFeatures(); - const { entities, ...result } = useEntities( - [Entities.BOOKMARK_FOLDERS], - () => client.myAccount.getBookmarkFolders(), - { enabled: features.bookmarkFolders }, - ); - - const bookmarkFolders = entities; - - return { - ...result, - bookmarkFolders, - }; + return useQuery({ + queryKey: ['bookmarkFolders'], + queryFn: () => client.myAccount.getBookmarkFolders(), + enabled: features.bookmarkFolders, + select, + }); }; export { useBookmarkFolders }; diff --git a/packages/pl-fe/src/api/hooks/statuses/use-create-bookmark-folder.ts b/packages/pl-fe/src/api/hooks/statuses/use-create-bookmark-folder.ts index 8f351906b..0cc9acfc8 100644 --- a/packages/pl-fe/src/api/hooks/statuses/use-create-bookmark-folder.ts +++ b/packages/pl-fe/src/api/hooks/statuses/use-create-bookmark-folder.ts @@ -1,6 +1,7 @@ -import { Entities } from 'pl-fe/entity-store/entities'; -import { useCreateEntity } from 'pl-fe/entity-store/hooks/use-create-entity'; +import { useMutation } from '@tanstack/react-query'; + import { useClient } from 'pl-fe/hooks/use-client'; +import { queryClient } from 'pl-fe/queries/client'; interface CreateBookmarkFolderParams { name: string; @@ -10,16 +11,11 @@ interface CreateBookmarkFolderParams { const useCreateBookmarkFolder = () => { const client = useClient(); - const { createEntity, ...rest } = useCreateEntity( - [Entities.BOOKMARK_FOLDERS], - (params: CreateBookmarkFolderParams) => - client.myAccount.createBookmarkFolder(params), - ); - - return { - createBookmarkFolder: createEntity, - ...rest, - }; + return useMutation({ + mutationKey: ['bookmarkFolders', 'create'], + mutationFn: (params: CreateBookmarkFolderParams) => client.myAccount.createBookmarkFolder(params), + onSettled: () => queryClient.invalidateQueries({ queryKey: ['bookmarkFolders'] }), + }); }; export { useCreateBookmarkFolder }; diff --git a/packages/pl-fe/src/api/hooks/statuses/use-delete-bookmark-folder.ts b/packages/pl-fe/src/api/hooks/statuses/use-delete-bookmark-folder.ts index feaf10793..9c96d5786 100644 --- a/packages/pl-fe/src/api/hooks/statuses/use-delete-bookmark-folder.ts +++ b/packages/pl-fe/src/api/hooks/statuses/use-delete-bookmark-folder.ts @@ -1,19 +1,16 @@ -import { Entities } from 'pl-fe/entity-store/entities'; -import { useDeleteEntity } from 'pl-fe/entity-store/hooks/use-delete-entity'; +import { useMutation } from '@tanstack/react-query'; + import { useClient } from 'pl-fe/hooks/use-client'; +import { queryClient } from 'pl-fe/queries/client'; const useDeleteBookmarkFolder = () => { const client = useClient(); - const { deleteEntity, isSubmitting } = useDeleteEntity( - Entities.BOOKMARK_FOLDERS, - (folderId: string) => client.myAccount.deleteBookmarkFolder(folderId), - ); - - return { - deleteBookmarkFolder: deleteEntity, - isSubmitting, - }; + return useMutation({ + mutationKey: ['bookmarkFolders', 'delete'], + mutationFn: (folderId: string) => client.myAccount.deleteBookmarkFolder(folderId), + onSettled: () => queryClient.invalidateQueries({ queryKey: ['bookmarkFolders'] }), + }); }; export { useDeleteBookmarkFolder }; diff --git a/packages/pl-fe/src/api/hooks/statuses/use-update-bookmark-folder.ts b/packages/pl-fe/src/api/hooks/statuses/use-update-bookmark-folder.ts index 4fa91b784..13c6b28e9 100644 --- a/packages/pl-fe/src/api/hooks/statuses/use-update-bookmark-folder.ts +++ b/packages/pl-fe/src/api/hooks/statuses/use-update-bookmark-folder.ts @@ -1,6 +1,7 @@ -import { Entities } from 'pl-fe/entity-store/entities'; -import { useCreateEntity } from 'pl-fe/entity-store/hooks/use-create-entity'; +import { useMutation } from '@tanstack/react-query'; + import { useClient } from 'pl-fe/hooks/use-client'; +import { queryClient } from 'pl-fe/queries/client'; interface UpdateBookmarkFolderParams { name: string; @@ -10,16 +11,11 @@ interface UpdateBookmarkFolderParams { const useUpdateBookmarkFolder = (folderId: string) => { const client = useClient(); - const { createEntity, ...rest } = useCreateEntity( - [Entities.BOOKMARK_FOLDERS], - (params: UpdateBookmarkFolderParams) => - client.myAccount.updateBookmarkFolder(folderId, params), - ); - - return { - updateBookmarkFolder: createEntity, - ...rest, - }; + return useMutation({ + mutationKey: ['bookmarkFolders', 'update', folderId], + mutationFn: (params: UpdateBookmarkFolderParams) => client.myAccount.updateBookmarkFolder(folderId, params), + onSettled: () => queryClient.invalidateQueries({ queryKey: ['bookmarkFolders'] }), + }); }; export { useUpdateBookmarkFolder }; diff --git a/packages/pl-fe/src/features/bookmark-folders/components/new-folder-form.tsx b/packages/pl-fe/src/features/bookmark-folders/components/new-folder-form.tsx index 0a8c93a02..4726af8c9 100644 --- a/packages/pl-fe/src/features/bookmark-folders/components/new-folder-form.tsx +++ b/packages/pl-fe/src/features/bookmark-folders/components/new-folder-form.tsx @@ -20,7 +20,7 @@ const NewFolderForm: React.FC = () => { const name = useTextField(); - const { createBookmarkFolder, isSubmitting } = useCreateBookmarkFolder(); + const { mutate: createBookmarkFolder, isPending } = useCreateBookmarkFolder(); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); @@ -47,13 +47,13 @@ const NewFolderForm: React.FC = () => {