Skip to content

Commit

Permalink
chore: the account deletion function continues to be improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Franqsanz committed Feb 22, 2024
1 parent 82f00c3 commit 051f502
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 22 deletions.
11 changes: 4 additions & 7 deletions src/components/modals/ModalConfirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
ModalHeader,
ModalCloseButton,
ModalFooter,
Box,
Alert,
Icon,
Button,
Expand All @@ -22,7 +21,9 @@ export function ModalConfirmation({
onClose,
onDeleteBook,
title,
warningText,
isPending,
isStrong,
}: ModalOptionsAndConfirType) {
const colorIconWar = useColorModeValue('yellow.700', 'yellow.300');

Expand All @@ -35,10 +36,7 @@ export function ModalConfirmation({
<ModalCloseButton />
<ModalBody>
¿Está seguro que desea eliminar{' '}
<Box as='span' fontWeight='bold'>
"{title}"
</Box>
?
{isStrong ? <strong>"{title}"</strong> : title}?
<Alert
mt='7'
status='warning'
Expand All @@ -51,8 +49,7 @@ export function ModalConfirmation({
mr='3'
color={colorIconWar}
/>
El libro será eliminado de manera permanente y no se podrá
recuperar.
{warningText}
</Alert>
</ModalBody>
<ModalFooter gap='3'>
Expand Down
2 changes: 2 additions & 0 deletions src/components/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ interface ModalOptionsAndConfirType
onDeleteBook?: () => any | void;
onEditBook?: () => any | void;
isPending?: boolean;
warningText?: string;
isStrong?: boolean;
id?: string;
title?: string;
authors?: string[];
Expand Down
18 changes: 15 additions & 3 deletions src/hooks/querys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getUserAndBooks,
updateBook,
deleteBook,
deleteAccount,
} from '@services/api';
import { useAccountActions } from '@hooks/useAccountActions';
import { keys } from '@utils/utils';
Expand Down Expand Up @@ -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();
},
});
}
Expand All @@ -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,
Expand All @@ -247,4 +258,5 @@ export {
useProfile,
useUpdateBook,
useDeleteBook,
useDeleteAccount,
};
9 changes: 8 additions & 1 deletion src/hooks/useAccountActions.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useNavigate } from 'react-router-dom';
import {
signOut,
reauthenticateWithCredential,
Expand All @@ -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 {
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/pages/Book.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ export default function Book() {
<ModalConfirmation
isOpen={isOpenConfirmation}
title={data.title}
isStrong={true}
warningText='El libro será eliminado de manera permanente y no se podrá recuperar.'
onDeleteBook={handleDeleteBook}
isPending={isPending}
onClose={() => {
Expand Down
31 changes: 21 additions & 10 deletions src/pages/profile/account/MyAccount.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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() {
Expand All @@ -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 (
<>
<MainHead title={`Mi Cuenta | XBuniverse`} />
<ContainerTitle title='Mi Cuenta' />
<Flex justify='center' h='230px' mt='70px'>
<Button
bgColor='red.300'
onClick={() => {
deleteAccount();
navigate('/');
}}
>
<Button bgColor='red.300' onClick={onOpen}>
Eliminar cuenta
</Button>
<ModalConfirmation
isOpen={isOpen}
title='su cuenta'
isStrong={false}
warningText='La eliminación de su cuenta, incluyendo las publicaciones, es irreversible.'
onDeleteBook={deleteAccount}
// isPending={ }
onClose={onClose}
/>
</Flex>
</>
);
Expand Down
11 changes: 10 additions & 1 deletion src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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,
Expand All @@ -143,4 +151,5 @@ export {
postRegister,
getCheckUser,
getUserAndBooks,
deleteAccount,
};

0 comments on commit 051f502

Please sign in to comment.