|
1
|
/* eslint-disable @medusajs/widget-must-have-default-export */ |
|
2
|
/* eslint-disable @medusajs/widget-must-export-config */ |
|
3
|
import { PencilSquare, Trash } from "@medusajs/icons"; |
|
4
|
import { AdminSalesChannel, HttpTypes } from "@medusajs/types"; |
|
5
|
import { Checkbox, Container, toast, usePrompt } from "@medusajs/ui"; |
|
6
|
import { RowSelectionState, createColumnHelper } from "@tanstack/react-table"; |
|
7
|
import { useMemo, useState } from "react"; |
|
8
|
|
|
9
|
import { ActionMenu } from "../../components/action-menu"; |
|
10
|
import { DataTable } from "../../components/data-table"; |
|
11
|
import { useProducts } from "../../hooks/api/products"; |
|
12
|
import { useSalesChannelRemoveProducts } from "../../hooks/api/sales-channels"; |
|
13
|
import { useGeneralEmptyState } from "../../hooks/common/use-general-empty-state"; |
|
14
|
import { useGiftCardProductsFilters } from "../../hooks/query/use-gift-card-products-filters"; |
|
15
|
import { useProductTableColumns } from "./use-gift-card-products-table-columns"; |
|
16
|
import { useGiftCardProductsTableQuery } from "./use-gift-card-products-table-query"; |
|
17
|
|
|
18
|
const PAGE_SIZE = 10; |
|
19
|
|
|
20
|
type SalesChannelGiftCardsSectionProps = { |
|
21
|
salesChannel: AdminSalesChannel; |
|
22
|
}; |
|
23
|
|
|
24
|
export const SalesChannelGiftCardsSection = ({ |
|
25
|
salesChannel, |
|
26
|
}: SalesChannelGiftCardsSectionProps) => { |
|
27
|
const [rowSelection, setRowSelection] = useState<RowSelectionState>({}); |
|
28
|
const { searchParams, raw } = useGiftCardProductsTableQuery({ |
|
29
|
pageSize: PAGE_SIZE, |
|
30
|
}); |
|
31
|
|
|
32
|
const { |
|
33
|
products, |
|
34
|
count, |
|
35
|
isPending: isLoading, |
|
36
|
isError, |
|
37
|
error, |
|
38
|
} = useProducts({ |
|
39
|
...searchParams, |
|
40
|
sales_channel_id: [salesChannel.id], |
|
41
|
is_giftcard: true, |
|
42
|
}); |
|
43
|
const columns = useColumns({ salesChannel }); |
|
44
|
const filters = useGiftCardProductsFilters(); |
|
45
|
const { mutateAsync } = useSalesChannelRemoveProducts(salesChannel.id); |
|
46
|
const prompt = usePrompt(); |
|
47
|
|
|
48
|
const handleRemove = async () => { |
|
49
|
const ids = Object.keys(rowSelection); |
|
50
|
|
|
51
|
const result = await prompt({ |
|
52
|
title: "Are you sure?", |
|
53
|
description: `Are you sure you want to remove ${ids.length} gift card products from ${salesChannel.name}?`, |
|
54
|
confirmText: "Remove", |
|
55
|
cancelText: "Cancel", |
|
56
|
}); |
|
57
|
|
|
58
|
if (!result) { |
|
59
|
return; |
|
60
|
} |
|
61
|
|
|
62
|
await mutateAsync(ids, { |
|
63
|
onSuccess: () => { |
|
64
|
toast.success("Gift card product(s) removed"); |
|
65
|
setRowSelection({}); |
|
66
|
}, |
|
67
|
onError: (error: { message: string }) => { |
|
68
|
toast.error(error.message); |
|
69
|
}, |
|
70
|
}); |
|
71
|
}; |
|
72
|
|
|
73
|
if (isError) { |
|
74
|
throw error; |
|
75
|
} |
|
76
|
|
|
77
|
const emptyState = useGeneralEmptyState(); |
|
78
|
|
|
79
|
return ( |
|
80
|
<Container className="divide-y p-0"> |
|
81
|
<DataTable |
|
82
|
heading="Gift Card Products" |
|
83
|
data={products ?? []} |
|
84
|
columns={columns} |
|
85
|
pageSize={PAGE_SIZE} |
|
86
|
commands={[ |
|
87
|
{ |
|
88
|
action: handleRemove, |
|
89
|
label: "Remove", |
|
90
|
shortcut: "r", |
|
91
|
}, |
|
92
|
]} |
|
93
|
rowCount={count} |
|
94
|
pagination |
|
95
|
search |
|
96
|
rowHref={(row: any) => |
|
97
|
`/gift-cards/gift-card-products/${row.original.id}` |
|
98
|
} |
|
99
|
filters={filters} |
|
100
|
navigateTo={(row: HttpTypes.AdminProduct) => `/products/${row.id}`} |
|
101
|
isLoading={isLoading} |
|
102
|
orderBy={[ |
|
103
|
{ key: "title", label: "Title" }, |
|
104
|
{ key: "status", label: "Status" }, |
|
105
|
{ key: "created_at", label: "Created At" }, |
|
106
|
{ key: "updated_at", label: "Updated At" }, |
|
107
|
]} |
|
108
|
queryObject={raw} |
|
109
|
emptyState={emptyState} |
|
110
|
actions={[ |
|
111
|
{ |
|
112
|
label: "Add", |
|
113
|
to: `/gift-cards/gift-card-products/${salesChannel.id}/add-gift-card-products`, |
|
114
|
}, |
|
115
|
]} |
|
116
|
/> |
|
117
|
</Container> |
|
118
|
); |
|
119
|
}; |
|
120
|
|
|
121
|
const columnHelper = createColumnHelper<HttpTypes.AdminProduct>(); |
|
122
|
|
|
123
|
const useColumns = ({ salesChannel }: { salesChannel: AdminSalesChannel }) => { |
|
124
|
const base = useProductTableColumns(); |
|
125
|
|
|
126
|
return useMemo( |
|
127
|
() => [ |
|
128
|
columnHelper.display({ |
|
129
|
id: "select", |
|
130
|
header: ({ table }) => { |
|
131
|
return ( |
|
132
|
<Checkbox |
|
133
|
checked={ |
|
134
|
table.getIsSomePageRowsSelected() |
|
135
|
? "indeterminate" |
|
136
|
: table.getIsAllPageRowsSelected() |
|
137
|
} |
|
138
|
onCheckedChange={(value) => |
|
139
|
table.getRowModel().rows.forEach((row) => { |
|
140
|
row.toggleSelected(!!value); |
|
141
|
}) |
|
142
|
} |
|
143
|
/> |
|
144
|
); |
|
145
|
}, |
|
146
|
cell: ({ row }) => { |
|
147
|
return ( |
|
148
|
<Checkbox |
|
149
|
checked={row.getIsSelected()} |
|
150
|
onCheckedChange={(value) => row.toggleSelected(!!value)} |
|
151
|
onClick={(e) => { |
|
152
|
e.stopPropagation(); |
|
153
|
}} |
|
154
|
/> |
|
155
|
); |
|
156
|
}, |
|
157
|
}), |
|
158
|
...base, |
|
159
|
columnHelper.display({ |
|
160
|
id: "actions", |
|
161
|
cell: ({ row }) => { |
|
162
|
return ( |
|
163
|
<div className="flex items-center justify-end"> |
|
164
|
<ProductListCellActions |
|
165
|
productId={row.original.id} |
|
166
|
salesChannelId={salesChannel.id} |
|
167
|
/> |
|
168
|
</div> |
|
169
|
); |
|
170
|
}, |
|
171
|
}), |
|
172
|
], |
|
173
|
[base] |
|
174
|
); |
|
175
|
}; |
|
176
|
|
|
177
|
const ProductListCellActions = ({ |
|
178
|
salesChannelId, |
|
179
|
productId, |
|
180
|
}: { |
|
181
|
productId: string; |
|
182
|
salesChannelId: string; |
|
183
|
}) => { |
|
184
|
const { mutateAsync } = useSalesChannelRemoveProducts(salesChannelId); |
|
185
|
|
|
186
|
const onRemove = async () => { |
|
187
|
await mutateAsync([productId], { |
|
188
|
onSuccess: () => { |
|
189
|
toast.success("Gift card product(s) removed"); |
|
190
|
}, |
|
191
|
onError: (e: { message: string }) => { |
|
192
|
toast.error(e.message); |
|
193
|
}, |
|
194
|
}); |
|
195
|
}; |
|
196
|
|
|
197
|
return ( |
|
198
|
<ActionMenu |
|
199
|
groups={[ |
|
200
|
{ |
|
201
|
actions: [ |
|
202
|
{ |
|
203
|
icon: <PencilSquare />, |
|
204
|
label: "Edit", |
|
205
|
to: `/gift-cards/gift-card-products/${productId}`, |
|
206
|
}, |
|
207
|
], |
|
208
|
}, |
|
209
|
{ |
|
210
|
actions: [ |
|
211
|
{ |
|
212
|
icon: <Trash />, |
|
213
|
label: "Remove", |
|
214
|
onClick: onRemove, |
|
215
|
}, |
|
216
|
], |
|
217
|
}, |
|
218
|
]} |
|
219
|
/> |
|
220
|
); |
|
221
|
}; |