feat(core-flows, dashboard, medusa, types): optional shipping profile (#11434)
* feat: create product flow changes * feat: allow unsetting SP on product update * feat: update prepare line item helper * test: add testcase * wip: fix tests * fix: update module tests * fix: cart module test
This commit is contained in:
-9
@@ -181,15 +181,6 @@ export const ProductCreateForm = ({
|
||||
}
|
||||
|
||||
if (currentTab === Tab.ORGANIZE) {
|
||||
// TODO: this is temp until we add partial validation per tab
|
||||
if (!form.getValues("shipping_profile_id")) {
|
||||
form.setError("shipping_profile_id", {
|
||||
type: "required",
|
||||
message: t("products.shippingProfile.create.errors.required"),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
setTab(Tab.VARIANTS)
|
||||
}
|
||||
|
||||
|
||||
+3
-1
@@ -173,7 +173,9 @@ export const ProductCreateOrganizationSection = ({
|
||||
</div>
|
||||
<div className="grid grid-cols-1 gap-4 md:grid-cols-2">
|
||||
<div>
|
||||
<Form.Label>{t("products.fields.shipping_profile.label")}</Form.Label>
|
||||
<Form.Label optional>
|
||||
{t("products.fields.shipping_profile.label")}
|
||||
</Form.Label>
|
||||
<Form.Hint>
|
||||
<Trans i18nKey={"products.fields.shipping_profile.hint"} />
|
||||
</Form.Hint>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { z } from "zod"
|
||||
import { i18n } from "../../../components/utilities/i18n/i18n.tsx"
|
||||
import { optionalFloat, optionalInt } from "../../../lib/validation.ts"
|
||||
import { decorateVariantsWithDefaultValues } from "./utils.ts"
|
||||
import { i18n } from "../../../components/utilities/i18n/i18n"
|
||||
import { optionalFloat, optionalInt } from "../../../lib/validation"
|
||||
import { decorateVariantsWithDefaultValues } from "./utils"
|
||||
|
||||
export const MediaSchema = z.object({
|
||||
id: z.string().optional(),
|
||||
@@ -64,7 +64,7 @@ export const ProductCreateSchema = z
|
||||
discountable: z.boolean(),
|
||||
type_id: z.string().optional(),
|
||||
collection_id: z.string().optional(),
|
||||
shipping_profile_id: z.string(), // TODO: require min(1) when partial validation per tab is added
|
||||
shipping_profile_id: z.string().optional(),
|
||||
categories: z.array(z.string()),
|
||||
tags: z.array(z.string()).optional(),
|
||||
sales_channels: z
|
||||
|
||||
@@ -24,7 +24,7 @@ export const normalizeProductFormValues = (
|
||||
: undefined,
|
||||
images,
|
||||
collection_id: values.collection_id || undefined,
|
||||
shipping_profile_id: values.shipping_profile_id,
|
||||
shipping_profile_id: values.shipping_profile_id || undefined,
|
||||
categories: values.categories.map((id) => ({ id })),
|
||||
type_id: values.type_id || undefined,
|
||||
handle: values.handle || undefined,
|
||||
|
||||
+13
-2
@@ -7,12 +7,13 @@ import { Form } from "../../../../../components/common/form"
|
||||
import { Combobox } from "../../../../../components/inputs/combobox"
|
||||
import { RouteDrawer, useRouteModal } from "../../../../../components/modals"
|
||||
import { KeyboundForm } from "../../../../../components/utilities/keybound-form"
|
||||
import { useExtendableForm } from "../../../../../extensions"
|
||||
import { useUpdateProduct } from "../../../../../hooks/api/products"
|
||||
import { useComboboxData } from "../../../../../hooks/use-combobox-data"
|
||||
import { sdk } from "../../../../../lib/client"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { se } from "date-fns/locale"
|
||||
import { useEffect } from "react"
|
||||
|
||||
type ProductShippingProfileFormProps = {
|
||||
product: HttpTypes.AdminProduct & {
|
||||
@@ -47,12 +48,15 @@ export const ProductShippingProfileForm = ({
|
||||
resolver: zodResolver(ProductShippingProfileSchema),
|
||||
})
|
||||
|
||||
const selectedShippingProfile = form.watch("shipping_profile_id")
|
||||
|
||||
const { mutateAsync, isPending } = useUpdateProduct(product.id)
|
||||
|
||||
const handleSubmit = form.handleSubmit(async (data) => {
|
||||
await mutateAsync(
|
||||
{
|
||||
shipping_profile_id: data.shipping_profile_id,
|
||||
shipping_profile_id:
|
||||
data.shipping_profile_id === "" ? null : data.shipping_profile_id,
|
||||
},
|
||||
{
|
||||
onSuccess: ({ product }) => {
|
||||
@@ -70,6 +74,12 @@ export const ProductShippingProfileForm = ({
|
||||
)
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof selectedShippingProfile === "undefined") {
|
||||
form.setValue("shipping_profile_id", "")
|
||||
}
|
||||
}, [selectedShippingProfile])
|
||||
|
||||
return (
|
||||
<RouteDrawer.Form form={form}>
|
||||
<KeyboundForm onSubmit={handleSubmit} className="flex h-full flex-col">
|
||||
@@ -87,6 +97,7 @@ export const ProductShippingProfileForm = ({
|
||||
<Form.Control>
|
||||
<Combobox
|
||||
{...field}
|
||||
allowClear
|
||||
options={shippingProfiles.options}
|
||||
searchValue={shippingProfiles.searchValue}
|
||||
onSearchValueChange={
|
||||
|
||||
Reference in New Issue
Block a user