forked from nhost/nhost
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sync Fork
committed
Nov 11, 2023
1 parent
5252fc4
commit 7b542c8
Showing
30 changed files
with
301 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
dashboard/src/features/account/settings/components/DeleteAccount/DeleteAccount.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import { useDialog } from '@/components/common/DialogProvider'; | ||
import { SettingsContainer } from '@/components/layout/SettingsContainer'; | ||
import { Box } from '@/components/ui/v2/Box'; | ||
import { Button } from '@/components/ui/v2/Button'; | ||
import { Checkbox } from '@/components/ui/v2/Checkbox'; | ||
import { Text } from '@/components/ui/v2/Text'; | ||
import { getToastStyleProps } from '@/utils/constants/settings'; | ||
import { | ||
useDeleteUserAccountMutation, | ||
useGetAllWorkspacesAndProjectsQuery, | ||
} from '@/utils/__generated__/graphql'; | ||
import { type ApolloError } from '@apollo/client'; | ||
import { useSignOut, useUserData } from '@nhost/nextjs'; | ||
import { useRouter } from 'next/router'; | ||
import { useState } from 'react'; | ||
import toast from 'react-hot-toast'; | ||
import { twMerge } from 'tailwind-merge'; | ||
|
||
function ConfirmDeleteAccountModal({ | ||
close, | ||
onDelete, | ||
}: { | ||
onDelete?: () => Promise<any>; | ||
close: () => void; | ||
}) { | ||
const [remove, setRemove] = useState(false); | ||
const [loadingRemove, setLoadingRemove] = useState(false); | ||
|
||
const user = useUserData(); | ||
|
||
const { data, loading } = useGetAllWorkspacesAndProjectsQuery({ | ||
skip: !user, | ||
}); | ||
|
||
const userHasProjects = | ||
!loading && data?.workspaces.some((workspace) => workspace.projects.length); | ||
|
||
const userData = useUserData(); | ||
|
||
const [deleteUserAccount] = useDeleteUserAccountMutation({ | ||
variables: { id: userData?.id }, | ||
}); | ||
|
||
const onClickConfirm = async () => { | ||
setLoadingRemove(true); | ||
|
||
await toast.promise( | ||
deleteUserAccount(), | ||
{ | ||
loading: 'Deleting your account...', | ||
success: `The account has been deleted successfully.`, | ||
error: (arg: ApolloError) => { | ||
// we need to get the internal error message from the GraphQL error | ||
const { internal } = arg.graphQLErrors[0]?.extensions || {}; | ||
const { message } = (internal as Record<string, any>)?.error || {}; | ||
|
||
// we use the default Apollo error message if we can't find the | ||
// internal error message | ||
return ( | ||
message || | ||
arg.message || | ||
'An error occurred while deleting your account. Please try again.' | ||
); | ||
}, | ||
}, | ||
getToastStyleProps(), | ||
); | ||
|
||
onDelete?.(); | ||
close(); | ||
}; | ||
|
||
return ( | ||
<Box className={twMerge('w-full rounded-lg p-6 text-left')}> | ||
<div className="grid grid-flow-row gap-1"> | ||
<Text variant="h3" component="h2"> | ||
Delete Account? | ||
</Text> | ||
|
||
{userHasProjects && ( | ||
<Text | ||
variant="subtitle2" | ||
className="font-bold" | ||
sx={{ color: (theme) => `${theme.palette.error.main} !important` }} | ||
> | ||
You still have active projects. Please delete your projects before | ||
proceeding with the account deletion. | ||
</Text> | ||
)} | ||
|
||
<Box className="my-4"> | ||
<Checkbox | ||
id="accept-1" | ||
label={`I'm sure I want to delete my account`} | ||
className="py-2" | ||
checked={remove} | ||
onChange={(_event, checked) => setRemove(checked)} | ||
aria-label="Confirm Delete Project #1" | ||
/> | ||
</Box> | ||
|
||
<div className="grid grid-flow-row gap-2"> | ||
<Button | ||
color="error" | ||
onClick={onClickConfirm} | ||
disabled={userHasProjects} | ||
loading={loadingRemove} | ||
> | ||
Delete | ||
</Button> | ||
|
||
<Button variant="outlined" color="secondary" onClick={close}> | ||
Cancel | ||
</Button> | ||
</div> | ||
</div> | ||
</Box> | ||
); | ||
} | ||
|
||
export default function DeleteAccount() { | ||
const router = useRouter(); | ||
const { signOut } = useSignOut(); | ||
|
||
const { openDialog, closeDialog } = useDialog(); | ||
|
||
const onDelete = async () => { | ||
await signOut(); | ||
await router.push('/signin'); | ||
}; | ||
|
||
const confirmDeleteAccount = async () => { | ||
openDialog({ | ||
component: ( | ||
<ConfirmDeleteAccountModal close={closeDialog} onDelete={onDelete} /> | ||
), | ||
}); | ||
}; | ||
|
||
return ( | ||
<SettingsContainer | ||
title="Delete Account" | ||
description="Please proceed with caution as the removal of your Personal Account and its contents from the Nhost platform is irreversible. This action will permanently delete your account and all associated data." | ||
className="px-0" | ||
slotProps={{ | ||
submitButton: { className: 'hidden' }, | ||
footer: { className: 'hidden' }, | ||
}} | ||
> | ||
<Box className="grid grid-flow-row border-t-1"> | ||
<Button | ||
color="error" | ||
className="mx-4 mt-4 justify-self-end" | ||
onClick={confirmDeleteAccount} | ||
> | ||
Delete Personal Account | ||
</Button> | ||
</Box> | ||
</SettingsContainer> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
dashboard/src/features/account/settings/components/DeleteAccount/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as DeleteAccount } from './DeleteAccount'; |
5 changes: 5 additions & 0 deletions
5
dashboard/src/features/account/settings/gql/deleteUserAccount.gql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mutation deleteUserAccount($id: uuid!) { | ||
deleteUser(id: $id) { | ||
__typename | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
# @nhost/react-urql | ||
|
||
## 3.0.1 | ||
|
||
### Patch Changes | ||
|
||
- @nhost/react@2.1.1 | ||
|
||
## 3.0.0 | ||
|
||
### Patch Changes | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.