Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

fix(vendor): accept decimal comma in offer price fields (#1520)

Viktor Holikedf4038af594
diff --git a/packages/vendor/src/pages/offers/[id]/edit-price/index.tsx b/packages/vendor/src/pages/offers/[id]/edit-price/index.tsx
index a54fcee3f..dd6d2f3c4 100644
--- a/packages/vendor/src/pages/offers/[id]/edit-price/index.tsx
+++ b/packages/vendor/src/pages/offers/[id]/edit-price/index.tsx
@@ -18,6 +18,7 @@ import { usePricePreferences } from "../../../../hooks/api/price-preferences"
import { useProduct, productsQueryKeys } from "../../../../hooks/api/products"
import { offerQueryKeys } from "../../../../hooks/api/offers"
import { useCurrentSeller } from "../../../../hooks/api/sellers"
+import { castNumber } from "../../../../lib/cast-number"
import { sdk } from "../../../../lib/client"
import { queryClient } from "../../../../lib/query-client"
import { OFFER_PRODUCT_DETAIL_FIELDS } from "../../common/constants"
@@ -26,7 +27,7 @@ type EditPriceRow = {
offer_id: string
variant_title: string
product_thumbnail?: string | null
- prices: Record<string, number | "">
+ prices: Record<string, number | string>
}
type FormValues = { rows: EditPriceRow[] }
@@ -37,8 +38,8 @@ type PriceProduct = HttpTypes.AdminProduct & {
> | null
}
-const numericOrZero = (v: number | "" | undefined | null): number =>
- v === "" || v === null || v === undefined ? 0 : Number(v) || 0
+const numericOrZero = (v: number | string | undefined | null): number =>
+ v === "" || v === null || v === undefined ? 0 : castNumber(v) || 0
const buildRows = (product: PriceProduct, currencies: string[]): EditPriceRow[] =>
(product.variants ?? []).flatMap((variant) =>
diff --git a/packages/vendor/src/pages/offers/[id]/pricing/pricing-form/pricing-form.tsx b/packages/vendor/src/pages/offers/[id]/pricing/pricing-form/pricing-form.tsx
index dfd8f5c47..7e1ead77e 100644
--- a/packages/vendor/src/pages/offers/[id]/pricing/pricing-form/pricing-form.tsx
+++ b/packages/vendor/src/pages/offers/[id]/pricing/pricing-form/pricing-form.tsx
@@ -18,6 +18,7 @@ import { KeyboundForm } from "../../../../../components/utilities/keybound-form"
import { useUpdateOffer } from "../../../../../hooks/api/offers"
import { usePricePreferences } from "../../../../../hooks/api/price-preferences"
import { useCurrentSeller } from "../../../../../hooks/api/sellers"
+import { castNumber } from "../../../../../lib/cast-number"
import { OfferDetail, OfferPrice } from "../../../common/types"
import {
PricingFormSchema,
@@ -110,7 +111,7 @@ const PricingFormInner = ({
row.currency_prices ?? {},
)) {
if (amount === "" || amount === undefined || amount === null) continue
- const num = Number(amount)
+ const num = castNumber(amount)
if (!Number.isFinite(num)) continue
submitted.push({ amount: num, currency_code })
}
diff --git a/packages/vendor/src/pages/offers/[id]/pricing/pricing-form/schema.ts b/packages/vendor/src/pages/offers/[id]/pricing/pricing-form/schema.ts
index e26fef8d9..189c3cfaa 100644
--- a/packages/vendor/src/pages/offers/[id]/pricing/pricing-form/schema.ts
+++ b/packages/vendor/src/pages/offers/[id]/pricing/pricing-form/schema.ts
@@ -1,6 +1,8 @@
import { z } from "zod"
-const PriceCellSchema = z.union([z.coerce.number().min(0), z.literal("")])
+import { optionalFloat } from "../../../../../lib/validation"
+
+const PriceCellSchema = optionalFloat
const PriceRowSchema = z.object({
id: z.string(),
diff --git a/packages/vendor/src/pages/offers/create/create-offer-form/create-offer-form.tsx b/packages/vendor/src/pages/offers/create/create-offer-form/create-offer-form.tsx
index 637d3b4a9..f9802ecc3 100644
--- a/packages/vendor/src/pages/offers/create/create-offer-form/create-offer-form.tsx
+++ b/packages/vendor/src/pages/offers/create/create-offer-form/create-offer-form.tsx
@@ -7,6 +7,7 @@ import { useTranslation } from "react-i18next";
import { RouteFocusModal, useRouteModal } from "../../../../components/modals";
import { TabbedForm } from "../../../../components/tabbed-form/tabbed-form";
import { useBulkCreateOffers } from "../../../../hooks/api/offers";
+import { castNumber } from "../../../../lib/cast-number";
import { useProducts } from "../../../../hooks/api/products";
import { useCurrentSeller } from "../../../../hooks/api/sellers";
import { useStockLocations } from "../../../../hooks/api/stock-locations";
@@ -23,9 +24,9 @@ const DEFAULTS: CreateOfferFormValues = {
variants: [],
};
-const numericOrZero = (v: number | "" | undefined | null): number => {
+const numericOrZero = (v: number | string | undefined | null): number => {
if (v === "" || v === null || v === undefined) return 0;
- return Number(v) || 0;
+ return castNumber(v) || 0;
};
const attachErrorToRow = (
diff --git a/packages/vendor/src/pages/offers/create/create-offer-form/schema.ts b/packages/vendor/src/pages/offers/create/create-offer-form/schema.ts
index 5e3745793..d4ddeb4d5 100644
--- a/packages/vendor/src/pages/offers/create/create-offer-form/schema.ts
+++ b/packages/vendor/src/pages/offers/create/create-offer-form/schema.ts
@@ -1,5 +1,8 @@
import { z } from "zod"
+import { castNumber } from "../../../../lib/cast-number"
+import { optionalFloat } from "../../../../lib/validation"
+
const VariantRowSchema = z.object({
variant_id: z.string().min(1),
product_id: z.string().min(1),
@@ -9,7 +12,7 @@ const VariantRowSchema = z.object({
variant_sku: z.string().nullish(),
sku: z.string().max(64).default(""),
shipping_profile_id: z.string().default(""),
- prices: z.record(z.string(), z.union([z.coerce.number().min(0), z.literal("")])).default({}),
+ prices: z.record(z.string(), optionalFloat).default({}),
inventory: z
.record(
z.string(),
@@ -33,9 +36,9 @@ export const CreateOfferSchema = z.object({
export type CreateOfferFormValues = z.infer<typeof CreateOfferSchema>
-const numericOrZero = (v: number | "" | undefined | null): number => {
+const numericOrZero = (v: number | string | undefined | null): number => {
if (v === "" || v === null || v === undefined) return 0
- return Number(v) || 0
+ return castNumber(v) || 0
}
export const isVariantRowPublishable = (row: OfferVariantRow): boolean => {