|
1
|
import { Link, PencilSquare, Trash } from "@medusajs/icons" |
|
2
|
import { HttpTypes } from "@medusajs/types" |
|
3
|
import { Container, Heading, Text, toast, usePrompt } from "@medusajs/ui" |
|
4
|
import copy from "copy-to-clipboard" |
|
5
|
import { useTranslation } from "react-i18next" |
|
6
|
import { useNavigate } from "react-router-dom" |
|
7
|
import { ActionMenu } from "../../../../../components/common/action-menu" |
|
8
|
import { |
|
9
|
useDeleteUser, |
|
10
|
useGenerateUserResetPasswordToken, |
|
11
|
useUserAuthProviders, |
|
12
|
} from "../../../../../hooks/api/users" |
|
13
|
|
|
14
|
type UserGeneralSectionProps = { |
|
15
|
user: HttpTypes.AdminUser |
|
16
|
} |
|
17
|
|
|
18
|
const RESET_PASSWORD_URL = `${window.location.origin}${ |
|
19
|
__BASE__ === "/" ? "" : __BASE__ |
|
20
|
}/reset-password?token=` |
|
21
|
|
|
22
|
export const UserGeneralSection = ({ user }: UserGeneralSectionProps) => { |
|
23
|
const { t } = useTranslation() |
|
24
|
const navigate = useNavigate() |
|
25
|
const prompt = usePrompt() |
|
26
|
|
|
27
|
const { mutateAsync } = useDeleteUser(user.id) |
|
28
|
const { mutate: generateResetPasswordToken } = |
|
29
|
useGenerateUserResetPasswordToken(user.id) |
|
30
|
const { providers } = useUserAuthProviders(user.id) |
|
31
|
|
|
32
|
// emailpass is the only provider the reset password flow the dashboard handles. |
|
33
|
const canResetPassword = !!providers?.includes("emailpass") |
|
34
|
|
|
35
|
const name = [user.first_name, user.last_name].filter(Boolean).join(" ") |
|
36
|
|
|
37
|
const handleDeleteUser = async () => { |
|
38
|
const res = await prompt({ |
|
39
|
title: t("general.areYouSure"), |
|
40
|
description: t("users.deleteUserWarning", { |
|
41
|
name: name ?? user.email, |
|
42
|
}), |
|
43
|
verificationText: name ?? user.email, |
|
44
|
verificationInstruction: t("general.typeToConfirm"), |
|
45
|
confirmText: t("actions.delete"), |
|
46
|
cancelText: t("actions.cancel"), |
|
47
|
}) |
|
48
|
|
|
49
|
if (!res) { |
|
50
|
return |
|
51
|
} |
|
52
|
|
|
53
|
await mutateAsync(undefined, { |
|
54
|
onSuccess: () => { |
|
55
|
toast.success(t("users.deleteUserSuccess", { name: user.email })) |
|
56
|
navigate("..") |
|
57
|
}, |
|
58
|
onError: (error) => { |
|
59
|
toast.error(error.message) |
|
60
|
}, |
|
61
|
}) |
|
62
|
} |
|
63
|
|
|
64
|
const handleCopyResetPasswordLink = () => { |
|
65
|
generateResetPasswordToken(undefined, { |
|
66
|
onSuccess: ({ token }) => { |
|
67
|
copy(`${RESET_PASSWORD_URL}${token}`) |
|
68
|
|
|
69
|
toast.success(t("users.copyResetPasswordLinkSuccess"), { |
|
70
|
description: t("users.copyResetPasswordLinkHint"), |
|
71
|
}) |
|
72
|
}, |
|
73
|
onError: (error) => { |
|
74
|
toast.error(error.message) |
|
75
|
}, |
|
76
|
}) |
|
77
|
} |
|
78
|
|
|
79
|
return ( |
|
80
|
<Container className="divide-y p-0"> |
|
81
|
<div className="flex items-center justify-between px-6 py-4"> |
|
82
|
<Heading>{user.email}</Heading> |
|
83
|
<ActionMenu |
|
84
|
groups={[ |
|
85
|
{ |
|
86
|
actions: [ |
|
87
|
{ |
|
88
|
label: t("actions.edit"), |
|
89
|
to: "edit", |
|
90
|
icon: <PencilSquare />, |
|
91
|
}, |
|
92
|
], |
|
93
|
}, |
|
94
|
...(canResetPassword |
|
95
|
? [ |
|
96
|
{ |
|
97
|
actions: [ |
|
98
|
{ |
|
99
|
label: t("users.copyResetPasswordLink"), |
|
100
|
onClick: handleCopyResetPasswordLink, |
|
101
|
icon: <Link />, |
|
102
|
}, |
|
103
|
], |
|
104
|
}, |
|
105
|
] |
|
106
|
: []), |
|
107
|
{ |
|
108
|
actions: [ |
|
109
|
{ |
|
110
|
label: t("actions.delete"), |
|
111
|
onClick: handleDeleteUser, |
|
112
|
icon: <Trash />, |
|
113
|
}, |
|
114
|
], |
|
115
|
}, |
|
116
|
]} |
|
117
|
/> |
|
118
|
</div> |
|
119
|
<div className="text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4"> |
|
120
|
<Text size="small" leading="compact" weight="plus"> |
|
121
|
{t("fields.name")} |
|
122
|
</Text> |
|
123
|
<Text size="small" leading="compact"> |
|
124
|
{name ?? "-"} |
|
125
|
</Text> |
|
126
|
</div> |
|
127
|
</Container> |
|
128
|
) |
|
129
|
} |