Skip to content

Commit 4b50288

Browse files
committed
feat: implement account deletion functionality and integrate into GeneralSettings
1 parent 8051228 commit 4b50288

4 files changed

Lines changed: 91 additions & 10 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useMutation } from '@tanstack/react-query'
2+
import { axios } from 'src/lib/axios'
3+
import { MutationConfig } from 'src/lib/react-query'
4+
5+
type DeleteAccountDTO = {
6+
userId: string
7+
}
8+
const deleteAccount = ({ userId }: DeleteAccountDTO): Promise<{ authLink: string }> => {
9+
return axios.delete(`/engine/user/${userId}`, {})
10+
}
11+
12+
type QueryFnType = typeof deleteAccount
13+
14+
type UseGetArticlesOptions = {
15+
config?: MutationConfig<QueryFnType>
16+
}
17+
18+
export const useDeleteAccount = ({ config }: UseGetArticlesOptions = {}) => {
19+
return useMutation({
20+
...config,
21+
mutationFn: deleteAccount,
22+
})
23+
}

src/features/settings/components/GeneralSettings/GeneralSettings.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import 'react-toggle/style.css'
44
import { ChipsSet } from 'src/components/Elements'
55
import { Footer } from 'src/components/Layout'
66
import { SettingsContentLayout } from 'src/components/Layout/SettingsContentLayout'
7-
import { useAuth } from 'src/features/auth'
87
import {
98
identifyUserLinksInNewTab,
109
identifyUserListingMode,
@@ -17,6 +16,7 @@ import {
1716
} from 'src/lib/analytics'
1817
import { useUserPreferences } from 'src/stores/preferences'
1918
import { Option } from 'src/types'
19+
import { DeleteAccount } from '../UserSettings/DeleteAccount'
2020
import { UserInfo } from '../UserSettings/UserInfo'
2121
import { DNDSettings } from './DNDSettings'
2222
import './generalSettings.css'
@@ -63,16 +63,14 @@ export const GeneralSettings = () => {
6363
}
6464
}
6565

66-
const { user } = useAuth()
67-
6866
return (
6967
<SettingsContentLayout
7068
title="General Settings"
7169
description={
7270
'Customize your experience by selecting the number of cards you want to see, the search engine you want to use and more.'
7371
}>
7472
<div>
75-
{user != null && <UserInfo user={user} />}
73+
<UserInfo />
7674
<div className="settingRow">
7775
<p className="settingTitle">Max number of cards to display</p>
7876
<div className="settingContent">
@@ -137,6 +135,8 @@ export const GeneralSettings = () => {
137135

138136
<DNDSettings />
139137

138+
<DeleteAccount />
139+
140140
<Footer />
141141
</div>
142142
</SettingsContentLayout>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}

src/features/settings/components/UserSettings/UserInfo.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ import { FcGoogle } from 'react-icons/fc'
44
import { IoCheckmarkOutline } from 'react-icons/io5'
55
import { ReactComponent as StreakIcon } from 'src/assets/icons/fire_icon.svg'
66
import { Button, ConfirmModal } from 'src/components/Elements'
7-
import { useAuth, User } from 'src/features/auth'
7+
import { useAuth } from 'src/features/auth'
88
import { pluralize } from 'src/utils/String'
99

10-
interface UserInfoProps {
11-
user: User
12-
}
13-
14-
export const UserInfo = ({ user }: UserInfoProps) => {
10+
export const UserInfo = () => {
11+
const { user } = useAuth()
1512
const { logout, providerId } = useAuth()
1613
const providerName = providerId?.split('.')[0] || 'Unknown'
1714
const [showLogout, setShowLogout] = useState(false)
1815

16+
if (!user) {
17+
return null
18+
}
19+
1920
return (
2021
<div className="userContent">
2122
<ConfirmModal

0 commit comments

Comments
 (0)