From 051f502ced25ac0d6c93dd15cacf4fc10acbfe11 Mon Sep 17 00:00:00 2001 From: franco sanchez Date: Thu, 22 Feb 2024 12:45:20 -0300 Subject: [PATCH] chore: the account deletion function continues to be improved --- src/components/modals/ModalConfirmation.tsx | 11 +++----- src/components/types.d.ts | 2 ++ src/hooks/querys.ts | 18 ++++++++++-- src/hooks/useAccountActions.tsx | 9 +++++- src/pages/Book.tsx | 2 ++ src/pages/profile/account/MyAccount.tsx | 31 ++++++++++++++------- src/services/api.ts | 11 +++++++- 7 files changed, 62 insertions(+), 22 deletions(-) diff --git a/src/components/modals/ModalConfirmation.tsx b/src/components/modals/ModalConfirmation.tsx index 5737c90..f2e652c 100644 --- a/src/components/modals/ModalConfirmation.tsx +++ b/src/components/modals/ModalConfirmation.tsx @@ -8,7 +8,6 @@ import { ModalHeader, ModalCloseButton, ModalFooter, - Box, Alert, Icon, Button, @@ -22,7 +21,9 @@ export function ModalConfirmation({ onClose, onDeleteBook, title, + warningText, isPending, + isStrong, }: ModalOptionsAndConfirType) { const colorIconWar = useColorModeValue('yellow.700', 'yellow.300'); @@ -35,10 +36,7 @@ export function ModalConfirmation({ ¿Está seguro que desea eliminar{' '} - - "{title}" - - ? + {isStrong ? "{title}" : title}? - El libro será eliminado de manera permanente y no se podrá - recuperar. + {warningText} diff --git a/src/components/types.d.ts b/src/components/types.d.ts index 987cd26..6e40367 100644 --- a/src/components/types.d.ts +++ b/src/components/types.d.ts @@ -147,6 +147,8 @@ interface ModalOptionsAndConfirType onDeleteBook?: () => any | void; onEditBook?: () => any | void; isPending?: boolean; + warningText?: string; + isStrong?: boolean; id?: string; title?: string; authors?: string[]; diff --git a/src/hooks/querys.ts b/src/hooks/querys.ts index 6bf8bb1..b0fff88 100644 --- a/src/hooks/querys.ts +++ b/src/hooks/querys.ts @@ -22,6 +22,7 @@ import { getUserAndBooks, updateBook, deleteBook, + deleteAccount, } from '@services/api'; import { useAccountActions } from '@hooks/useAccountActions'; import { keys } from '@utils/utils'; @@ -205,14 +206,11 @@ function useProfile( } function useDeleteBook() { - const { logOut } = useAccountActions(); - return useMutation({ mutationKey: [keys.deleteBook], mutationFn: (id: string | undefined) => deleteBook(id), onError: async (error) => { console.error('Error en el servidor:', error); - await logOut(); }, }); } @@ -230,6 +228,19 @@ function useUpdateBook(book: any) { }); } +function useDeleteAccount() { + // const { logOut } = useAccountActions(); + + return useMutation({ + mutationKey: ['deleteAccount'], + mutationFn: (id: string | undefined) => deleteAccount(id), + onError: async (error) => { + console.error('Error en el servidor:', error); + // await logOut(); + }, + }); +} + export { useMutatePost, useAllFilterOptions, @@ -247,4 +258,5 @@ export { useProfile, useUpdateBook, useDeleteBook, + useDeleteAccount, }; diff --git a/src/hooks/useAccountActions.tsx b/src/hooks/useAccountActions.tsx index e06719d..bb12757 100644 --- a/src/hooks/useAccountActions.tsx +++ b/src/hooks/useAccountActions.tsx @@ -1,3 +1,4 @@ +import { useNavigate } from 'react-router-dom'; import { signOut, reauthenticateWithCredential, @@ -6,9 +7,13 @@ import { import { logIn } from '@services/auth/config'; import { useAuth } from '@contexts/AuthContext'; +import { useDeleteAccount } from '@hooks/querys'; export function useAccountActions() { + const navigate = useNavigate(); const { currentUser } = useAuth(); + const uid = currentUser?.uid; + const { mutate } = useDeleteAccount(); async function logOut() { try { @@ -24,11 +29,13 @@ export function useAccountActions() { if (currentUser) { // const idToken = await currentUser?.; // const credential = GoogleAuthProvider.credential(idToken); - // await reauthenticateWithCredential(currentUser, credential); + + await mutate(uid); // Elimina la cuenta y sus libros de la base de datos await currentUser?.delete(); // Elimina la cuenta de Firebase await signOut(logIn); await window.localStorage.removeItem('app_tk'); + await navigate('/', { replace: true }); } } catch (error) { console.error('Error al borrar la cuenta:', error); diff --git a/src/pages/Book.tsx b/src/pages/Book.tsx index 312876d..e9ac67f 100644 --- a/src/pages/Book.tsx +++ b/src/pages/Book.tsx @@ -199,6 +199,8 @@ export default function Book() { { diff --git a/src/pages/profile/account/MyAccount.tsx b/src/pages/profile/account/MyAccount.tsx index 04cd453..01217c3 100644 --- a/src/pages/profile/account/MyAccount.tsx +++ b/src/pages/profile/account/MyAccount.tsx @@ -1,6 +1,13 @@ import React, { useEffect } from 'react'; -import { useParams, useLocation, useNavigate } from 'react-router-dom'; -import { Box, Button, Flex, Image, useColorModeValue } from '@chakra-ui/react'; +// import { useParams, useLocation, useNavigate } from 'react-router-dom'; +import { + Box, + Button, + Flex, + Image, + useColorModeValue, + useDisclosure, +} from '@chakra-ui/react'; // import { MySimpleGrid } from '@components/MySimpleGrid'; // import { Card } from '@components/cards/Card'; @@ -13,6 +20,7 @@ import { MainHead } from '@components/Head'; // import { useAuth } from '@contexts/AuthContext'; import { useAccountActions } from '@hooks/useAccountActions'; import { ContainerTitle } from '@components/ContainerTitle'; +import { ModalConfirmation } from '@components/modals/ModalConfirmation'; // import { logOut } from '../../services/firebase/auth'; export function MyAccount() { @@ -21,24 +29,27 @@ export function MyAccount() { // const { currentUser } = useAuth(); // const uid = currentUser?.uid; // const { username } = useParams(); - const navigate = useNavigate(); // const { data } = useProfile(username, uid, getToken); // const bgCover = useColorModeValue('gray.100', 'gray.700'); + const { isOpen, onOpen, onClose } = useDisclosure(); return ( <> - + ); diff --git a/src/services/api.ts b/src/services/api.ts index 9261ab9..93c3e34 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -114,7 +114,7 @@ async function getUserAndBooks( token: string | null, ) { const data = await fetchData( - `${API_URL}/users/my-books/${username}/${userId}`, + `${API_URL}/users/${username}/my-books/${userId}`, { method: 'GET', headers: { @@ -126,6 +126,14 @@ async function getUserAndBooks( return data; } +async function deleteAccount(id: string | undefined) { + const data = await fetchData(`${API_URL}/users/delete-account/${id}`, { + method: 'DELETE', + }); + + return data; +} + export { getAllBooks, getAllSearchBooks, @@ -143,4 +151,5 @@ export { postRegister, getCheckUser, getUserAndBooks, + deleteAccount, };