Skip to content

Commit

Permalink
fix(dashboard): Add default value to inventory item combobox (medusaj…
Browse files Browse the repository at this point in the history
…s#10412)

**What**
- Resolves CMRC-761
- Fixes TS errors in the same file.

Co-authored-by: Oli Juhl <[email protected]>
  • Loading branch information
kasperkristensen and olivermrbl authored Dec 3, 2024
1 parent 18583ed commit e8f4f7e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 45 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-crews-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/dashboard": patch
---

fix(dashboard): Add default value to inventory item combobox
11 changes: 7 additions & 4 deletions packages/admin/dashboard/src/hooks/api/products.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,15 @@ export const useUpdateProductVariantsBatch = (

export const useProductVariantsInventoryItemsBatch = (
productId: string,
options?: UseMutationOptions<any, FetchError, any>
options?: UseMutationOptions<
HttpTypes.AdminBatchProductVariantInventoryItemResponse,
FetchError,
HttpTypes.AdminBatchProductVariantInventoryItemRequest
>
) => {
return useMutation({
mutationFn: (
payload: HttpTypes.AdminBatchProductVariantInventoryItemRequest
) => sdk.admin.product.batchVariantInventoryItems(productId, payload),
mutationFn: (payload) =>
sdk.admin.product.batchVariantInventoryItems(productId, payload),
onSuccess: (data: any, variables: any, context: any) => {
queryClient.invalidateQueries({ queryKey: variantsQueryKeys.lists() })
queryClient.invalidateQueries({ queryKey: variantsQueryKeys.details() })
Expand Down
1 change: 1 addition & 0 deletions packages/admin/dashboard/src/hooks/use-combobox-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const useComboboxData = <
enabled: !!defaultValue,
})


const { data, ...rest } = useInfiniteQuery({
queryKey: [...queryKey, query],
queryFn: async ({ pageParam = 0 }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { zodResolver } from "@hookform/resolvers/zod"
import { XMarkMini } from "@medusajs/icons"
import { AdminInventoryItem, AdminProductVariant } from "@medusajs/types"
import { AdminProductVariant, HttpTypes } from "@medusajs/types"
import { Button, Heading, IconButton, Input, Label, toast } from "@medusajs/ui"
import i18next from "i18next"
import { useFieldArray, useForm } from "react-hook-form"
Expand All @@ -16,33 +16,43 @@ import {
import { KeyboundForm } from "../../../../../components/utilities/keybound-form"
import { useProductVariantsInventoryItemsBatch } from "../../../../../hooks/api/products"
import { useComboboxData } from "../../../../../hooks/use-combobox-data"
import { castNumber } from "../../../../../lib/cast-number"
import { sdk } from "../../../../../lib/client"

type ManageVariantInventoryItemsFormProps = {
variant: AdminProductVariant & {
inventory_items: {
inventory: AdminInventoryItem[]
inventory: HttpTypes.AdminInventoryItem
inventory_item_id: string
required_quantity: number
}
}[]
}
}

const ManageVariantInventoryItemsSchema = zod.object({
inventory: zod.array(
zod.object({
inventory_item_id: zod
.string()
.min(1, i18next.t("products.variant.inventory.validation.itemId")),
required_quantity: zod
.number({
errorMap: () => ({
zod
.object({
inventory_item_id: zod
.string()
.min(1, i18next.t("products.variant.inventory.validation.itemId")),
required_quantity: zod.union([zod.number(), zod.string()]),
})
.superRefine((data, ctx) => {
const quantity = data.required_quantity
? castNumber(data.required_quantity)
: 0

if (quantity < 1) {
ctx.addIssue({
code: zod.ZodIssueCode.custom,
message: i18next.t(
"products.variant.inventory.validation.quantity"
),
}),
})
.min(0, i18next.t("products.variant.inventory.validation.quantity")),
})
path: ["required_quantity"],
})
}
})
),
})

Expand Down Expand Up @@ -81,43 +91,44 @@ export function ManageVariantInventoryItemsForm({
queryFn: (params) => sdk.admin.inventoryItem.list(params),
getOptions: (data) =>
data.inventory_items.map((item) => ({
label: item.title,
value: item.id,
label: item.title || item.sku!,
value: item.id!,
})),
defaultValue: variant.inventory_items?.[0]?.inventory_item_id,
})

const { mutateAsync, isPending } = useProductVariantsInventoryItemsBatch(
variant.product_id
variant?.product_id!
)

const handleSubmit = form.handleSubmit(async (values) => {
const existingItems = {}
const selectedItems = {}
const existingItems: Record<string, number> = {}
const selectedItems: Record<string, boolean> = {}

variant.inventory_items.forEach(
(i) => (existingItems[i.inventory.id] = i.required_quantity)
)

values.inventory.forEach((i) => (selectedItems[i.inventory_item_id] = true))

const payload = {
create: [],
update: [],
delete: [],
}
const payload: HttpTypes.AdminBatchProductVariantInventoryItemRequest = {}

values.inventory.forEach((v) => {
if (v.inventory_item_id in existingItems) {
if (v.required_quantity !== existingItems[v.inventory_item_id]) {
payload.update = payload.update || []

payload.update.push({
required_quantity: v.required_quantity,
required_quantity: castNumber(v.required_quantity),
inventory_item_id: v.inventory_item_id,
variant_id: variant.id,
})
}
} else {
payload.create = payload.create || []

payload.create.push({
required_quantity: v.required_quantity,
required_quantity: castNumber(v.required_quantity),
inventory_item_id: v.inventory_item_id,
variant_id: variant.id,
})
Expand All @@ -126,19 +137,15 @@ export function ManageVariantInventoryItemsForm({

variant.inventory_items.forEach((i) => {
if (!(i.inventory.id in selectedItems)) {
payload.delete = payload.delete || []

payload.delete.push({
inventory_item_id: i.inventory.id,
variant_id: variant.id,
})
}
})

for (const k in payload) {
if (!payload[k].length) {
delete payload[k]
}
}

await mutateAsync(payload, {
onSuccess: () => {
toast.success(t("products.variant.inventory.toast.itemsManageSuccess"))
Expand Down Expand Up @@ -268,15 +275,7 @@ export function ManageVariantInventoryItemsForm({
className="bg-ui-bg-field-component"
min={0}
value={value}
onChange={(e) => {
const value = e.target.value

if (value === "") {
onChange(null)
} else {
onChange(Number(value))
}
}}
onChange={onChange}
{...field}
placeholder={t(
"products.create.inventory.quantityPlaceholder"
Expand Down

0 comments on commit e8f4f7e

Please sign in to comment.