Skip to content

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

Fix product type autocomplete

dominik-zeglena75d85add3ef
diff --git a/saleor/static/dashboard-next/components/Form/Form.tsx b/saleor/static/dashboard-next/components/Form/Form.tsx
index 7579afbbaf..9d530456f5 100644
--- a/saleor/static/dashboard-next/components/Form/Form.tsx
+++ b/saleor/static/dashboard-next/components/Form/Form.tsx
@@ -7,7 +7,7 @@ export interface FormProps<T extends {}> {
data: T;
hasChanged: boolean;
errors: { [key: string]: string };
- change(event: React.ChangeEvent<any>);
+ change(event: React.ChangeEvent<any>, cb?: () => void);
submit(event?: React.FormEvent<any>);
}
) => React.ReactElement<any>);
@@ -86,19 +86,22 @@ class FormComponent<T extends {} = {}> extends React.Component<
}
}
- handleChange = (event: React.ChangeEvent<any>) => {
+ handleChange = (event: React.ChangeEvent<any>, cb?: () => void) => {
const { target } = event;
if (!(target.name in this.state.fields)) {
console.error(`Unknown form field: ${target.name}`);
return;
}
- this.setState({
- fields: {
- ...(this.state.fields as any),
- [target.name]: target.value
- }
- });
+ this.setState(
+ {
+ fields: {
+ ...(this.state.fields as any),
+ [target.name]: target.value
+ }
+ },
+ cb
+ );
};
handleKeyDown = (event: React.KeyboardEvent<any>) => {
diff --git a/saleor/static/dashboard-next/misc.ts b/saleor/static/dashboard-next/misc.ts
index 7ddf7b5fbd..5cf1cb7a8c 100644
--- a/saleor/static/dashboard-next/misc.ts
+++ b/saleor/static/dashboard-next/misc.ts
@@ -28,8 +28,11 @@ export function renderCollection<T>(
return collection.map(renderItem);
}
-export function decimal(value: string) {
- return value === "" ? null : value;
+export function decimal(value: string | number) {
+ if (typeof value === "string") {
+ return value === "" ? null : value;
+ }
+ return value;
}
export const removeDoubleSlashes = (url: string) =>
diff --git a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
index 1eb4dfeec3..fa712fe297 100644
--- a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
@@ -20,16 +20,20 @@ import ProductDetailsForm from "../ProductDetailsForm";
import ProductOrganization from "../ProductOrganization";
import ProductPricing from "../ProductPricing";
-interface FormData {
+interface ChoiceType {
+ label: string;
+ value: string;
+}
+export interface FormData {
attributes: Array<{
slug: string;
value: string;
}>;
available: boolean;
availableOn: string;
- category: string;
+ category: ChoiceType;
chargeTaxes: boolean;
- collections: string[];
+ collections: ChoiceType[];
description: string;
name: string;
price: number;
@@ -89,6 +93,8 @@ interface ProductCreatePageProps extends WithStyles<typeof styles> {
}>;
header: string;
saveButtonBarState: ConfirmButtonTransitionState;
+ fetchCategories: (data: string) => void;
+ fetchCollections: (data: string) => void;
onAttributesEdit: () => void;
onBack?();
onSubmit?(data: FormData);
@@ -104,6 +110,8 @@ export const ProductCreatePage = withStyles(styles, {
categories,
collections,
errors: userErrors,
+ fetchCategories,
+ fetchCollections,
header,
productTypes,
saveButtonBarState,
@@ -114,7 +122,10 @@ export const ProductCreatePage = withStyles(styles, {
attributes: [],
available: false,
availableOn: "",
- category: "",
+ category: {
+ label: "",
+ value: ""
+ },
chargeTaxes: false,
collections: [],
description: "",
@@ -171,7 +182,6 @@ export const ProductCreatePage = withStyles(styles, {
</div>
<div>
<ProductOrganization
- category={data.category}
categories={
categories !== undefined && categories !== null
? categories.map(category => ({
@@ -181,7 +191,8 @@ export const ProductCreatePage = withStyles(styles, {
: []
}
errors={errors}
- productCollections={data.collections}
+ fetchCategories={fetchCategories}
+ fetchCollections={fetchCollections}
collections={
collections !== undefined && collections !== null
? collections.map(collection => ({
diff --git a/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx b/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
index fa2a9fd3b9..e6d9af06a6 100644
--- a/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductOrganization/ProductOrganization.tsx
@@ -78,7 +78,7 @@ interface ProductOrganizationProps extends WithStyles<typeof styles> {
productTypes?: ProductType[];
fetchCategories: (query: string) => void;
fetchCollections: (query: string) => void;
- onChange: (event: React.ChangeEvent<any>) => void;
+ onChange: (event: React.ChangeEvent<any>, cb?: () => void) => void;
}
const ProductOrganization = withStyles(styles, { name: "ProductOrganization" })(
@@ -128,18 +128,21 @@ const ProductOrganization = withStyles(styles, { name: "ProductOrganization" })(
};
}>
) => {
- onChange(event);
- onChange({
- ...event,
- target: {
- ...event.target,
- name: "attributes",
- value: event.target.value.value.productAttributes.map(attribute => ({
- slug: attribute.slug,
- value: ""
- }))
- }
- });
+ onChange(event, () =>
+ onChange({
+ ...event,
+ target: {
+ ...event.target,
+ name: "attributes",
+ value: event.target.value.value.productAttributes.map(
+ attribute => ({
+ slug: attribute.slug,
+ value: ""
+ })
+ )
+ }
+ })
+ );
};
const handleAttributeValueSelect = (
event: React.ChangeEvent<{
diff --git a/saleor/static/dashboard-next/products/views/ProductCreate.tsx b/saleor/static/dashboard-next/products/views/ProductCreate.tsx
index 531fdc31db..91ee311ec7 100644
--- a/saleor/static/dashboard-next/products/views/ProductCreate.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductCreate.tsx
@@ -6,7 +6,9 @@ import Shop from "../../components/Shop";
import { WindowTitle } from "../../components/WindowTitle";
import i18n from "../../i18n";
import { decimal, getMutationState, maybe } from "../../misc";
-import ProductCreatePage from "../components/ProductCreatePage";
+import ProductCreatePage, { FormData } from "../components/ProductCreatePage";
+import { CategorySearchProvider } from "../containers/CategorySearch";
+import { CollectionSearchProvider } from "../containers/CollectionSearch";
import { TypedProductCreateMutation } from "../mutations";
import { TypedProductCreateQuery } from "../queries";
import { ProductCreate } from "../types/ProductCreate";
@@ -30,102 +32,138 @@ export const ProductUpdate: React.StatelessComponent<
const handleBack = () => navigate(productListUrl());
return (
- <TypedProductCreateQuery displayLoader>
- {({ data, loading }) => {
- const handleSuccess = (data: ProductCreate) => {
- if (data.productCreate.errors.length === 0) {
- pushMessage({ text: i18n.t("Product created") });
- navigate(productUrl(data.productCreate.product.id));
- }
- };
-
- return (
- <TypedProductCreateMutation onCompleted={handleSuccess}>
- {(
- productCreate,
- {
- called: productCreateCalled,
- data: productCreateData,
- loading: productCreateDataLoading
- }
- ) => {
- const handleSubmit = formData => {
- productCreate({
- variables: {
- attributes: formData.attributes,
- availableOn:
- formData.availableOn !== ""
- ? formData.availableOn
- : null,
- category: formData.category,
- chargeTaxes: formData.chargeTaxes,
- collections: formData.collections,
- description: formData.description,
- isPublished: formData.available,
- name: formData.name,
- price: decimal(formData.price),
- productType: formData.productType.value.id
+ <CategorySearchProvider>
+ {({
+ search: searchCategory,
+ searchOpts: searchCategoryOpts
+ }) => (
+ <CollectionSearchProvider>
+ {({
+ search: searchCollection,
+ searchOpts: searchCollectionOpts
+ }) => (
+ <TypedProductCreateQuery displayLoader>
+ {({ data, loading }) => {
+ const handleSuccess = (data: ProductCreate) => {
+ if (data.productCreate.errors.length === 0) {
+ pushMessage({
+ text: i18n.t("Product created")
+ });
+ navigate(
+ productUrl(data.productCreate.product.id)
+ );
}
- });
- };
+ };
+
+ return (
+ <TypedProductCreateMutation
+ onCompleted={handleSuccess}
+ >
+ {(
+ productCreate,
+ {
+ called: productCreateCalled,
+ data: productCreateData,
+ loading: productCreateDataLoading
+ }
+ ) => {
+ const handleSubmit = (
+ formData: FormData
+ ) => {
+ productCreate({
+ variables: {
+ attributes: formData.attributes,
+ availableOn:
+ formData.availableOn !== ""
+ ? formData.availableOn
+ : null,
+ category: formData.category.value,
+ chargeTaxes: formData.chargeTaxes,
+ collections: formData.collections.map(
+ collection => collection.value
+ ),
+ description: formData.description,
+ isPublished: formData.available,
+ name: formData.name,
+ price: decimal(formData.price),
+ productType:
+ formData.productType.value.id
+ }
+ });
+ };
- const disabled =
- loading || productCreateDataLoading;
+ const disabled =
+ loading || productCreateDataLoading;
- const formTransitionState = getMutationState(
- productCreateCalled,
- productCreateDataLoading,
- maybe(
- () => productCreateData.productCreate.errors
- )
- );
- return (
- <>
- <WindowTitle title={i18n.t("Create product")} />
- <ProductCreatePage
- currency={maybe(() => shop.defaultCurrency)}
- categories={
- data && data.categories
- ? data.categories.edges.map(
- edge => edge.node
- )
- : undefined
- }
- collections={
- data && data.collections
- ? data.collections.edges.map(
- edge => edge.node
- )
- : undefined
- }
- disabled={disabled}
- errors={
- productCreateData &&
- productCreateData.productCreate &&
- productCreateData.productCreate.errors
- ? productCreateData.productCreate.errors
- : []
- }
- header={i18n.t("New Product")}
- productTypes={
- data && data.productTypes
- ? data.productTypes.edges.map(
- edge => edge.node
- )
- : undefined
- }
- onAttributesEdit={handleAttributesEdit}
- onBack={handleBack}
- onSubmit={handleSubmit}
- saveButtonBarState={formTransitionState}
- />
- </>
- );
- }}
- </TypedProductCreateMutation>
- );
- }}
- </TypedProductCreateQuery>
+ const formTransitionState = getMutationState(
+ productCreateCalled,
+ productCreateDataLoading,
+ maybe(
+ () =>
+ productCreateData.productCreate.errors
+ )
+ );
+ return (
+ <>
+ <WindowTitle
+ title={i18n.t("Create product")}
+ />
+ <ProductCreatePage
+ currency={maybe(
+ () => shop.defaultCurrency
+ )}
+ categories={maybe(
+ () =>
+ searchCategoryOpts.data.categories
+ .edges,
+ []
+ ).map(edge => edge.node)}
+ collections={maybe(
+ () =>
+ searchCollectionOpts.data
+ .collections.edges,
+ []
+ ).map(edge => edge.node)}
+ disabled={disabled}
+ errors={
+ productCreateData &&
+ productCreateData.productCreate &&
+ productCreateData.productCreate
+ .errors
+ ? productCreateData.productCreate
+ .errors
+ : []
+ }
+ fetchCategories={searchCategory}
+ fetchCollections={searchCollection}
+ header={i18n.t("New Product")}
+ productTypes={
+ data && data.productTypes
+ ? data.productTypes.edges.map(
+ edge => edge.node
+ )
+ : undefined
+ }
+ onAttributesEdit={
+ handleAttributesEdit
+ }
+ onBack={handleBack}
+ onSubmit={handleSubmit}
+ saveButtonBarState={
+ formTransitionState
+ }
+ />
+ </>
+ );
+ }}
+ </TypedProductCreateMutation>
+ );
+ }}
+ </TypedProductCreateQuery>
+ )}
+ </CollectionSearchProvider>
+ )}
+ </CategorySearchProvider>
);
}}
</Navigator>