Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix product type change

dominik-zeglen5e93a38b39a4
diff --git a/saleor/static/dashboard-next/hooks/useForm.ts b/saleor/static/dashboard-next/hooks/useForm.ts
index fd8d6904dc..c921ac51ef 100644
--- a/saleor/static/dashboard-next/hooks/useForm.ts
+++ b/saleor/static/dashboard-next/hooks/useForm.ts
@@ -5,10 +5,10 @@ import { UserError } from "@saleor/types";
import { toggle } from "@saleor/utils/lists";
import useStateFromProps from "./useStateFromProps";
-interface ChangeEvent<T> {
+export interface ChangeEvent<TName = string, TData = any> {
target: {
- name: keyof T | string;
- value: any;
+ name: keyof TName | string;
+ value: TData;
};
}
@@ -18,6 +18,7 @@ export interface UseFormResult<T> {
errors: Record<string, string>;
hasChanged: boolean;
reset: () => void;
+ set: (data: T) => void;
submit: () => void;
toggleValue: (event: ChangeEvent<T>) => void;
}
@@ -54,7 +55,7 @@ function useForm<T extends Record<keyof T, any | any[]>>(
}
}
- function change(event: ChangeEvent<T>, cb?: () => void) {
+ function change(event: ChangeEvent<T>) {
const { name, value } = event.target;
if (!(name in data)) {
@@ -69,15 +70,19 @@ function useForm<T extends Record<keyof T, any | any[]>>(
...data,
[name]: value
});
- if (typeof cb === "function") {
- cb();
- }
}
function reset() {
setData(initial);
}
+ function set(newData: Partial<T>) {
+ setData({
+ ...data,
+ ...newData
+ });
+ }
+
function submit() {
return onSubmit(data);
}
@@ -88,6 +93,7 @@ function useForm<T extends Record<keyof T, any | any[]>>(
errors: parseErrors(errors),
hasChanged,
reset,
+ set,
submit,
toggleValue
};
diff --git a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
index 216e575a4f..0fd7ee7555 100644
--- a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
@@ -130,7 +130,7 @@ export const ProductCreatePage: React.StatelessComponent<
initial={initialData}
confirmLeave
>
- {({ change, data, errors, hasChanged, submit }) => {
+ {({ change, data, errors, hasChanged, set, submit }) => {
const hasVariants =
data.productType && data.productType.value.hasVariants;
return (
@@ -203,6 +203,7 @@ export const ProductCreatePage: React.StatelessComponent<
data={data}
disabled={disabled}
onChange={change}
+ onSet={set}
/>
<CardSpacer />
<VisibilityCard
diff --git a/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx b/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
index 2c757074f8..c747cc53a5 100644
--- a/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
@@ -16,6 +16,7 @@ import { FormSpacer } from "@saleor/components/FormSpacer";
import MultiAutocompleteSelectField from "@saleor/components/MultiAutocompleteSelectField";
import SingleAutocompleteSelectField from "@saleor/components/SingleAutocompleteSelectField";
import Skeleton from "@saleor/components/Skeleton";
+import { ChangeEvent } from "@saleor/hooks/useForm";
import i18n from "../../../i18n";
import { maybe } from "../../../misc";
import { ProductCreateData_productTypes_edges_node_productAttributes } from "../../types/ProductCreateData";
@@ -51,27 +52,29 @@ const styles = (theme: Theme) =>
}
});
+interface ProductOrganizationFormData {
+ attributes: Array<{
+ slug: string;
+ value: string;
+ }>;
+ category: ChoiceType;
+ collections: ChoiceType[];
+ productType: {
+ label: string;
+ value: {
+ hasVariants: boolean;
+ id: string;
+ name: string;
+ productAttributes: ProductCreateData_productTypes_edges_node_productAttributes[];
+ };
+ };
+}
+
interface ProductOrganizationProps extends WithStyles<typeof styles> {
canChangeType: boolean;
categories?: Array<{ value: string; label: string }>;
collections?: Array<{ value: string; label: string }>;
- data: {
- attributes: Array<{
- slug: string;
- value: string;
- }>;
- category: ChoiceType;
- collections: ChoiceType[];
- productType: {
- label: string;
- value: {
- hasVariants: boolean;
- id: string;
- name: string;
- productAttributes: ProductCreateData_productTypes_edges_node_productAttributes[];
- };
- };
- };
+ data: ProductOrganizationFormData;
disabled: boolean;
errors: { [key: string]: string };
product?: {
@@ -83,7 +86,8 @@ interface ProductOrganizationProps extends WithStyles<typeof styles> {
productTypes?: ProductType[];
fetchCategories: (query: string) => void;
fetchCollections: (query: string) => void;
- onChange: (event: React.ChangeEvent<any>, cb?: () => void) => void;
+ onChange: (event: ChangeEvent<string, any>) => void;
+ onSet: (data: Partial<ProductOrganizationFormData>) => void;
}
const ProductOrganization = withStyles(styles, { name: "ProductOrganization" })(
@@ -99,7 +103,8 @@ const ProductOrganization = withStyles(styles, { name: "ProductOrganization" })(
fetchCollections,
product,
productTypes,
- onChange
+ onChange,
+ onSet
}: ProductOrganizationProps) => {
const unrolledAttributes = maybe(
() => data.productType.value.productAttributes,
@@ -141,35 +146,32 @@ const ProductOrganization = withStyles(styles, { name: "ProductOrganization" })(
const getAttributeValues = (slug: string) => {
const match = unrolledAttributes.find(a => a.slug === slug);
if (match) {
- return match.values;
+ return match.values.map(v => ({
+ label: v.name,
+ value: v.slug
+ }));
}
return [];
};
const handleProductTypeSelect = (
- event: React.ChangeEvent<{
- name: string;
- value: {
+ event: ChangeEvent<
+ string,
+ {
label: string;
value: ProductType;
- };
- }>
+ }
+ >
) => {
- onChange(event, () =>
- onChange({
- ...event,
- target: {
- ...event.target,
- name: "attributes",
- value: event.target.value.value.productAttributes.map(
- attribute => ({
- slug: attribute.slug,
- value: ""
- })
- )
- }
- })
- );
+ onSet({
+ attributes: event.target.value.value.productAttributes.map(
+ attribute => ({
+ slug: attribute.slug,
+ value: ""
+ })
+ ),
+ productType: event.target.value
+ });
};
const handleAttributeValueSelect = (
event: React.ChangeEvent<{
@@ -260,10 +262,7 @@ const ProductOrganization = withStyles(styles, { name: "ProductOrganization" })(
label={getAttributeName(item.slug)}
onChange={handleAttributeValueSelect}
value={getAttributeValue(item.slug)}
- choices={getAttributeValues(item.slug).map(v => ({
- label: v.name,
- value: v.slug
- }))}
+ choices={getAttributeValues(item.slug)}
custom
/>
<FormSpacer />
diff --git a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
index 93db6711cb..8fc31ac91d 100644
--- a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
@@ -206,7 +206,7 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
initial={initialData}
confirmLeave
>
- {({ change, data, errors, hasChanged, submit }) => (
+ {({ change, data, errors, hasChanged, set, submit }) => (
<>
<Container>
<AppHeader onBack={onBack}>{i18n.t("Products")}</AppHeader>
@@ -287,6 +287,7 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
data={data}
disabled={disabled}
onChange={change}
+ onSet={set}
/>
<CardSpacer />
<VisibilityCard