|
1
|
import { Plus, ReceiptPercent } from "@medusajs/icons" |
|
2
|
import { HttpTypes } from "@medusajs/types" |
|
3
|
import { |
|
4
|
Button, |
|
5
|
clx, |
|
6
|
Container, |
|
7
|
Divider, |
|
8
|
Heading, |
|
9
|
Text, |
|
10
|
toast, |
|
11
|
usePrompt, |
|
12
|
} from "@medusajs/ui" |
|
13
|
import { Link, useNavigate } from "react-router-dom" |
|
14
|
import { useConvertDraftOrder } from "../../hooks/api/draft-orders" |
|
15
|
import { getLocaleAmount, getStylizedAmount } from "../../lib/data/currencies" |
|
16
|
import { ActionMenu } from "../common/action-menu" |
|
17
|
import { Thumbnail } from "../common/thumbnail" |
|
18
|
|
|
19
|
interface SummarySectionProps { |
|
20
|
order: HttpTypes.AdminOrder & { |
|
21
|
promotions: HttpTypes.AdminPromotion[] |
|
22
|
} |
|
23
|
} |
|
24
|
|
|
25
|
export const SummarySection = ({ order }: SummarySectionProps) => { |
|
26
|
const promotions: HttpTypes.AdminPromotion[] | null = order.promotions || [] |
|
27
|
|
|
28
|
return ( |
|
29
|
<Container className="p-0 overflow-hidden"> |
|
30
|
<div className="px-6 py-4 flex items-center justify-between gap-x-4"> |
|
31
|
<Heading>Summary</Heading> |
|
32
|
<ActionMenu |
|
33
|
groups={[ |
|
34
|
{ |
|
35
|
actions: [ |
|
36
|
{ |
|
37
|
label: "Edit items", |
|
38
|
icon: <Plus />, |
|
39
|
to: "items", |
|
40
|
}, |
|
41
|
], |
|
42
|
}, |
|
43
|
{ |
|
44
|
actions: [ |
|
45
|
{ |
|
46
|
label: "Edit promotions", |
|
47
|
icon: <ReceiptPercent />, |
|
48
|
to: "promotions", |
|
49
|
}, |
|
50
|
], |
|
51
|
}, |
|
52
|
]} |
|
53
|
/> |
|
54
|
</div> |
|
55
|
<Divider variant="dashed" /> |
|
56
|
<div> |
|
57
|
{order.items |
|
58
|
.sort((a, b) => { |
|
59
|
// sort the items so items with no variant id go last |
|
60
|
if (a.variant_id && !b.variant_id) { |
|
61
|
return -1 |
|
62
|
} |
|
63
|
|
|
64
|
return 1 |
|
65
|
}) |
|
66
|
.map((item, idx) => ( |
|
67
|
<div key={item.id}> |
|
68
|
<Item item={item} currencyCode={order.currency_code} /> |
|
69
|
{idx !== order.items.length - 1 && <Divider variant="dashed" />} |
|
70
|
</div> |
|
71
|
))} |
|
72
|
</div> |
|
73
|
<Divider variant="dashed" /> |
|
74
|
<Total |
|
75
|
currencyCode={order.currency_code} |
|
76
|
total={order.total} |
|
77
|
shippingSubtotal={order.shipping_subtotal} |
|
78
|
discountTotal={order.discount_total} |
|
79
|
promotions={promotions} |
|
80
|
taxTotal={order.tax_total} |
|
81
|
itemSubTotal={order.item_subtotal} |
|
82
|
itemCount={ |
|
83
|
order.items?.reduce((acc, item) => acc + item.quantity, 0) || 0 |
|
84
|
} |
|
85
|
/> |
|
86
|
<Divider variant="dashed" /> |
|
87
|
<Footer order={order} /> |
|
88
|
</Container> |
|
89
|
) |
|
90
|
} |
|
91
|
|
|
92
|
interface ItemProps { |
|
93
|
item: HttpTypes.AdminOrderLineItem |
|
94
|
currencyCode: string |
|
95
|
} |
|
96
|
|
|
97
|
const Item = ({ item, currencyCode }: ItemProps) => { |
|
98
|
return ( |
|
99
|
<div className="px-6 py-4 grid grid-cols-2 gap-3"> |
|
100
|
<div className="flex items-center gap-x-3"> |
|
101
|
{/* Only display a thumbnail for non-custom items */} |
|
102
|
{item.variant_id && ( |
|
103
|
<Thumbnail thumbnail={item.thumbnail} alt={item.title} /> |
|
104
|
)} |
|
105
|
<div> |
|
106
|
<div className="flex items-center gap-x-1"> |
|
107
|
<Text size="small" weight="plus" leading="compact"> |
|
108
|
{item.product_title || item.title} |
|
109
|
</Text> |
|
110
|
{item.variant_title && ( |
|
111
|
<Text |
|
112
|
size="small" |
|
113
|
leading="compact" |
|
114
|
className="text-ui-fg-subtle" |
|
115
|
> |
|
116
|
({item.variant_title}) |
|
117
|
</Text> |
|
118
|
)} |
|
119
|
</div> |
|
120
|
<Text size="small" leading="compact" className="text-ui-fg-subtle"> |
|
121
|
{item.variant_sku} |
|
122
|
</Text> |
|
123
|
</div> |
|
124
|
</div> |
|
125
|
<div className="grid grid-cols-3 items-center gap-3 [&>div]:text-right text-ui-fg-subtle"> |
|
126
|
<div> |
|
127
|
<Text>{getLocaleAmount(item.unit_price, currencyCode)}</Text> |
|
128
|
</div> |
|
129
|
<div> |
|
130
|
<Text>{item.quantity}x</Text> |
|
131
|
</div> |
|
132
|
<div> |
|
133
|
<Text>{getLocaleAmount(item.subtotal, currencyCode)}</Text> |
|
134
|
</div> |
|
135
|
</div> |
|
136
|
</div> |
|
137
|
) |
|
138
|
} |
|
139
|
|
|
140
|
interface TotalProps { |
|
141
|
total: number |
|
142
|
shippingSubtotal: number | null |
|
143
|
discountTotal: number | null |
|
144
|
promotions: HttpTypes.AdminPromotion[] |
|
145
|
taxTotal: number | null |
|
146
|
currencyCode: string |
|
147
|
itemSubTotal: number |
|
148
|
itemCount: number |
|
149
|
} |
|
150
|
|
|
151
|
const Total = ({ |
|
152
|
total, |
|
153
|
discountTotal, |
|
154
|
shippingSubtotal, |
|
155
|
taxTotal, |
|
156
|
currencyCode, |
|
157
|
promotions, |
|
158
|
itemSubTotal, |
|
159
|
itemCount, |
|
160
|
}: TotalProps) => { |
|
161
|
return ( |
|
162
|
<div className="flex flex-col px-6 py-4 gap-y-2"> |
|
163
|
{itemCount > 0 && ( |
|
164
|
<div className="grid grid-cols-3 items-center justify-between gap-x-4 text-ui-fg-subtle"> |
|
165
|
<Text size="small" leading="compact"> |
|
166
|
Subtotal |
|
167
|
</Text> |
|
168
|
<div className="flex items-center justify-end"> |
|
169
|
<Text size="small" leading="compact"> |
|
170
|
{`${itemCount} ${itemCount === 1 ? "item" : "items"}`} |
|
171
|
</Text> |
|
172
|
</div> |
|
173
|
<div className="flex items-center justify-end"> |
|
174
|
<Text size="small" leading="compact"> |
|
175
|
{getLocaleAmount(itemSubTotal, currencyCode)} |
|
176
|
</Text> |
|
177
|
</div> |
|
178
|
</div> |
|
179
|
)} |
|
180
|
{shippingSubtotal !== null && ( |
|
181
|
<div className="flex items-center justify-between gap-x-4 text-ui-fg-subtle"> |
|
182
|
<Text size="small" leading="compact"> |
|
183
|
Shipping |
|
184
|
</Text> |
|
185
|
<Text size="small" leading="compact"> |
|
186
|
{getLocaleAmount(shippingSubtotal, currencyCode)} |
|
187
|
</Text> |
|
188
|
</div> |
|
189
|
)} |
|
190
|
{discountTotal !== null && ( |
|
191
|
<div |
|
192
|
className={clx( |
|
193
|
"grid grid-cols-2 items-center gap-x-4 text-ui-fg-subtle", |
|
194
|
{ |
|
195
|
"grid-cols-3": !!promotions, |
|
196
|
} |
|
197
|
)} |
|
198
|
> |
|
199
|
<Text size="small" leading="compact"> |
|
200
|
Discount |
|
201
|
</Text> |
|
202
|
<div className="flex items-center justify-end gap-x-2"> |
|
203
|
{promotions.map((promotion) => ( |
|
204
|
<Link to={`/promotions/${promotion.id}`} key={promotion.id}> |
|
205
|
<Text size="small" leading="compact"> |
|
206
|
{promotion.code} |
|
207
|
</Text> |
|
208
|
</Link> |
|
209
|
))} |
|
210
|
</div> |
|
211
|
<div className="flex items-center justify-end"> |
|
212
|
<Text size="small" leading="compact"> |
|
213
|
{getLocaleAmount(discountTotal, currencyCode)} |
|
214
|
</Text> |
|
215
|
</div> |
|
216
|
</div> |
|
217
|
)} |
|
218
|
{taxTotal !== null && ( |
|
219
|
<div className="flex items-center justify-between gap-x-4 text-ui-fg-subtle"> |
|
220
|
<Text size="small" leading="compact"> |
|
221
|
Tax |
|
222
|
</Text> |
|
223
|
<Text size="small" leading="compact"> |
|
224
|
{taxTotal > 0 ? getLocaleAmount(taxTotal, currencyCode) : "-"} |
|
225
|
</Text> |
|
226
|
</div> |
|
227
|
)} |
|
228
|
<div className="flex items-center justify-between gap-x-4"> |
|
229
|
<Text size="small" leading="compact" weight="plus"> |
|
230
|
Total |
|
231
|
</Text> |
|
232
|
<Text |
|
233
|
size="small" |
|
234
|
leading="compact" |
|
235
|
weight="plus" |
|
236
|
className="text-right" |
|
237
|
> |
|
238
|
{getStylizedAmount(total, currencyCode)} |
|
239
|
</Text> |
|
240
|
</div> |
|
241
|
</div> |
|
242
|
) |
|
243
|
} |
|
244
|
|
|
245
|
const Footer = ({ order }: { order: HttpTypes.AdminOrder }) => { |
|
246
|
const navigate = useNavigate() |
|
247
|
const prompt = usePrompt() |
|
248
|
|
|
249
|
const { mutateAsync: convertDraftOrder, isPending } = useConvertDraftOrder( |
|
250
|
order.id |
|
251
|
) |
|
252
|
|
|
253
|
const handleConvert = async () => { |
|
254
|
const res = await prompt({ |
|
255
|
title: "Are you sure?", |
|
256
|
description: |
|
257
|
"You are about to convert this draft order to an order. This action cannot be undone.", |
|
258
|
variant: "confirmation", |
|
259
|
}) |
|
260
|
|
|
261
|
if (!res) { |
|
262
|
return |
|
263
|
} |
|
264
|
|
|
265
|
await convertDraftOrder(undefined, { |
|
266
|
onSuccess: () => { |
|
267
|
toast.success("Draft order converted to order") |
|
268
|
navigate(`/orders/${order.id}`) |
|
269
|
}, |
|
270
|
onError: (error) => { |
|
271
|
toast.error(error.message) |
|
272
|
}, |
|
273
|
}) |
|
274
|
} |
|
275
|
|
|
276
|
return ( |
|
277
|
<div className="px-6 py-4 flex items-center justify-end gap-x-2 bg-ui-bg-component"> |
|
278
|
<Button |
|
279
|
size="small" |
|
280
|
variant="secondary" |
|
281
|
isLoading={isPending} |
|
282
|
onClick={handleConvert} |
|
283
|
> |
|
284
|
Convert to order |
|
285
|
</Button> |
|
286
|
</div> |
|
287
|
) |
|
288
|
} |