|
1
|
import { HttpTypes } from "@medusajs/types" |
|
2
|
import { clx, Container, Heading, toast, usePrompt } from "@medusajs/ui" |
|
3
|
import { useTranslation } from "react-i18next" |
|
4
|
|
|
5
|
import { Trash } from "@medusajs/icons" |
|
6
|
import { Link, useNavigate } from "react-router-dom" |
|
7
|
import { ActionMenu } from "../../../../../components/common/action-menu" |
|
8
|
import { NoRecords } from "../../../../../components/common/empty-table-content" |
|
9
|
import { Listicle } from "../../../../../components/common/listicle" |
|
10
|
import { PermissionGuard } from "../../../../../components/common/permission-guard" |
|
11
|
import { useDeleteCustomerAddress } from "../../../../../hooks/api/customers" |
|
12
|
import { useCustomerPermissions } from "../../../../../hooks/use-resource-permissions" |
|
13
|
|
|
14
|
type CustomerAddressSectionProps = { |
|
15
|
customer: HttpTypes.AdminCustomer |
|
16
|
} |
|
17
|
|
|
18
|
export const CustomerAddressSection = ({ |
|
19
|
customer, |
|
20
|
}: CustomerAddressSectionProps) => { |
|
21
|
const { t } = useTranslation() |
|
22
|
const prompt = usePrompt() |
|
23
|
const navigate = useNavigate() |
|
24
|
const { canDelete } = useCustomerPermissions() |
|
25
|
const { mutateAsync: deleteAddress } = useDeleteCustomerAddress(customer.id) |
|
26
|
|
|
27
|
const addresses = customer.addresses ?? [] |
|
28
|
|
|
29
|
const handleDelete = async (address: HttpTypes.AdminCustomerAddress) => { |
|
30
|
const confirm = await prompt({ |
|
31
|
title: t("general.areYouSure"), |
|
32
|
description: t("general.areYouSureDescription", { |
|
33
|
entity: t("fields.address"), |
|
34
|
title: address.address_name ?? "n/a", |
|
35
|
}), |
|
36
|
verificationInstruction: t("general.typeToConfirm"), |
|
37
|
verificationText: address.address_name ?? "address", |
|
38
|
confirmText: t("actions.delete"), |
|
39
|
cancelText: t("actions.cancel"), |
|
40
|
}) |
|
41
|
|
|
42
|
if (!confirm) { |
|
43
|
return |
|
44
|
} |
|
45
|
|
|
46
|
await deleteAddress(address.id, { |
|
47
|
onSuccess: () => { |
|
48
|
toast.success( |
|
49
|
t("general.success", { name: address.address_name ?? "address" }) |
|
50
|
) |
|
51
|
|
|
52
|
navigate(`/customers/${customer.id}`, { replace: true }) |
|
53
|
}, |
|
54
|
onError: (e) => { |
|
55
|
toast.error(e.message) |
|
56
|
}, |
|
57
|
}) |
|
58
|
} |
|
59
|
|
|
60
|
return ( |
|
61
|
<Container className="p-0"> |
|
62
|
<div className="flex items-center justify-between px-6 py-4"> |
|
63
|
<Heading level="h2">{t("addresses.title")}</Heading> |
|
64
|
{/* Only show add link if user has update permission */} |
|
65
|
<PermissionGuard resource="customer" operation="update"> |
|
66
|
<Link to={`create-address`} className="text-ui-fg-muted text-xs"> |
|
67
|
Add |
|
68
|
</Link> |
|
69
|
</PermissionGuard> |
|
70
|
</div> |
|
71
|
|
|
72
|
{addresses.length === 0 && ( |
|
73
|
<NoRecords |
|
74
|
className={clx({ |
|
75
|
"flex h-full flex-col overflow-hidden border-t p-6": true, |
|
76
|
})} |
|
77
|
icon={null} |
|
78
|
title={t("general.noRecordsTitle")} |
|
79
|
message={t("general.noRecordsMessage")} |
|
80
|
/> |
|
81
|
)} |
|
82
|
|
|
83
|
{addresses.map((address) => { |
|
84
|
return ( |
|
85
|
<Listicle |
|
86
|
key={address.id} |
|
87
|
labelKey={address.address_name ?? "n/a"} |
|
88
|
descriptionKey={[address.address_1, address.address_2].join(" ")} |
|
89
|
> |
|
90
|
{/* Only show delete action if user has delete permission */} |
|
91
|
{canDelete && ( |
|
92
|
<ActionMenu |
|
93
|
groups={[ |
|
94
|
{ |
|
95
|
actions: [ |
|
96
|
{ |
|
97
|
icon: <Trash />, |
|
98
|
label: t("actions.delete"), |
|
99
|
onClick: async () => { |
|
100
|
await handleDelete(address) |
|
101
|
}, |
|
102
|
}, |
|
103
|
], |
|
104
|
}, |
|
105
|
]} |
|
106
|
/> |
|
107
|
)} |
|
108
|
</Listicle> |
|
109
|
) |
|
110
|
})} |
|
111
|
</Container> |
|
112
|
) |
|
113
|
} |