diff --git a/src/components/modals/ModalCollection.tsx b/src/components/modals/ModalCollection.tsx index b4ce607..3617770 100644 --- a/src/components/modals/ModalCollection.tsx +++ b/src/components/modals/ModalCollection.tsx @@ -17,10 +17,9 @@ import { import { useCreateCollections } from '@hooks/queries'; import { useAuth } from '@contexts/AuthContext'; +import { DisclosureType } from '@components/types'; -interface ModalCollectionType { - isOpen: boolean; - onClose: () => void; +interface ModalCollectionType extends DisclosureType { refetch: () => void; } @@ -33,6 +32,15 @@ export function ModalCollection({ isOpen, onClose, refetch }: ModalCollectionTyp const navigate = useNavigate(); const { mutate, isPending, isSuccess } = useCreateCollections(uid); + useEffect(() => { + if (isSuccess) { + onClose(); + refetch(); + setName(''); + navigate('/my-collections'); + } + }, [isSuccess, navigate, onClose]); + function handleNameCollection(e: React.ChangeEvent) { const { value } = e.target; @@ -41,17 +49,11 @@ export function ModalCollection({ isOpen, onClose, refetch }: ModalCollectionTyp function handleSubmit(e: React.FormEvent) { e.preventDefault(); - mutate(name); - } - useEffect(() => { - if (isSuccess) { - onClose(); - refetch(); - setName(''); - navigate('/my-collections'); + if (!isPending) { + mutate(name); } - }, [isSuccess, navigate, onClose]); + } return ( <> @@ -83,7 +85,7 @@ export function ModalCollection({ isOpen, onClose, refetch }: ModalCollectionTyp fontWeight='normal' border='1px' rounded='lg' - isDisabled={!name} + isDisabled={!name || isPending} isLoading={isPending} loadingText='Creando...' _hover={{ outline: 'none', bg: 'green.600' }} diff --git a/src/components/types.d.ts b/src/components/types.d.ts index d2fa767..bf5442d 100644 --- a/src/components/types.d.ts +++ b/src/components/types.d.ts @@ -199,6 +199,7 @@ interface AuthProviderType { export type { MyChangeEvent, AboutType, + DisclosureType, LinkType, SelectBooksType, SelectType, diff --git a/src/hooks/queries.ts b/src/hooks/queries.ts index e0374a2..ebc40b1 100644 --- a/src/hooks/queries.ts +++ b/src/hooks/queries.ts @@ -28,6 +28,7 @@ import { getBooksFilterPaginated, getFindAllBookFavorite, getFindAllCollections, + getFindOneCollection, deleteCollections, postCollections, } from '@services/api'; @@ -305,6 +306,15 @@ function useDeleteCollections() { }); } +function useCollectionDetail(collectionId: string | undefined) { + return useQuery({ + queryKey: [keys.collectionsDetail, collectionId], + queryFn: () => getFindOneCollection(collectionId), + refetchOnWindowFocus: true, + retry: false, + }); +} + function useDeleteBook() { return useMutation({ mutationKey: [keys.deleteBook], @@ -356,6 +366,7 @@ export { useMoreBooksAuthors, useFavoriteBook, useCollections, + useCollectionDetail, useCreateCollections, useDeleteCollections, diff --git a/src/pages/Favorites.tsx b/src/pages/Favorites.tsx index 91cc11a..9acd235 100644 --- a/src/pages/Favorites.tsx +++ b/src/pages/Favorites.tsx @@ -102,8 +102,6 @@ export default function Favorites() { Todavía no has añadido libros a tus favoritos. ; } if (!data || !data?.collections || data?.collections.length === 0) { @@ -65,50 +65,66 @@ export function MyCollections() { ); } else { collectionsUI = ( - + {data?.collections.map(({ id, name, createdAt }) => ( - + {/* */} - - {name} - {parseDate(createdAt)} - - + + + {name} + + {parseDate(createdAt)} + + + - + {/* */} + ))} - + ); } @@ -129,7 +145,8 @@ export function MyCollections() { align='center' fontSize='lg' > - {data?.totalCollections} Colecciones + {data?.totalCollections ?? 0}{' '} + {data?.totalCollections === 1 ? 'Colección' : 'Colecciones'} + + + + + + + {asideAndCardsUI} + + ); +} diff --git a/src/routes.tsx b/src/routes.tsx index 0cdb56f..009fecf 100644 --- a/src/routes.tsx +++ b/src/routes.tsx @@ -14,7 +14,8 @@ import { PrivateRoute } from '@components/nav/PrivateRoute'; import { MyAccount } from '@pages/profile/account/MyAccount'; import { Profile } from '@pages/profile/Profile'; import { RouteWatcher } from '@hooks/RouteWatcher'; -import { MyCollections } from '@pages/profile/MyCollections'; +import { AllCollections } from '@pages/profile/collections/AllCollections'; +import { CollectionDetail } from '@pages/profile/collections/CollectionDetail'; const PrivacyPolicies = lazy(() => import('@pages/PrivacyPolicies')); const Explore = lazy(() => import('@pages/Explore')); @@ -128,7 +129,15 @@ export const routes = createBrowserRouter([ path: '/my-collections', element: ( - + + + ), + }, + { + path: '/my-collections/:collectionId', + element: ( + + ), }, diff --git a/src/services/api.ts b/src/services/api.ts index 7cf1a5c..84d1861 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -89,6 +89,10 @@ async function deleteCollections(id: string | undefined, collectionId: string) { }); } +async function getFindOneCollection(collectionsId: string | undefined) { + return await fetchData(`${API_URL}/users/my-collections/${collectionsId}`); +} + async function postBook(books: any) { return await fetchData(`${API_URL}/books`, { method: 'POST', @@ -174,6 +178,7 @@ export { getFindAllCollections, postCollections, deleteCollections, + getFindOneCollection, postBook, deleteBook, updateBook, diff --git a/src/utils/utils.ts b/src/utils/utils.ts index f2712ab..1904cd2 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -25,6 +25,7 @@ const keys = { userFavoriteBooks: 'UserFavoriteBooks', createCollections: 'CreateCollections', allCollections: 'AllCollections', + collectionsDetail: 'CollectionsDetail', deleteCollections: 'DeleteCollections', deleteAccount: 'DeleteAccount', };