forked from medusajs/medusa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitem-table.tsx
More file actions
257 lines (232 loc) · 7.03 KB
/
Copy pathitem-table.tsx
File metadata and controls
257 lines (232 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import React, { useMemo } from "react"
import { useTranslation } from "react-i18next"
import FeatureToggle from "../../../../components/fundamentals/feature-toggle"
import ImagePlaceholder from "../../../../components/fundamentals/image-placeholder"
import InputField from "../../../../components/molecules/input"
import { LineItem } from "@medusajs/medusa"
import clsx from "clsx"
import { useAdminVariantsInventory, useAdminReservations } from "medusa-react"
import { useFeatureFlag } from "../../../../providers/feature-flag-provider"
export const getFulfillableQuantity = (item: LineItem): number => {
return item.quantity - (item.fulfilled_quantity || 0)
}
const CreateFulfillmentItemsTable = ({
items,
quantities,
setQuantities,
locationId,
setErrors,
}: {
items: LineItem[]
quantities: Record<string, number>
setQuantities: (quantities: Record<string, number>) => void
locationId?: string
setErrors: (errors: React.SetStateAction<{}>) => void
}) => {
const handleQuantityUpdate = React.useCallback(
(value: number, id: string) => {
let newQuantities = { ...quantities }
newQuantities = {
...newQuantities,
[id]: value,
}
setQuantities(newQuantities)
},
[quantities, setQuantities]
)
return (
<div>
{items.map((item, idx) => {
return (
<FulfillmentLine
item={item}
locationId={locationId}
key={`fulfillmentLine-${idx}`}
quantities={quantities}
handleQuantityUpdate={handleQuantityUpdate}
setErrors={setErrors}
/>
)
})}
</div>
)
}
const FulfillmentLine = ({
item,
locationId,
quantities,
handleQuantityUpdate,
setErrors,
}: {
locationId?: string
item: LineItem
quantities: Record<string, number>
handleQuantityUpdate: (value: number, id: string) => void
setErrors: (errors: Record<string, string>) => void
}) => {
const { t } = useTranslation()
const { isFeatureEnabled } = useFeatureFlag()
const isLocationFulfillmentEnabled =
isFeatureEnabled("inventoryService") &&
isFeatureEnabled("stockLocationService")
const {
variant,
isLoading: isInventoryLoading,
refetch,
} = useAdminVariantsInventory(item.variant_id as string, {
enabled: isLocationFulfillmentEnabled,
})
const { reservations, isLoading: isReservationsLoading } =
useAdminReservations({
location_id: locationId,
line_item_id: [item.id],
})
const isLoading = isInventoryLoading || isReservationsLoading
const hasInventoryItem = !!variant?.inventory.length
React.useEffect(() => {
if (isLocationFulfillmentEnabled) {
refetch()
}
}, [isLocationFulfillmentEnabled, refetch])
const { availableQuantity, inStockQuantity } = useMemo(() => {
if (!isLocationFulfillmentEnabled) {
return {
availableQuantity: item.variant.inventory_quantity,
inStockQuantity: item.variant.inventory_quantity,
}
}
if (isLoading || !locationId || !variant) {
return {}
}
const { inventory } = variant
const locationInventory = inventory[0]?.location_levels?.find(
(inv) => inv.location_id === locationId
)
if (!locationInventory) {
return {}
}
const reservedQuantityForLineItem = reservations.reduce(
(q, r) => q + r.quantity,
0
)
return {
availableQuantity: locationInventory.available_quantity + reservedQuantityForLineItem,
inStockQuantity: locationInventory.stocked_quantity,
}
}, [
isLoading,
locationId,
variant,
item.variant,
isLocationFulfillmentEnabled,
])
const validQuantity =
!locationId ||
(locationId &&
(!availableQuantity || quantities[item.id] <= availableQuantity))
React.useEffect(() => {
setErrors((errors) => {
if (validQuantity) {
delete errors[item.id]
return errors
}
errors[item.id] = t(
"create-fulfillment-quantity-is-not-valid",
"Quantity is not valid"
)
return errors
})
}, [validQuantity, setErrors, item.id])
React.useEffect(() => {
if (!availableQuantity && hasInventoryItem) {
handleQuantityUpdate(0, item.id)
} else {
handleQuantityUpdate(
Math.min(
getFulfillableQuantity(item),
...[hasInventoryItem ? availableQuantity : Number.MAX_VALUE]
),
item.id
)
}
// Note: we can't add handleQuantityUpdate to the dependency array as it will cause an infinite loop
}, [availableQuantity, item, item.id])
if (getFulfillableQuantity(item) <= 0) {
return null
}
return (
<div
className={clsx(
"rounded-rounded hover:bg-grey-5 mx-[-5px] mb-1 flex h-[64px] justify-between py-2 px-[5px]",
{
"pointer-events-none opacity-50":
(!availableQuantity && hasInventoryItem) ||
(!locationId && isLocationFulfillmentEnabled),
}
)}
>
<div className="flex justify-center space-x-4">
<div className="rounded-rounded flex h-[48px] w-[36px] overflow-hidden">
{item.thumbnail ? (
<img src={item.thumbnail} className="object-cover" />
) : (
<ImagePlaceholder />
)}
</div>
<div className="flex max-w-[185px] flex-col justify-center">
<span className="inter-small-regular text-grey-90 truncate">
{item.title}
</span>
{item?.variant && (
<span className="inter-small-regular text-grey-50 truncate">
{`${item.variant.title}${
item.variant.sku ? ` (${item.variant.sku})` : ""
}`}
</span>
)}
</div>
</div>
<div className="flex items-center">
<FeatureToggle featureFlag="inventoryService">
{hasInventoryItem && (
<div className="inter-base-regular text-grey-50 mr-6 flex flex-col items-end whitespace-nowrap">
<p>{availableQuantity || 0} available</p>
<p>({inStockQuantity || 0} in stock)</p>
</div>
)}
</FeatureToggle>
<InputField
type="number"
name={`quantity`}
defaultValue={getFulfillableQuantity(item)}
min={0}
suffix={
<span className="flex">
{"/"}
<span className="pl-1">{getFulfillableQuantity(item)}</span>
</span>
}
value={quantities[item.id]}
max={Math.min(
getFulfillableQuantity(item),
...[hasInventoryItem ? availableQuantity || 0 : Number.MAX_VALUE]
)}
onChange={(e) =>
handleQuantityUpdate(e.target.valueAsNumber, item.id)
}
errors={
validQuantity
? undefined
: {
quantity: t(
"create-fulfillment-quantity-is-not-valid",
"Quantity is not valid"
),
}
}
/>
</div>
</div>
)
}
export default CreateFulfillmentItemsTable