1 import { PencilSquare, Trash } from "@medusajs/icons"
2 import { HttpTypes } from "@medusajs/types"
3 import {
4 Container,
5 Heading,
6 StatusBadge,
7 Text,
8 toast,
9 usePrompt,
10 } from "@medusajs/ui"
11 import { useTranslation } from "react-i18next"
12 import { useNavigate } from "react-router-dom"
13
14 import {
15 ActionGroup,
16 ActionMenu,
17 } from "../../../../../components/common/action-menu"
18 import { useDeleteCustomer } from "../../../../../hooks/api/customers"
19 import { useCustomerPermissions } from "../../../../../hooks/use-resource-permissions"
20
21 type CustomerGeneralSectionProps = {
22 customer: HttpTypes.AdminCustomer
23 }
24
25 export const CustomerGeneralSection = ({
26 customer,
27 }: CustomerGeneralSectionProps) => {
28 const { t } = useTranslation()
29 const prompt = usePrompt()
30 const navigate = useNavigate()
31 const { canUpdate, canDelete } = useCustomerPermissions()
32
33 const { mutateAsync } = useDeleteCustomer(customer.id)
34
35 const name = [customer.first_name, customer.last_name]
36 .filter(Boolean)
37 .join(" ")
38
39 const statusColor = customer.has_account ? "green" : "orange"
40 const statusText = customer.has_account
41 ? t("customers.fields.registered")
42 : t("customers.fields.guest")
43
44 const handleDelete = async () => {
45 const res = await prompt({
46 title: t("customers.delete.title"),
47 description: t("customers.delete.description", {
48 email: customer.email,
49 }),
50 verificationInstruction: t("general.typeToConfirm"),
51 verificationText: customer.email,
52 confirmText: t("actions.delete"),
53 cancelText: t("actions.cancel"),
54 })
55
56 if (!res) {
57 return
58 }
59
60 await mutateAsync(undefined, {
61 onSuccess: () => {
62 toast.success(
63 t("customers.delete.successToast", {
64 email: customer.email,
65 })
66 )
67
68 navigate("/customers", { replace: true })
69 },
70 onError: (error) => {
71 toast.error(error.message)
72 },
73 })
74 }
75
76 const groups: ActionGroup[] = []
77
78 if (canUpdate) {
79 groups.push({
80 actions: [
81 {
82 label: t("actions.edit"),
83 icon: <PencilSquare />,
84 to: "edit",
85 },
86 ],
87 })
88 }
89
90 if (canDelete) {
91 groups.push({
92 actions: [
93 {
94 label: t("actions.delete"),
95 icon: <Trash />,
96 onClick: handleDelete,
97 },
98 ],
99 })
100 }
101
102 return (
103 <Container className="divide-y p-0">
104 <div className="flex items-center justify-between px-6 py-4">
105 <Heading>{customer.email}</Heading>
106 <div className="flex items-center gap-x-2">
107 <StatusBadge color={statusColor}>{statusText}</StatusBadge>
108 {groups.length > 0 && <ActionMenu groups={groups} />}
109 </div>
110 </div>
111 <div className="text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4">
112 <Text size="small" leading="compact" weight="plus">
113 {t("fields.name")}
114 </Text>
115 <Text size="small" leading="compact">
116 {name || "-"}
117 </Text>
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.company")}
122 </Text>
123 <Text size="small" leading="compact">
124 {customer.company_name || "-"}
125 </Text>
126 </div>
127 <div className="text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4">
128 <Text size="small" leading="compact" weight="plus">
129 {t("fields.phone")}
130 </Text>
131 <Text size="small" leading="compact">
132 {customer.phone || "-"}
133 </Text>
134 </div>
135 </Container>
136 )
137 }