From 4587a69f3f2b2ee60defac4a2c49df519bb235b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frane=20Poli=C4=87?= <16856471+fPolic@users.noreply.github.com> Date: Tue, 1 Oct 2024 11:03:51 +0200 Subject: [PATCH] fix(dashboard): allow to unset currency cell value (#9312) **What** - unset datagrid currency cell on delete press instead of setting it to 0 - consolidate pricing editors validations - fix PL edit pricing schema --- FIXES CC-529 --- .../hooks/use-data-grid-form-handlers.tsx | 4 +- .../create-shipping-options-form.tsx | 16 ++++--- .../src/routes/price-lists/common/schemas.ts | 4 +- .../price-list-prices-edit-form.tsx | 43 +++++++++++++------ .../create-product-variant-form.tsx | 19 ++++---- .../routes/products/product-create/utils.ts | 4 ++ .../products/product-prices/pricing-edit.tsx | 10 +++-- 7 files changed, 64 insertions(+), 36 deletions(-) diff --git a/packages/admin/dashboard/src/components/data-grid/hooks/use-data-grid-form-handlers.tsx b/packages/admin/dashboard/src/components/data-grid/hooks/use-data-grid-form-handlers.tsx index 69fffff082..bee8c62bac 100644 --- a/packages/admin/dashboard/src/components/data-grid/hooks/use-data-grid-form-handlers.tsx +++ b/packages/admin/dashboard/src/components/data-grid/hooks/use-data-grid-form-handlers.tsx @@ -12,7 +12,7 @@ type UseDataGridFormHandlersOptions = { export const useDataGridFormHandlers = < TData, - TFieldValues extends FieldValues + TFieldValues extends FieldValues, >({ matrix, form, @@ -119,7 +119,7 @@ export function convertArrayToPrimitive( ): any[] { switch (type) { case "number": - return values.map(convertToNumber) + return values.map((v) => (v === "" ? v : convertToNumber(v))) case "boolean": return values.map(convertToBoolean) case "text": diff --git a/packages/admin/dashboard/src/routes/locations/location-service-zone-shipping-option-create/components/create-shipping-options-form/create-shipping-options-form.tsx b/packages/admin/dashboard/src/routes/locations/location-service-zone-shipping-option-create/components/create-shipping-options-form/create-shipping-options-form.tsx index 0aa6f40ef1..4e7ebbe844 100644 --- a/packages/admin/dashboard/src/routes/locations/location-service-zone-shipping-option-create/components/create-shipping-options-form/create-shipping-options-form.tsx +++ b/packages/admin/dashboard/src/routes/locations/location-service-zone-shipping-option-create/components/create-shipping-options-form/create-shipping-options-form.tsx @@ -62,25 +62,29 @@ export function CreateShippingOptionsForm({ const handleSubmit = form.handleSubmit(async (data) => { const currencyPrices = Object.entries(data.currency_prices) .map(([code, value]) => { - const amount = value ? castNumber(value) : undefined + if (value === "" || value === undefined) { + return undefined + } return { currency_code: code, - amount: amount, + amount: castNumber(value), } }) - .filter((o) => !!o.amount) as { currency_code: string; amount: number }[] + .filter((o) => !!o) as { currency_code: string; amount: number }[] const regionPrices = Object.entries(data.region_prices) .map(([region_id, value]) => { - const amount = value ? castNumber(value) : undefined + if (value === "" || value === undefined) { + return undefined + } return { region_id, - amount: amount, + amount: castNumber(value), } }) - .filter((o) => !!o.amount) as { region_id: string; amount: number }[] + .filter((o) => !!o) as { region_id: string; amount: number }[] await mutateAsync( { diff --git a/packages/admin/dashboard/src/routes/price-lists/common/schemas.ts b/packages/admin/dashboard/src/routes/price-lists/common/schemas.ts index 755c14add1..dceda6eb87 100644 --- a/packages/admin/dashboard/src/routes/price-lists/common/schemas.ts +++ b/packages/admin/dashboard/src/routes/price-lists/common/schemas.ts @@ -57,7 +57,7 @@ export type PriceListCreateProductsSchema = z.infer< > export const PriceListUpdateCurrencyPriceSchema = z.object({ - amount: z.string().nullish(), + amount: z.string().or(z.number()).optional(), id: z.string().nullish(), }) @@ -66,7 +66,7 @@ export type PriceListUpdateCurrencyPrice = z.infer< > export const PriceListUpdateRegionPriceSchema = z.object({ - amount: z.string().nullish(), + amount: z.string().or(z.number()).optional(), id: z.string().nullish(), }) diff --git a/packages/admin/dashboard/src/routes/price-lists/price-list-prices-edit/components/price-list-prices-edit-form/price-list-prices-edit-form.tsx b/packages/admin/dashboard/src/routes/price-lists/price-list-prices-edit/components/price-list-prices-edit-form/price-list-prices-edit-form.tsx index 1d71d3df79..b9b090491f 100644 --- a/packages/admin/dashboard/src/routes/price-lists/price-list-prices-edit/components/price-list-prices-edit-form/price-list-prices-edit-form.tsx +++ b/packages/admin/dashboard/src/routes/price-lists/price-list-prices-edit/components/price-list-prices-edit-form/price-list-prices-edit-form.tsx @@ -185,10 +185,13 @@ function convertToPriceArray( ) { const prices: PriceObject[] = [] - const regionCurrencyMap = regions.reduce((map, region) => { - map[region.id] = region.currency_code - return map - }, {} as Record) + const regionCurrencyMap = regions.reduce( + (map, region) => { + map[region.id] = region.currency_code + return map + }, + {} as Record + ) for (const [_productId, product] of Object.entries(data || {})) { const { variants } = product || {} @@ -200,7 +203,10 @@ function convertToPriceArray( for (const [currencyCode, currencyPrice] of Object.entries( currencyPrices || {} )) { - if (currencyPrice?.amount) { + if ( + currencyPrice?.amount !== "" && + typeof currencyPrice?.amount !== "undefined" + ) { prices.push({ variantId, currencyCode, @@ -213,7 +219,10 @@ function convertToPriceArray( for (const [regionId, regionPrice] of Object.entries( regionPrices || {} )) { - if (regionPrice?.amount) { + if ( + regionPrice?.amount !== "" && + typeof regionPrice?.amount !== "undefined" + ) { prices.push({ variantId, regionId, @@ -240,15 +249,21 @@ function comparePrices(initialPrices: PriceObject[], newPrices: PriceObject[]) { const pricesToCreate: HttpTypes.AdminCreatePriceListPrice[] = [] const pricesToDelete: string[] = [] - const initialPriceMap = initialPrices.reduce((map, price) => { - map[createMapKey(price)] = price - return map - }, {} as Record) + const initialPriceMap = initialPrices.reduce( + (map, price) => { + map[createMapKey(price)] = price + return map + }, + {} as Record + ) - const newPriceMap = newPrices.reduce((map, price) => { - map[createMapKey(price)] = price - return map - }, {} as Record) + const newPriceMap = newPrices.reduce( + (map, price) => { + map[createMapKey(price)] = price + return map + }, + {} as Record + ) const keys = new Set([ ...Object.keys(initialPriceMap), diff --git a/packages/admin/dashboard/src/routes/products/product-create-variant/components/create-product-variant-form/create-product-variant-form.tsx b/packages/admin/dashboard/src/routes/products/product-create-variant/components/create-product-variant-form/create-product-variant-form.tsx index 7dcd1cb1ae..3b6f35bc64 100644 --- a/packages/admin/dashboard/src/routes/products/product-create-variant/components/create-product-variant-form/create-product-variant-form.tsx +++ b/packages/admin/dashboard/src/routes/products/product-create-variant/components/create-product-variant-form/create-product-variant-form.tsx @@ -74,10 +74,13 @@ export const CreateProductVariantForm = ({ return {} } - return regions.reduce((acc, reg) => { - acc[reg.id] = reg.currency_code - return acc - }, {} as Record) + return regions.reduce( + (acc, reg) => { + acc[reg.id] = reg.currency_code + return acc + }, + {} as Record + ) }, [regions]) const isManageInventoryEnabled = useWatch({ @@ -210,13 +213,13 @@ export const CreateProductVariantForm = ({ options: data.options, prices: Object.entries(data.prices ?? {}) .map(([currencyOrRegion, value]) => { - const ret: AdminCreateProductVariantPrice = {} - const amount = castNumber(value) - - if (isNaN(amount) || value === "") { + if (value === "" || value === undefined) { return undefined } + const ret: AdminCreateProductVariantPrice = {} + const amount = castNumber(value) + if (currencyOrRegion.startsWith("reg_")) { ret.rules = { region_id: currencyOrRegion } ret.currency_code = regionsCurrencyMap[currencyOrRegion] diff --git a/packages/admin/dashboard/src/routes/products/product-create/utils.ts b/packages/admin/dashboard/src/routes/products/product-create/utils.ts index 506de39ee8..ef57931290 100644 --- a/packages/admin/dashboard/src/routes/products/product-create/utils.ts +++ b/packages/admin/dashboard/src/routes/products/product-create/utils.ts @@ -75,6 +75,10 @@ export const normalizeVariants = ( .filter(Boolean), prices: Object.entries(variant.prices || {}) .map(([key, value]: any) => { + if (value === "" || value === undefined) { + return undefined + } + if (key.startsWith("reg_")) { return { currency_code: regionsCurrencyMap[key], diff --git a/packages/admin/dashboard/src/routes/products/product-prices/pricing-edit.tsx b/packages/admin/dashboard/src/routes/products/product-prices/pricing-edit.tsx index f01a9d94bc..9840a3f287 100644 --- a/packages/admin/dashboard/src/routes/products/product-prices/pricing-edit.tsx +++ b/packages/admin/dashboard/src/routes/products/product-prices/pricing-edit.tsx @@ -74,8 +74,11 @@ export const PricingEdit = ({ const handleSubmit = form.handleSubmit(async (values) => { const reqData = values.variants.map((variant, ind) => ({ id: variants[ind].id, - prices: Object.entries(variant.prices || {}).map( - ([currencyCodeOrRegionId, value]: any) => { + prices: Object.entries(variant.prices || {}) + .filter( + ([_, value]) => value !== "" && typeof value !== "undefined" // deleted cells + ) + .map(([currencyCodeOrRegionId, value]: any) => { const regionId = currencyCodeOrRegionId.startsWith("reg_") ? currencyCodeOrRegionId : undefined @@ -105,8 +108,7 @@ export const PricingEdit = ({ amount, ...(regionId ? { rules: { region_id: regionId } } : {}), } - } - ), + }), })) await mutateAsync(reqData, {