|
1
|
import { PencilSquare, Trash } from "@medusajs/icons" |
|
2
|
import { HttpTypes } from "@medusajs/types" |
|
3
|
import { Button, Checkbox, Container, Heading, usePrompt } from "@medusajs/ui" |
|
4
|
import { RowSelectionState, createColumnHelper } from "@tanstack/react-table" |
|
5
|
import { useMemo, useState } from "react" |
|
6
|
import { useTranslation } from "react-i18next" |
|
7
|
import { Link } from "react-router-dom" |
|
8
|
|
|
9
|
import { ActionMenu } from "../../../../../components/common/action-menu" |
|
10
|
import { _DataTable } from "../../../../../components/table/data-table" |
|
11
|
import { useRemoveCustomersFromGroup } from "../../../../../hooks/api/customer-groups" |
|
12
|
import { useCustomers } from "../../../../../hooks/api/customers" |
|
13
|
import { useCustomerTableColumns } from "../../../../../hooks/table/columns/use-customer-table-columns" |
|
14
|
import { useCustomerTableFilters } from "../../../../../hooks/table/filters/use-customer-table-filters" |
|
15
|
import { useCustomerTableQuery } from "../../../../../hooks/table/query/use-customer-table-query" |
|
16
|
import { useDataTable } from "../../../../../hooks/use-data-table" |
|
17
|
|
|
18
|
type CustomerGroupCustomerSectionProps = { |
|
19
|
group: HttpTypes.AdminCustomerGroup |
|
20
|
} |
|
21
|
|
|
22
|
const PAGE_SIZE = 10 |
|
23
|
|
|
24
|
export const CustomerGroupCustomerSection = ({ |
|
25
|
group, |
|
26
|
}: CustomerGroupCustomerSectionProps) => { |
|
27
|
const [rowSelection, setRowSelection] = useState<RowSelectionState>({}) |
|
28
|
const { t } = useTranslation() |
|
29
|
const prompt = usePrompt() |
|
30
|
|
|
31
|
const { searchParams, raw } = useCustomerTableQuery({ pageSize: PAGE_SIZE }) |
|
32
|
const { customers, count, isLoading, isError, error } = useCustomers({ |
|
33
|
...searchParams, |
|
34
|
groups: group.id, |
|
35
|
}) |
|
36
|
|
|
37
|
const columns = useColumns() |
|
38
|
const filters = useCustomerTableFilters(["groups"]) |
|
39
|
|
|
40
|
const { table } = useDataTable({ |
|
41
|
data: customers ?? [], |
|
42
|
columns, |
|
43
|
count, |
|
44
|
getRowId: (row) => row.id, |
|
45
|
enablePagination: true, |
|
46
|
enableRowSelection: true, |
|
47
|
pageSize: PAGE_SIZE, |
|
48
|
rowSelection: { |
|
49
|
state: rowSelection, |
|
50
|
updater: setRowSelection, |
|
51
|
}, |
|
52
|
meta: { |
|
53
|
customerGroupId: group.id, |
|
54
|
}, |
|
55
|
}) |
|
56
|
|
|
57
|
if (isError) { |
|
58
|
throw error |
|
59
|
} |
|
60
|
|
|
61
|
const { mutateAsync } = useRemoveCustomersFromGroup(group.id) |
|
62
|
|
|
63
|
const handleRemove = async () => { |
|
64
|
const keys = Object.keys(rowSelection) |
|
65
|
|
|
66
|
const res = await prompt({ |
|
67
|
title: t("customerGroups.customers.remove.title", { |
|
68
|
count: keys.length, |
|
69
|
}), |
|
70
|
description: t("customerGroups.customers.remove.description", { |
|
71
|
count: keys.length, |
|
72
|
}), |
|
73
|
confirmText: t("actions.continue"), |
|
74
|
cancelText: t("actions.cancel"), |
|
75
|
}) |
|
76
|
|
|
77
|
if (!res) { |
|
78
|
return |
|
79
|
} |
|
80
|
|
|
81
|
await mutateAsync(keys, { |
|
82
|
onSuccess: () => { |
|
83
|
setRowSelection({}) |
|
84
|
}, |
|
85
|
}) |
|
86
|
} |
|
87
|
|
|
88
|
return ( |
|
89
|
<Container className="divide-y p-0"> |
|
90
|
<div className="flex items-center justify-between px-6 py-4"> |
|
91
|
<Heading level="h2">{t("customers.domain")}</Heading> |
|
92
|
<Link to={`/customer-groups/${group.id}/add-customers`}> |
|
93
|
<Button variant="secondary" size="small"> |
|
94
|
{t("general.add")} |
|
95
|
</Button> |
|
96
|
</Link> |
|
97
|
</div> |
|
98
|
<_DataTable |
|
99
|
table={table} |
|
100
|
columns={columns} |
|
101
|
pageSize={PAGE_SIZE} |
|
102
|
isLoading={isLoading} |
|
103
|
count={count} |
|
104
|
navigateTo={(row) => `/customers/${row.id}`} |
|
105
|
filters={filters} |
|
106
|
search |
|
107
|
pagination |
|
108
|
orderBy={[ |
|
109
|
{ key: "email", label: t("fields.email") }, |
|
110
|
{ key: "first_name", label: t("fields.firstName") }, |
|
111
|
{ key: "last_name", label: t("fields.lastName") }, |
|
112
|
{ key: "has_account", label: t("customers.hasAccount") }, |
|
113
|
{ key: "created_at", label: t("fields.createdAt") }, |
|
114
|
{ key: "updated_at", label: t("fields.updatedAt") }, |
|
115
|
]} |
|
116
|
queryObject={raw} |
|
117
|
commands={[ |
|
118
|
{ |
|
119
|
action: handleRemove, |
|
120
|
label: t("actions.remove"), |
|
121
|
shortcut: "r", |
|
122
|
}, |
|
123
|
]} |
|
124
|
noRecords={{ |
|
125
|
message: t("customerGroups.customers.list.noRecordsMessage"), |
|
126
|
}} |
|
127
|
/> |
|
128
|
</Container> |
|
129
|
) |
|
130
|
} |
|
131
|
|
|
132
|
export const CustomerActions = ({ |
|
133
|
customer, |
|
134
|
customerGroupId, |
|
135
|
}: { |
|
136
|
customer: HttpTypes.AdminCustomer |
|
137
|
customerGroupId: string |
|
138
|
}) => { |
|
139
|
const { t } = useTranslation() |
|
140
|
const { mutateAsync } = useRemoveCustomersFromGroup(customerGroupId) |
|
141
|
|
|
142
|
const prompt = usePrompt() |
|
143
|
|
|
144
|
const handleRemove = async () => { |
|
145
|
const res = await prompt({ |
|
146
|
title: t("customerGroups.customers.remove.title", { |
|
147
|
count: 1, |
|
148
|
}), |
|
149
|
description: t("customerGroups.customers.remove.description", { |
|
150
|
count: 1, |
|
151
|
}), |
|
152
|
confirmText: t("actions.continue"), |
|
153
|
cancelText: t("actions.cancel"), |
|
154
|
}) |
|
155
|
|
|
156
|
if (!res) { |
|
157
|
return |
|
158
|
} |
|
159
|
|
|
160
|
await mutateAsync([customer.id]) |
|
161
|
} |
|
162
|
|
|
163
|
return ( |
|
164
|
<ActionMenu |
|
165
|
groups={[ |
|
166
|
{ |
|
167
|
actions: [ |
|
168
|
{ |
|
169
|
icon: <PencilSquare />, |
|
170
|
label: t("actions.edit"), |
|
171
|
to: `/customers/${customer.id}/edit`, |
|
172
|
}, |
|
173
|
], |
|
174
|
}, |
|
175
|
{ |
|
176
|
actions: [ |
|
177
|
{ |
|
178
|
icon: <Trash />, |
|
179
|
label: t("actions.remove"), |
|
180
|
onClick: handleRemove, |
|
181
|
}, |
|
182
|
], |
|
183
|
}, |
|
184
|
]} |
|
185
|
/> |
|
186
|
) |
|
187
|
} |
|
188
|
|
|
189
|
const columnHelper = createColumnHelper<HttpTypes.AdminCustomer>() |
|
190
|
|
|
191
|
const useColumns = () => { |
|
192
|
const columns = useCustomerTableColumns() |
|
193
|
|
|
194
|
return useMemo( |
|
195
|
() => [ |
|
196
|
columnHelper.display({ |
|
197
|
id: "select", |
|
198
|
header: ({ table }) => { |
|
199
|
return ( |
|
200
|
<Checkbox |
|
201
|
checked={ |
|
202
|
table.getIsSomePageRowsSelected() |
|
203
|
? "indeterminate" |
|
204
|
: table.getIsAllPageRowsSelected() |
|
205
|
} |
|
206
|
onCheckedChange={(value) => |
|
207
|
table.toggleAllPageRowsSelected(!!value) |
|
208
|
} |
|
209
|
/> |
|
210
|
) |
|
211
|
}, |
|
212
|
cell: ({ row }) => { |
|
213
|
return ( |
|
214
|
<Checkbox |
|
215
|
checked={row.getIsSelected()} |
|
216
|
onCheckedChange={(value) => row.toggleSelected(!!value)} |
|
217
|
onClick={(e) => { |
|
218
|
e.stopPropagation() |
|
219
|
}} |
|
220
|
/> |
|
221
|
) |
|
222
|
}, |
|
223
|
}), |
|
224
|
...columns, |
|
225
|
columnHelper.display({ |
|
226
|
id: "actions", |
|
227
|
cell: ({ row, table }) => { |
|
228
|
const { customerGroupId } = table.options.meta as { |
|
229
|
customerGroupId: string |
|
230
|
} |
|
231
|
|
|
232
|
return ( |
|
233
|
<CustomerActions |
|
234
|
customer={row.original} |
|
235
|
customerGroupId={customerGroupId} |
|
236
|
/> |
|
237
|
) |
|
238
|
}, |
|
239
|
}), |
|
240
|
], |
|
241
|
[columns] |
|
242
|
) |
|
243
|
} |