|
| 1 | +import { useState } from 'react' |
| 2 | +import toast from 'react-simple-toasts' |
| 3 | +import { Button, ConfirmModal } from 'src/components/Elements' |
| 4 | +import { useAuth } from 'src/features/auth' |
| 5 | +import { useDeleteAccount } from 'src/features/auth/api/deleteAccount' |
| 6 | + |
| 7 | +export const DeleteAccount = () => { |
| 8 | + const deleteAccountMutation = useDeleteAccount() |
| 9 | + const { user, logout } = useAuth() |
| 10 | + const [confirmDelete, setConfirmDelete] = useState(false) |
| 11 | + |
| 12 | + if (!user) { |
| 13 | + return null |
| 14 | + } |
| 15 | + |
| 16 | + const onDeleteAccount = () => { |
| 17 | + deleteAccountMutation |
| 18 | + .mutateAsync({ |
| 19 | + userId: user.id, |
| 20 | + }) |
| 21 | + .then(() => { |
| 22 | + logout() |
| 23 | + toast('Account deleted successfully', { theme: 'successToast' }) |
| 24 | + }) |
| 25 | + .catch(() => { |
| 26 | + toast('Failed to delete account', { theme: 'defaultToast' }) |
| 27 | + }) |
| 28 | + } |
| 29 | + |
| 30 | + return ( |
| 31 | + <div className="settingRow"> |
| 32 | + <p className="settingTitle"> |
| 33 | + Delete account? |
| 34 | + <br /> |
| 35 | + <span className="settingHint"> |
| 36 | + This action is irreversible and will delete all your data. Proceed with caution. |
| 37 | + </span> |
| 38 | + </p> |
| 39 | + <div className="settingContent"> |
| 40 | + <ConfirmModal |
| 41 | + showModal={confirmDelete} |
| 42 | + title="Confirm delete account" |
| 43 | + description="Are you sure you want to delete your account? This action is irreversible and will delete all your data including your streaks and settings." |
| 44 | + onClose={() => setConfirmDelete(false)} |
| 45 | + onConfirm={onDeleteAccount} |
| 46 | + /> |
| 47 | + |
| 48 | + <Button |
| 49 | + size="small" |
| 50 | + onClick={() => setConfirmDelete(true)} |
| 51 | + isLoading={deleteAccountMutation.isLoading}> |
| 52 | + Delete account |
| 53 | + </Button> |
| 54 | + </div> |
| 55 | + </div> |
| 56 | + ) |
| 57 | +} |
0 commit comments