import UserContext from '@/contexts/UserContext'; import { AccountPageState, AccountPageAction } from '@/reducers/accountPageReducer'; import { Switch } from '@headlessui/react'; import { useRouter } from 'next/router'; import { Dispatch, FunctionComponent, useContext, useRef } from 'react'; import { toast } from 'react-hot-toast'; interface DeleteAccountProps { pageState: AccountPageState; dispatch: Dispatch; } const DeleteAccount: FunctionComponent = ({ dispatch, pageState, }) => { const deleteRef = useRef(null); const router = useRouter(); const { user, mutate } = useContext(UserContext); const onDeleteSubmit = async () => { deleteRef.current!.close(); const loadingToast = toast.loading( 'Deleting your account. We are sad to see you go. 😭', ); const request = await fetch(`/api/users/${user?.id}`, { method: 'DELETE', }); if (!request.ok) { throw new Error('Could not delete that user.'); } toast.remove(loadingToast); toast.success('Deleted your account. Goodbye. 😓'); await mutate!(); router.push('/'); }; return (

Delete Your Account

Want to leave? Delete your account here.

{ dispatch({ type: 'TOGGLE_DELETE_ACCOUNT_VISIBILITY' }); }} />
{pageState.deleteAccountOpen && ( <>

{`You're about to delete your account.`}

This action is permanent and cannot be reversed.

)}
); }; export default DeleteAccount;