Skip to content

Commit

Permalink
chore: the account deletion button and other changes are being implem…
Browse files Browse the repository at this point in the history
…ented
  • Loading branch information
Franqsanz committed Feb 16, 2024
1 parent 96546fe commit a1aefe0
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/components/nav/menu/MenuProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import {
MenuDivider,
} from '@chakra-ui/react';

import { logOut } from '@services/firebase/auth';
import { useAccountActions } from '@hooks/useAccountActions';
import { MenuType } from '@components/types';

export function MenuProfile({ displayName, photoURL, username }: MenuType) {
const navigate = useNavigate();
const { logOut } = useAccountActions();
const colorBorder = useColorModeValue('black', 'white');

return (
Expand Down
16 changes: 13 additions & 3 deletions src/hooks/querys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
useInfiniteQuery,
QueryClient,
} from '@tanstack/react-query';
import { useNavigate } from 'react-router-dom';

import {
getAllBooks,
Expand All @@ -24,7 +23,7 @@ import {
updateBook,
deleteBook,
} from '@services/api';
import { logOut } from '@services/firebase/auth';
import { useAccountActions } from '@hooks/useAccountActions';
import { keys } from '@utils/utils';
import { BookType } from '@components/types';
// import { useAuth } from '@contexts/AuthContext';
Expand Down Expand Up @@ -81,6 +80,7 @@ function useAllSearchBooks(book: string) {
queryFn: () => getAllSearchBooks(book),
refetchOnWindowFocus: false,
enabled: false,
retry: 1,
});
}

Expand All @@ -102,6 +102,7 @@ function useBooksPaginate() {

return lastPage.info.nextPage;
},
retry: 1,
});
}

Expand All @@ -110,6 +111,7 @@ function useFilter(query: string | undefined, param: string | undefined) {
queryKey: [keys.filter, query, param],
queryFn: () => getBooksFilter(query, param),
gcTime: 3000,
retry: 1,
});
}

Expand All @@ -120,6 +122,7 @@ function useMoreBooks() {
refetchOnWindowFocus: false,
gcTime: 3000,
staleTime: 50000,
retry: 1,
});
}

Expand All @@ -130,6 +133,7 @@ function useRelatedBooks(id: string | undefined) {
refetchOnWindowFocus: false,
gcTime: 3000,
staleTime: 50000,
retry: 1,
});
}

Expand All @@ -140,6 +144,7 @@ function useMoreBooksAuthors(id: string | undefined) {
refetchOnWindowFocus: false,
gcTime: 3000,
staleTime: 50000,
retry: 1,
});
}

Expand All @@ -156,6 +161,8 @@ function useBook(pathUrl: string | undefined) {
// Usuarios

function useUserRegister(body: any) {
const { logOut } = useAccountActions();

return useMutation({
mutationKey: [keys.userRegister],
mutationFn: (token: string) => postRegister(token, body),
Expand All @@ -181,7 +188,6 @@ function useUserData(id: string | undefined) {
queryKey: [keys.userData, id],
queryFn: () => getCheckUser(id),
staleTime: 0,
// refetchInterval: 1000,
retry: 4,
});
}
Expand All @@ -199,6 +205,8 @@ function useProfile(
}

function useDeleteBook() {
const { logOut } = useAccountActions();

return useMutation({
mutationKey: [keys.deleteBook],
mutationFn: (id: string | undefined) => deleteBook(id),
Expand All @@ -210,6 +218,8 @@ function useDeleteBook() {
}

function useUpdateBook(book: any) {
const { logOut } = useAccountActions();

return useMutation({
mutationKey: [keys.updateBook],
mutationFn: (id: string | undefined) => updateBook(id, book),
Expand Down
39 changes: 39 additions & 0 deletions src/hooks/useAccountActions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
signOut,
reauthenticateWithCredential,
GoogleAuthProvider,
} from 'firebase/auth';

import { logIn } from '@services/auth/config';
import { useAuth } from '@contexts/AuthContext';

export function useAccountActions() {
const { currentUser } = useAuth();

async function logOut() {
try {
await signOut(logIn);
await window.localStorage.removeItem('app_tk');
} catch (error) {
console.error('Error al cerrar sesión:', error);
}
}

async function deleteAccount() {
try {
if (currentUser) {
// const idToken = await currentUser?.;
// const credential = GoogleAuthProvider.credential(idToken);

// await reauthenticateWithCredential(currentUser, credential);
await currentUser?.delete(); // Elimina la cuenta de Firebase
await signOut(logIn);
await window.localStorage.removeItem('app_tk');
}
} catch (error) {
console.error('Error al borrar la cuenta:', error);
}
}

return { logOut, deleteAccount };
}
2 changes: 1 addition & 1 deletion src/pages/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BsFacebook, BsTwitterX } from 'react-icons/bs';

import { ContainerTitle } from '@components/ContainerTitle';
import { MainHead } from '@components/Head';
import { SignIn } from '@services/firebase/auth';
import { SignIn } from '@services/auth/auth';

export function Login() {
return (
Expand Down
15 changes: 13 additions & 2 deletions src/pages/profile/Profile.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect } from 'react';
import { useParams, useLocation } from 'react-router-dom';
import { Box, Flex, Image, useColorModeValue } from '@chakra-ui/react';
import { useParams, useLocation, useNavigate } from 'react-router-dom';
import { Box, Button, Flex, Image, useColorModeValue } from '@chakra-ui/react';

import { MySimpleGrid } from '@components/MySimpleGrid';
import { Card } from '@components/cards/Card';
Expand All @@ -11,13 +11,16 @@ import { parseDate } from '@utils/utils';
import { CardType } from '@components/types';
import ResultLength from '@components/ResultLength';
import { useAuth } from '@contexts/AuthContext';
import { useAccountActions } from '@hooks/useAccountActions';
// import { logOut } from '../../services/firebase/auth';

export default function Profile() {
const getToken = window.localStorage.getItem('app_tk');
const { deleteAccount } = useAccountActions();
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');

Expand Down Expand Up @@ -64,6 +67,14 @@ export default function Profile() {
<Box mt={{ base: 5, md: 8 }} mb='1' fontSize={{ base: 'md', md: 'lg' }}>
PUBLICACIONES
</Box>
<Button
onClick={() => {
deleteAccount();
navigate('/');
}}
>
Eliminar cuenta
</Button>
</Flex>
<Flex
direction={{ base: 'column', md: 'row' }}
Expand Down
31 changes: 9 additions & 22 deletions src/services/firebase/auth.tsx → src/services/auth/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import React, { useState, useEffect } from 'react';
import { Button, useColorModeValue } from '@chakra-ui/react';
import { useNavigate } from 'react-router-dom';
import { GrGoogle } from 'react-icons/gr';
import { GoogleAuthProvider, signInWithPopup, signOut } from 'firebase/auth';
import { GoogleAuthProvider, signInWithPopup } from 'firebase/auth';

import { logIn } from './config';
import { useCheckUser } from '@hooks/querys';
import { useAuth } from '@contexts/AuthContext';
import { useAccountActions } from '@hooks/useAccountActions';

const provider = new GoogleAuthProvider();

provider.setCustomParameters({ prompt: 'select_account ' });

function SignIn() {
export function SignIn() {
const provider = new GoogleAuthProvider();
provider.setCustomParameters({ prompt: 'select_account' });
const navigate = useNavigate();
const { currentUser, loading } = useAuth();
const { currentUser } = useAuth();
const { logOut } = useAccountActions();
const [userId, setUserId] = useState('');
const { data, isPending, error, refetch } = useCheckUser(userId);

Expand Down Expand Up @@ -84,24 +84,11 @@ function SignIn() {
_hover={{ bg: '#D23C2F' }}
_active={{ bg: '#BB352A' }}
onClick={SignInWithGoogle}
// loadingText='Redirigiendo...'
// isLoading={loading}
loadingText='Redirigiendo...'
isLoading={!isPending}
>
Continuar con Google
</Button>
{/* <div>{errorUI}</div> */}
</>
);
}

async function logOut() {
try {
await signOut(logIn);
await window.localStorage.removeItem('app_tk');
await window.localStorage.removeItem('app_ud');
} catch (error) {
console.error('Error al cerrar sesión:', error);
}
}

export { SignIn, logOut };
File renamed without changes.

0 comments on commit a1aefe0

Please sign in to comment.