-
Notifications
You must be signed in to change notification settings - Fork 441
Expand file tree
/
Copy pathDeleteUserForm.tsx
More file actions
87 lines (78 loc) · 2.84 KB
/
DeleteUserForm.tsx
File metadata and controls
87 lines (78 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { useClerk, useReverification, useUser } from '@clerk/shared/react';
import { useSignOutContext } from '../../contexts';
import { Col, localizationKeys, Text, useLocalizations } from '../../customizables';
import type { FormProps } from '../../elements';
import { Form, FormButtons, FormContainer, useCardState, withCardStateProvider } from '../../elements';
import { useMultipleSessions } from '../../hooks/useMultipleSessions';
import { handleError, useFormControl } from '../../utils';
type DeleteUserFormProps = FormProps;
export const DeleteUserForm = withCardStateProvider((props: DeleteUserFormProps) => {
const { onReset } = props;
const card = useCardState();
const {
afterSignOutUrl,
afterMultiSessionSingleSignOutUrl,
// navigateAfterSignOut
} = useSignOutContext();
const { user } = useUser();
const { t } = useLocalizations();
const { otherSessions } = useMultipleSessions({ user });
const { setActive } = useClerk();
const [deleteUserWithReverification] = useReverification(() => user?.delete());
const confirmationField = useFormControl('deleteConfirmation', '', {
type: 'text',
label: localizationKeys('userProfile.deletePage.actionDescription'),
isRequired: true,
placeholder: localizationKeys('formFieldInputPlaceholder__confirmDeletionUserAccount'),
});
const canSubmit =
confirmationField.value ===
(t(localizationKeys('formFieldInputPlaceholder__confirmDeletionUserAccount')) || 'Delete account');
const deleteUser = async () => {
if (!canSubmit) {
return;
}
try {
await deleteUserWithReverification();
const redirectUrl = otherSessions.length === 0 ? afterSignOutUrl : afterMultiSessionSingleSignOutUrl;
return await setActive({
session: null,
// beforeEmit: navigateAfterSignOut
redirectUrl,
});
} catch (e) {
handleError(e, [], card.setError);
}
};
return (
<FormContainer
headerTitle={localizationKeys('userProfile.deletePage.title')}
sx={t => ({ gap: t.space.$0x5 })}
>
<Form.Root onSubmit={deleteUser}>
<Col gap={1}>
<Text
colorScheme='secondary'
localizationKey={localizationKeys('userProfile.deletePage.messageLine1')}
/>
<Text
colorScheme='danger'
localizationKey={localizationKeys('userProfile.deletePage.messageLine2')}
/>
</Col>
<Form.ControlRow elementId={confirmationField.id}>
<Form.PlainInput
{...confirmationField.props}
ignorePasswordManager
/>
</Form.ControlRow>
<FormButtons
submitLabel={localizationKeys('userProfile.deletePage.confirm')}
colorScheme='danger'
isDisabled={!canSubmit}
onReset={onReset}
/>
</Form.Root>
</FormContainer>
);
});