|
1
|
import { |
|
2
|
Buildings, |
|
3
|
Shopping, |
|
4
|
TriangleRightMini, |
|
5
|
TruckFast, |
|
6
|
} from "@medusajs/icons" |
|
7
|
import { HttpTypes } from "@medusajs/types" |
|
8
|
import { |
|
9
|
Badge, |
|
10
|
Button, |
|
11
|
clx, |
|
12
|
Container, |
|
13
|
Divider, |
|
14
|
Heading, |
|
15
|
IconButton, |
|
16
|
StatusBadge, |
|
17
|
Text, |
|
18
|
Tooltip, |
|
19
|
} from "@medusajs/ui" |
|
20
|
import { Accordion } from "radix-ui" |
|
21
|
import { Link } from "react-router-dom" |
|
22
|
|
|
23
|
import { useShippingOptions } from "../../hooks/api/shipping-options" |
|
24
|
import { getUniqueShippingProfiles } from "../../lib/utils/order-utils" |
|
25
|
import { pluralize } from "../../lib/utils/string-utils" |
|
26
|
import { Thumbnail } from "../common/thumbnail" |
|
27
|
import { useMemo } from "react" |
|
28
|
|
|
29
|
interface ShippingSectionProps { |
|
30
|
order: HttpTypes.AdminOrder |
|
31
|
} |
|
32
|
|
|
33
|
export const ShippingSection = ({ order }: ShippingSectionProps) => { |
|
34
|
const orderHasShipping = order.shipping_methods.length > 0 |
|
35
|
|
|
36
|
const { |
|
37
|
shipping_options = [], |
|
38
|
isPending, |
|
39
|
isError, |
|
40
|
error, |
|
41
|
} = useShippingOptions( |
|
42
|
{ |
|
43
|
id: order.shipping_methods |
|
44
|
.map((method) => method.shipping_option_id) |
|
45
|
.filter(Boolean) as string[], |
|
46
|
fields: |
|
47
|
"+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*,shipping_profile.*", |
|
48
|
}, |
|
49
|
{ |
|
50
|
enabled: orderHasShipping, |
|
51
|
} |
|
52
|
) |
|
53
|
|
|
54
|
if (isError) { |
|
55
|
throw error |
|
56
|
} |
|
57
|
|
|
58
|
const ready = !orderHasShipping ? true : shipping_options && !isPending |
|
59
|
|
|
60
|
const data = useMemo(() => { |
|
61
|
const shippingProfilesData = getShippingProfileData(order.items) |
|
62
|
|
|
63
|
// shipping profiles of the items on the order |
|
64
|
const profileIdMap = new Map<string, boolean>() |
|
65
|
shippingProfilesData.forEach((profile) => { |
|
66
|
profileIdMap.set(profile.id, true) |
|
67
|
}) |
|
68
|
|
|
69
|
// shipping profiles of the shipping methods on the order |
|
70
|
const uniqueProfilesOfShippingMethods = Array.from( |
|
71
|
new Set(shipping_options.map((option) => option.shipping_profile_id)) |
|
72
|
) |
|
73
|
|
|
74
|
// prepare data of shipping profiles that are handed by the order shipping methods but not on needed by the order items |
|
75
|
const additionalShippingProfilesData = uniqueProfilesOfShippingMethods |
|
76
|
.filter((id) => !profileIdMap.has(id)) |
|
77
|
.map((id) => ({ |
|
78
|
id, |
|
79
|
name: |
|
80
|
shipping_options.find((option) => option.shipping_profile_id === id) |
|
81
|
?.shipping_profile?.name || "", |
|
82
|
items: [], |
|
83
|
})) |
|
84
|
|
|
85
|
const shippingProfileDisplayData = [ |
|
86
|
...shippingProfilesData, |
|
87
|
...additionalShippingProfilesData, |
|
88
|
] |
|
89
|
|
|
90
|
return shippingProfileDisplayData.sort((a, b) => |
|
91
|
a.name.localeCompare(b.name) |
|
92
|
) |
|
93
|
}, [order.items, shipping_options]) |
|
94
|
|
|
95
|
const isSomeProfilesAssigned = data.some((profile) => |
|
96
|
shipping_options?.find( |
|
97
|
(option) => option.shipping_profile_id === profile.id |
|
98
|
) |
|
99
|
) |
|
100
|
|
|
101
|
return ( |
|
102
|
<Container className="p-0 overflow-hidden"> |
|
103
|
<div className="px-6 py-4"> |
|
104
|
<Heading>Shipping</Heading> |
|
105
|
</div> |
|
106
|
<Divider variant="dashed" /> |
|
107
|
<Accordion.Root type="multiple"> |
|
108
|
{ready && |
|
109
|
data.map((profile, idx) => ( |
|
110
|
<div key={profile.id}> |
|
111
|
{renderShippingProfile( |
|
112
|
profile, |
|
113
|
shipping_options, |
|
114
|
idx === data.length - 1 |
|
115
|
)} |
|
116
|
</div> |
|
117
|
))} |
|
118
|
</Accordion.Root> |
|
119
|
|
|
120
|
<Divider variant="dashed" /> |
|
121
|
<Footer isSomeProfilesAssigned={isSomeProfilesAssigned} /> |
|
122
|
</Container> |
|
123
|
) |
|
124
|
} |
|
125
|
|
|
126
|
function renderShippingProfile( |
|
127
|
profile: ShippingProfileData, |
|
128
|
shippingOptions: HttpTypes.AdminShippingOption[], |
|
129
|
isLast: boolean |
|
130
|
) { |
|
131
|
const shippingOption = shippingOptions.find( |
|
132
|
(option) => option.shipping_profile_id === profile.id |
|
133
|
) |
|
134
|
|
|
135
|
if (shippingOption) { |
|
136
|
return ( |
|
137
|
<ProfileWithShipping |
|
138
|
profile={profile} |
|
139
|
shippingOption={shippingOption} |
|
140
|
isLast={isLast} |
|
141
|
/> |
|
142
|
) |
|
143
|
} |
|
144
|
|
|
145
|
return <ProfileWithoutShipping profile={profile} isLast={isLast} /> |
|
146
|
} |
|
147
|
|
|
148
|
interface ProfileWithShipping { |
|
149
|
profile: ShippingProfileData |
|
150
|
shippingOption: HttpTypes.AdminShippingOption |
|
151
|
isLast?: boolean |
|
152
|
} |
|
153
|
|
|
154
|
const ProfileWithShipping = ({ |
|
155
|
profile, |
|
156
|
shippingOption, |
|
157
|
isLast, |
|
158
|
}: ProfileWithShipping) => { |
|
159
|
const hasItems = profile.items.length > 0 |
|
160
|
|
|
161
|
return ( |
|
162
|
<div> |
|
163
|
<Accordion.Item value={profile.id}> |
|
164
|
<div className="flex items-center px-6 py-4 justify-between gap-x-3"> |
|
165
|
<div className="flex items-center gap-x-3 max-sm:items-start"> |
|
166
|
<Accordion.Trigger asChild> |
|
167
|
<IconButton |
|
168
|
size="2xsmall" |
|
169
|
variant="transparent" |
|
170
|
className="group/trigger" |
|
171
|
disabled={!hasItems} |
|
172
|
> |
|
173
|
<TriangleRightMini className="group-data-[state=open]/trigger:rotate-90 transition-transform" /> |
|
174
|
</IconButton> |
|
175
|
</Accordion.Trigger> |
|
176
|
<div className="flex items-center gap-[5px] max-sm:flex-col max-sm:items-start flex-1 w-full overflow-hidden"> |
|
177
|
<Tooltip |
|
178
|
content={ |
|
179
|
<ul> |
|
180
|
{profile.items.map((item) => ( |
|
181
|
<li |
|
182
|
key={item.id} |
|
183
|
>{`${item.quantity}x ${item.variant?.product?.title} (${item.variant?.title})`}</li> |
|
184
|
))} |
|
185
|
</ul> |
|
186
|
} |
|
187
|
> |
|
188
|
<Badge |
|
189
|
className="flex items-center gap-x-[3px] overflow-hidden cursor-default" |
|
190
|
size="xsmall" |
|
191
|
> |
|
192
|
<Shopping className="shrink-0" /> |
|
193
|
<span className="truncate"> |
|
194
|
{profile.items.reduce( |
|
195
|
(acc, item) => acc + item.quantity, |
|
196
|
0 |
|
197
|
)} |
|
198
|
x {pluralize(profile.items.length, "items", "item")} |
|
199
|
</span> |
|
200
|
</Badge> |
|
201
|
</Tooltip> |
|
202
|
<Tooltip |
|
203
|
content={ |
|
204
|
shippingOption.service_zone?.fulfillment_set?.location?.name |
|
205
|
} |
|
206
|
> |
|
207
|
<Badge |
|
208
|
className="flex items-center gap-x-[3px] overflow-hidden cursor-default" |
|
209
|
size="xsmall" |
|
210
|
> |
|
211
|
<Buildings className="shrink-0" /> |
|
212
|
<span className="truncate"> |
|
213
|
{ |
|
214
|
shippingOption.service_zone?.fulfillment_set?.location |
|
215
|
?.name |
|
216
|
} |
|
217
|
</span> |
|
218
|
</Badge> |
|
219
|
</Tooltip> |
|
220
|
<Tooltip content={shippingOption.name}> |
|
221
|
<Badge |
|
222
|
className="flex items-center gap-x-[3px] overflow-hidden cursor-default" |
|
223
|
size="xsmall" |
|
224
|
> |
|
225
|
<TruckFast className="shrink-0" /> |
|
226
|
<span className="truncate">{shippingOption.name}</span> |
|
227
|
</Badge> |
|
228
|
</Tooltip> |
|
229
|
</div> |
|
230
|
</div> |
|
231
|
</div> |
|
232
|
<ShippingProfileItems profile={profile} /> |
|
233
|
</Accordion.Item> |
|
234
|
{!isLast && <Divider variant="dashed" />} |
|
235
|
</div> |
|
236
|
) |
|
237
|
} |
|
238
|
|
|
239
|
interface ProfileWithoutShippingProps { |
|
240
|
profile: ShippingProfileData |
|
241
|
isLast?: boolean |
|
242
|
} |
|
243
|
|
|
244
|
const ProfileWithoutShipping = ({ |
|
245
|
profile, |
|
246
|
isLast, |
|
247
|
}: ProfileWithoutShippingProps) => { |
|
248
|
return ( |
|
249
|
<div> |
|
250
|
<Accordion.Item value={profile.id}> |
|
251
|
<div className="flex items-center gap-x-3 justify-between px-6 py-4"> |
|
252
|
<div className="flex items-center gap-x-3"> |
|
253
|
<Accordion.Trigger asChild> |
|
254
|
<IconButton |
|
255
|
size="2xsmall" |
|
256
|
variant="transparent" |
|
257
|
className="group/trigger" |
|
258
|
> |
|
259
|
<TriangleRightMini className="group-data-[state=open]/trigger:rotate-90 transition-transform" /> |
|
260
|
</IconButton> |
|
261
|
</Accordion.Trigger> |
|
262
|
<div className="flex-1 flex items-center gap-x-3"> |
|
263
|
<ShippingBadge /> |
|
264
|
<div> |
|
265
|
<Text size="small" weight="plus" leading="compact"> |
|
266
|
{profile.name} |
|
267
|
</Text> |
|
268
|
<Text |
|
269
|
size="small" |
|
270
|
leading="compact" |
|
271
|
className="text-ui-fg-subtle" |
|
272
|
> |
|
273
|
{`${profile.items.length} ${pluralize( |
|
274
|
profile.items.length, |
|
275
|
"item", |
|
276
|
"items" |
|
277
|
)}`} |
|
278
|
</Text> |
|
279
|
</div> |
|
280
|
</div> |
|
281
|
</div> |
|
282
|
<div> |
|
283
|
<StatusBadge color="orange">Requires shipping</StatusBadge> |
|
284
|
</div> |
|
285
|
</div> |
|
286
|
<ShippingProfileItems profile={profile} /> |
|
287
|
</Accordion.Item> |
|
288
|
{!isLast && <Divider variant="dashed" />} |
|
289
|
</div> |
|
290
|
) |
|
291
|
} |
|
292
|
|
|
293
|
interface ShippingProfileItemsProps { |
|
294
|
profile: ShippingProfileData |
|
295
|
} |
|
296
|
|
|
297
|
const ShippingProfileItems = ({ profile }: ShippingProfileItemsProps) => { |
|
298
|
return ( |
|
299
|
<Accordion.Content> |
|
300
|
<Divider variant="dashed" /> |
|
301
|
{profile.items.map((item, idx) => { |
|
302
|
return ( |
|
303
|
<div key={item.id}> |
|
304
|
<div className="px-6 flex items-center gap-x-3" key={item.id}> |
|
305
|
<div className="w-5 h-[72px] flex flex-col justify-center items-center"> |
|
306
|
<Divider variant="dashed" orientation="vertical" /> |
|
307
|
</div> |
|
308
|
<div className="py-4 flex items-center gap-x-3"> |
|
309
|
<div className="size-7 flex items-center justify-center tabular-nums"> |
|
310
|
<Text |
|
311
|
size="small" |
|
312
|
leading="compact" |
|
313
|
className="text-ui-fg-subtle" |
|
314
|
> |
|
315
|
{item.quantity}x |
|
316
|
</Text> |
|
317
|
</div> |
|
318
|
<Thumbnail thumbnail={item.thumbnail} /> |
|
319
|
<div> |
|
320
|
<Text size="small" leading="compact" weight="plus"> |
|
321
|
{item.variant?.product?.title} ({item.variant?.title}) |
|
322
|
</Text> |
|
323
|
<Text |
|
324
|
size="small" |
|
325
|
leading="compact" |
|
326
|
className="text-ui-fg-subtle" |
|
327
|
> |
|
328
|
{item.variant?.options |
|
329
|
?.map((option) => option.value) |
|
330
|
.join(" · ")} |
|
331
|
</Text> |
|
332
|
</div> |
|
333
|
</div> |
|
334
|
</div> |
|
335
|
{idx !== profile.items.length - 1 && <Divider variant="dashed" />} |
|
336
|
</div> |
|
337
|
) |
|
338
|
})} |
|
339
|
</Accordion.Content> |
|
340
|
) |
|
341
|
} |
|
342
|
|
|
343
|
interface FooterProps { |
|
344
|
isSomeProfilesAssigned: boolean |
|
345
|
} |
|
346
|
|
|
347
|
const Footer = ({ isSomeProfilesAssigned }: FooterProps) => { |
|
348
|
return ( |
|
349
|
<div className="px-6 py-4 flex items-center justify-end bg-ui-bg-component"> |
|
350
|
<Button size="small" variant="secondary" asChild> |
|
351
|
<Link to="shipping"> |
|
352
|
{isSomeProfilesAssigned ? "Edit shipping" : "Add shipping"} |
|
353
|
</Link> |
|
354
|
</Button> |
|
355
|
</div> |
|
356
|
) |
|
357
|
} |
|
358
|
|
|
359
|
interface ShippingBadgeProps { |
|
360
|
className?: string |
|
361
|
} |
|
362
|
|
|
363
|
const ShippingBadge = ({ className }: ShippingBadgeProps) => { |
|
364
|
return ( |
|
365
|
<div |
|
366
|
className={clx( |
|
367
|
"size-7 rounded-md shadow-borders-base flex items-center justify-center", |
|
368
|
className |
|
369
|
)} |
|
370
|
> |
|
371
|
<div className="size-6 rounded bg-ui-bg-component-hover flex items-center justify-center"> |
|
372
|
<Shopping className="text-ui-fg-subtle" /> |
|
373
|
</div> |
|
374
|
</div> |
|
375
|
) |
|
376
|
} |
|
377
|
|
|
378
|
interface ShippingProfileData { |
|
379
|
id: string |
|
380
|
name: string |
|
381
|
items: HttpTypes.AdminOrderLineItem[] |
|
382
|
} |
|
383
|
|
|
384
|
function getShippingProfileData( |
|
385
|
items: HttpTypes.AdminOrderLineItem[] |
|
386
|
): ShippingProfileData[] { |
|
387
|
const uniqueShippingProfiles = getUniqueShippingProfiles(items) |
|
388
|
|
|
389
|
const output = uniqueShippingProfiles.map((profile) => { |
|
390
|
return { |
|
391
|
id: profile.id, |
|
392
|
name: profile.name, |
|
393
|
items: items.filter( |
|
394
|
(item) => item.variant?.product?.shipping_profile?.id === profile.id |
|
395
|
), |
|
396
|
} |
|
397
|
}) |
|
398
|
|
|
399
|
return output |
|
400
|
} |