Skip to content

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

Fix adding simple products

dominik-zeglena0bdcd060537
diff --git a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
index 3f3ef63320..59e62e7d2f 100644
--- a/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductCreatePage/ProductCreatePage.tsx
@@ -11,12 +11,14 @@ import PageHeader from "../../../components/PageHeader";
import SaveButtonBar from "../../../components/SaveButtonBar/SaveButtonBar";
import SeoForm from "../../../components/SeoForm";
import i18n from "../../../i18n";
+import { maybe } from "../../../misc";
import { UserError } from "../../../types";
import { ProductCreateData_productTypes_edges_node_productAttributes } from "../../types/ProductCreateData";
import ProductAvailabilityForm from "../ProductAvailabilityForm";
import ProductDetailsForm from "../ProductDetailsForm";
import ProductOrganization from "../ProductOrganization";
import ProductPricing from "../ProductPricing";
+import ProductStock from "../ProductStock";
interface ChoiceType {
label: string;
@@ -120,6 +122,7 @@ export const ProductCreatePage: React.StatelessComponent<
sku: null,
stockQuantity: null
};
+
return (
<Form
onSubmit={onSubmit}
@@ -127,81 +130,96 @@ export const ProductCreatePage: React.StatelessComponent<
initial={initialData}
confirmLeave
>
- {({ change, data, errors, hasChanged, submit }) => (
- <Container>
- <AppHeader onBack={onBack}>{i18n.t("Products")}</AppHeader>
- <PageHeader title={header} />
- <Grid>
- <div>
- <ProductDetailsForm
- data={data}
- disabled={disabled}
- errors={errors}
- onChange={change}
- />
- <CardSpacer />
- <ProductPricing
- currency={currency}
- data={data}
- disabled={disabled}
- onChange={change}
- />
- <CardSpacer />
- <SeoForm
- helperText={i18n.t(
- "Add search engine title and description to make this product easier to find"
+ {({ change, data, errors, hasChanged, submit }) => {
+ const hasVariants =
+ data.productType && data.productType.value.hasVariants;
+ return (
+ <Container>
+ <AppHeader onBack={onBack}>{i18n.t("Products")}</AppHeader>
+ <PageHeader title={header} />
+ <Grid>
+ <div>
+ <ProductDetailsForm
+ data={data}
+ disabled={disabled}
+ errors={errors}
+ onChange={change}
+ />
+ <CardSpacer />
+ <ProductPricing
+ currency={currency}
+ data={data}
+ disabled={disabled}
+ onChange={change}
+ />
+ <CardSpacer />
+ {!hasVariants && (
+ <>
+ <ProductStock
+ data={data}
+ disabled={disabled}
+ product={undefined}
+ onChange={change}
+ />
+ <CardSpacer />
+ </>
)}
- title={data.seoTitle}
- titlePlaceholder={data.name}
- description={data.seoDescription}
- descriptionPlaceholder={data.seoTitle}
- loading={disabled}
- onChange={change}
- />
- </div>
- <div>
- <ProductOrganization
- categories={
- categories !== undefined && categories !== null
- ? categories.map(category => ({
+ <SeoForm
+ helperText={i18n.t(
+ "Add search engine title and description to make this product easier to find"
+ )}
+ title={data.seoTitle}
+ titlePlaceholder={data.name}
+ description={data.seoDescription}
+ descriptionPlaceholder={data.seoTitle}
+ loading={disabled}
+ onChange={change}
+ />
+ </div>
+ <div>
+ <ProductOrganization
+ categories={maybe(
+ () =>
+ categories.map(category => ({
label: category.name,
value: category.id
- }))
- : []
- }
- errors={errors}
- fetchCategories={fetchCategories}
- fetchCollections={fetchCollections}
- collections={
- collections !== undefined && collections !== null
- ? collections.map(collection => ({
+ })),
+ []
+ )}
+ errors={errors}
+ fetchCategories={fetchCategories}
+ fetchCollections={fetchCollections}
+ collections={maybe(
+ () =>
+ collections.map(collection => ({
label: collection.name,
value: collection.id
- }))
- : []
- }
- productTypes={productTypes}
- data={data}
- disabled={disabled}
- onChange={change}
- />
- <CardSpacer />
- <ProductAvailabilityForm
- data={data}
- errors={errors}
- loading={disabled}
- onChange={change}
- />
- </div>
- </Grid>
- <SaveButtonBar
- onCancel={onBack}
- onSave={submit}
- state={saveButtonBarState}
- disabled={disabled || !onSubmit || !hasChanged}
- />
- </Container>
- )}
+ })),
+ []
+ )}
+ productTypes={productTypes}
+ data={data}
+ disabled={disabled}
+ onChange={change}
+ />
+ <CardSpacer />
+ <ProductAvailabilityForm
+ data={data}
+ errors={errors}
+ loading={disabled}
+ onChange={change}
+ />
+ </div>
+ </Grid>
+ <SaveButtonBar
+ onCancel={onBack}
+ onSave={submit}
+ state={saveButtonBarState}
+ disabled={disabled || !onSubmit || !hasChanged}
+ />
+ </Container>
+ );
+ }}
</Form>
);
};
diff --git a/saleor/static/dashboard-next/products/components/ProductStock/ProductStock.tsx b/saleor/static/dashboard-next/products/components/ProductStock/ProductStock.tsx
index d0b115895f..7da4553a78 100644
--- a/saleor/static/dashboard-next/products/components/ProductStock/ProductStock.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductStock/ProductStock.tsx
@@ -53,9 +53,13 @@ const ProductStock = withStyles(styles, { name: "ProductStock" })(
value={data.stockQuantity}
type="number"
onChange={onChange}
- helperText={i18n.t("Allocated: {{ quantity }}", {
- quantity: maybe(() => product.variants[0].quantityAllocated)
- })}
+ helperText={
+ product
+ ? i18n.t("Allocated: {{ quantity }}", {
+ quantity: maybe(() => product.variants[0].quantityAllocated)
+ })
+ : undefined
+ }
/>
</div>
</CardContent>
diff --git a/saleor/static/dashboard-next/products/mutations.ts b/saleor/static/dashboard-next/products/mutations.ts
index 1d5477fdc7..9f847d2be6 100644
--- a/saleor/static/dashboard-next/products/mutations.ts
+++ b/saleor/static/dashboard-next/products/mutations.ts
@@ -210,6 +210,8 @@ export const productCreateMutation = gql`
$name: String!
$price: Decimal
$productType: ID!
+ $sku: String
+ $stockQuantity: Int
) {
productCreate(
input: {
@@ -223,6 +225,8 @@ export const productCreateMutation = gql`
name: $name
price: $price
productType: $productType
+ sku: $sku
+ quantity: $stockQuantity
}
) {
errors {
diff --git a/saleor/static/dashboard-next/products/types/ProductCreate.ts b/saleor/static/dashboard-next/products/types/ProductCreate.ts
index 8ec1fee9a0..d444319430 100644
--- a/saleor/static/dashboard-next/products/types/ProductCreate.ts
+++ b/saleor/static/dashboard-next/products/types/ProductCreate.ts
@@ -194,4 +194,6 @@ export interface ProductCreateVariables {
name: string;
price?: any | null;
productType: string;
+ sku?: string | null;
+ stockQuantity?: number | null;
}
diff --git a/saleor/static/dashboard-next/products/views/ProductCreate.tsx b/saleor/static/dashboard-next/products/views/ProductCreate.tsx
index 67812657b0..9d6ddd269f 100644
--- a/saleor/static/dashboard-next/products/views/ProductCreate.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductCreate.tsx
@@ -89,7 +89,9 @@ export const ProductUpdate: React.StatelessComponent<
publicationDate:
formData.publicationDate !== ""
? formData.publicationDate
- : null
+ : null,
+ sku: formData.sku,
+ stockQuantity: formData.stockQuantity
}
});
};
@@ -127,25 +129,20 @@ export const ProductUpdate: React.StatelessComponent<
[]
).map(edge => edge.node)}
disabled={disabled}
- errors={
- productCreateData &&
- productCreateData.productCreate &&
- productCreateData.productCreate
- .errors
- ? productCreateData.productCreate
- .errors
- : []
- }
+ errors={maybe(
+ () =>
+ productCreateData.productCreate
+ .errors,
+ []
+ )}
fetchCategories={searchCategory}
fetchCollections={searchCollection}
header={i18n.t("New Product")}
- productTypes={
- data && data.productTypes
- ? data.productTypes.edges.map(
- edge => edge.node
- )
- : undefined
- }
+ productTypes={maybe(() =>
+ data.productTypes.edges.map(
+ edge => edge.node
+ )
+ )}
onAttributesEdit={
handleAttributesEdit
}