From fef29ab07c5971227bfc5176ea92eb08e4011764 Mon Sep 17 00:00:00 2001 From: Andy Date: Sat, 16 Mar 2024 21:08:01 +0000 Subject: [PATCH] refactor: create component for UserInformationModal --- .../UserInformationModal.tsx | 99 +++++++++++++++++++ src/components/AuthUserInformation/index.tsx | 88 ++--------------- src/components/AuthUserInformation/types.ts | 6 ++ 3 files changed, 111 insertions(+), 82 deletions(-) create mode 100644 src/components/AuthUserInformation/UserInformationModal.tsx create mode 100644 src/components/AuthUserInformation/types.ts diff --git a/src/components/AuthUserInformation/UserInformationModal.tsx b/src/components/AuthUserInformation/UserInformationModal.tsx new file mode 100644 index 00000000..77365b1f --- /dev/null +++ b/src/components/AuthUserInformation/UserInformationModal.tsx @@ -0,0 +1,99 @@ +import { Auth } from '@supabase/auth-ui-react'; +import { ThemeSupa } from '@supabase/auth-ui-shared'; +import { useAtomValue } from 'jotai'; +import { LogOutIcon } from 'lucide-react'; +import { useEffect, useState } from 'react'; + +import { supabase } from '@/api'; +import { AvatarIcon } from '@/components/AvatarIcon'; +import { Button } from '@/components/Button'; +import { Modal } from '@/components/Modal'; +import { ModalConfirm } from '@/components/ModalConfirm'; +import { authAtom } from '@/context/auth'; +import { dbAtom } from '@/context/db'; +import { UserInformationModalProps } from './types'; + +export function UserInformationModal({ + open, + setOpen, +}: UserInformationModalProps) { + const db = useAtomValue(dbAtom); + const auth = useAtomValue(authAtom); + const [openSignOutConfirmation, setOpenSignOutConfirmation] = useState(false); + + const signOut = async () => { + setOpen(false); + + const { error } = await supabase.auth.signOut(); + if (error) console.error(error); // eslint-disable-line no-console + + db?.remove() + .then(() => window.location.reload()); // TODO: Remove the need for the page reload + }; + + useEffect(() => { + if (auth?.user) { + setOpen(false); + } + }, [auth?.user, setOpen]); + + return ( + setOpen(false)} + theme={auth ? undefined : 'dark'} + > + {auth ? ( + <> +
+ +
+ +
+

+ Email: + {auth.user.email || 'Signed in'} +

+
+ + + + setOpenSignOutConfirmation(false)} + onConfirm={() => { + setOpenSignOutConfirmation(false); + signOut(); + }} + /> + + ) : ( + + )} +
+ ); +} diff --git a/src/components/AuthUserInformation/index.tsx b/src/components/AuthUserInformation/index.tsx index 2d4c2fd3..6c745ee5 100644 --- a/src/components/AuthUserInformation/index.tsx +++ b/src/components/AuthUserInformation/index.tsx @@ -1,39 +1,16 @@ -import { Auth } from '@supabase/auth-ui-react'; -import { ThemeSupa } from '@supabase/auth-ui-shared'; import { useAtomValue } from 'jotai'; -import { LogOutIcon, UserIcon } from 'lucide-react'; -import { useEffect, useState } from 'react'; +import { UserIcon } from 'lucide-react'; +import { useState } from 'react'; -import { supabase } from '@/api'; import { AvatarIcon } from '@/components/AvatarIcon'; import { Button } from '@/components/Button'; -import { Modal } from '@/components/Modal'; -import { ModalConfirm } from '@/components/ModalConfirm'; import { Tooltip } from '@/components/Tooltip'; import { authAtom } from '@/context/auth'; -import { dbAtom } from '@/context/db'; +import { UserInformationModal } from './UserInformationModal'; export function AuthUserInformation() { const auth = useAtomValue(authAtom); - const db = useAtomValue(dbAtom); const [open, setOpen] = useState(false); - const [openSignOutConfirmation, setOpenSignOutConfirmation] = useState(false); - - const signOut = async () => { - setOpen(false); - - const { error } = await supabase.auth.signOut(); - if (error) console.error(error); // eslint-disable-line no-console - - db?.remove() - .then(() => window.location.reload()); // TODO: Remove the need for the page reload - }; - - useEffect(() => { - if (auth?.user) { - setOpen(false); - } - }, [auth?.user]); return ( <> @@ -53,63 +30,10 @@ export function AuthUserInformation() { - setOpen(false)} - theme={auth ? undefined : 'dark'} - > - {auth ? ( - <> -
- -
- -
-

- Email: - {auth.user.email || 'Signed in'} -

-
- - - - setOpenSignOutConfirmation(false)} - onConfirm={() => { - setOpenSignOutConfirmation(false); - signOut(); - }} - /> - - ) : ( - - )} -
+ setOpen={setOpen} + /> ); } diff --git a/src/components/AuthUserInformation/types.ts b/src/components/AuthUserInformation/types.ts new file mode 100644 index 00000000..ca23bcc9 --- /dev/null +++ b/src/components/AuthUserInformation/types.ts @@ -0,0 +1,6 @@ +import type { Dispatch, SetStateAction } from 'react'; + +export type UserInformationModalProps = { + open: boolean, + setOpen: Dispatch>, +};