Skip to content

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

Fix variant create page

dominik-zeglen1d1d3c3ace55
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx b/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx
index 4d1307133f..4ad8dc52a8 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantCreatePage/ProductVariantCreatePage.tsx
@@ -8,6 +8,8 @@ import Form from "@saleor/components/Form";
import Grid from "@saleor/components/Grid";
import PageHeader from "@saleor/components/PageHeader";
import SaveButtonBar from "@saleor/components/SaveButtonBar";
+import useFormset, { FormsetChange } from "@saleor/hooks/useFormset";
+import { getVariantAttributeInputFromProduct } from "@saleor/products/utils/data";
import i18n from "../../../i18n";
import { maybe } from "../../../misc";
import { UserError } from "../../../types";
@@ -18,11 +20,6 @@ import ProductVariantPrice from "../ProductVariantPrice";
import ProductVariantStock from "../ProductVariantStock";
interface FormData {
- attributes: Array<{
- name: string;
- slug: string;
- values: string[];
- }>;
costPrice: string;
images: string[];
priceOverride: string;
@@ -53,6 +50,14 @@ const ProductVariantCreatePage: React.FC<ProductVariantCreatePageProps> = ({
onSubmit,
onVariantClick
}) => {
+ const attributeInput = React.useMemo(
+ () => getVariantAttributeInputFromProduct(product),
+ [product]
+ );
+ const { change: changeAttributeData, data: attributes } = useFormset(
+ attributeInput
+ );
+
const initialForm = {
attributes: maybe(
() =>
@@ -71,60 +76,66 @@ const ProductVariantCreatePage: React.FC<ProductVariantCreatePageProps> = ({
};
return (
<Form initial={initialForm} errors={formErrors} onSubmit={onSubmit}>
- {({ change, data, errors, hasChanged, submit }) => (
- <Container>
- <AppHeader onBack={onBack}>{maybe(() => product.name)}</AppHeader>
- <PageHeader title={header} />
- <Grid variant="inverted">
- <div>
- <ProductVariantNavigation
- fallbackThumbnail={maybe(() => product.thumbnail.url)}
- variants={maybe(() => product.variants)}
- onRowClick={(variantId: string) => {
- if (product && product.variants) {
- return onVariantClick(variantId);
- }
- }}
- />
- </div>
- <div>
- <ProductVariantAttributes
- attributes={maybe(() => product.productType.variantAttributes)}
- data={data}
- disabled={loading}
- onChange={change}
- />
- <CardSpacer />
- <ProductVariantPrice
- errors={errors}
- priceOverride={data.priceOverride}
- currencySymbol={currencySymbol}
- costPrice={data.costPrice}
- loading={loading}
- onChange={change}
- />
- <CardSpacer />
- <ProductVariantStock
- errors={errors}
- sku={data.sku}
- quantity={data.quantity}
- loading={loading}
- onChange={change}
- />
- </div>
- </Grid>
- <SaveButtonBar
- disabled={loading || !onSubmit || !hasChanged}
- labels={{
- delete: i18n.t("Remove variant"),
- save: i18n.t("Save variant")
- }}
- state={saveButtonBarState}
- onCancel={onBack}
- onSave={submit}
- />
- </Container>
- )}
+ {({ change, data, errors, hasChanged, submit, triggerChange }) => {
+ const handleAttributeChange: FormsetChange = (id, value) => {
+ changeAttributeData(id, value);
+ triggerChange();
+ };
+
+ return (
+ <Container>
+ <AppHeader onBack={onBack}>{maybe(() => product.name)}</AppHeader>
+ <PageHeader title={header} />
+ <Grid variant="inverted">
+ <div>
+ <ProductVariantNavigation
+ fallbackThumbnail={maybe(() => product.thumbnail.url)}
+ variants={maybe(() => product.variants)}
+ onRowClick={(variantId: string) => {
+ if (product && product.variants) {
+ return onVariantClick(variantId);
+ }
+ }}
+ />
+ </div>
+ <div>
+ <ProductVariantAttributes
+ attributes={attributes}
+ disabled={loading}
+ onChange={handleAttributeChange}
+ />
+ <CardSpacer />
+ <ProductVariantPrice
+ errors={errors}
+ priceOverride={data.priceOverride}
+ currencySymbol={currencySymbol}
+ costPrice={data.costPrice}
+ loading={loading}
+ onChange={change}
+ />
+ <CardSpacer />
+ <ProductVariantStock
+ errors={errors}
+ sku={data.sku}
+ quantity={data.quantity}
+ loading={loading}
+ onChange={change}
+ />
+ </div>
+ </Grid>
+ <SaveButtonBar
+ disabled={loading || !onSubmit || !hasChanged}
+ labels={{
+ delete: i18n.t("Remove variant"),
+ save: i18n.t("Save variant")
+ }}
+ state={saveButtonBarState}
+ onCancel={onBack}
+ onSave={submit}
+ />
+ </Container>
+ );
+ }}
</Form>
);
};
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx b/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx
index 166c7b7b56..5eccc60801 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantPage/ProductVariantPage.tsx
@@ -26,8 +26,8 @@ import ProductVariantPrice from "../ProductVariantPrice";
import ProductVariantStock from "../ProductVariantStock";
export interface ProductVariantPageFormData {
- costPrice: number;
- priceOverride: number;
+ costPrice: string;
+ priceOverride: string;
quantity: number;
sku: string;
}
@@ -90,8 +90,8 @@ const ProductVariantPage: React.FC<ProductVariantPageProps> = ({
);
const initialForm: ProductVariantPageFormData = {
- costPrice: maybe(() => variant.costPrice.amount, 0),
- priceOverride: maybe(() => variant.priceOverride.amount, 0),
+ costPrice: maybe(() => variant.costPrice.amount.toString(), ""),
+ priceOverride: maybe(() => variant.priceOverride.amount.toString(), ""),
quantity: maybe(() => variant.quantity, 0),
sku: maybe(() => variant.sku, "")
};
diff --git a/saleor/static/dashboard-next/products/components/ProductVariantPrice/ProductVariantPrice.tsx b/saleor/static/dashboard-next/products/components/ProductVariantPrice/ProductVariantPrice.tsx
index b0453d1da3..077a4b49de 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariantPrice/ProductVariantPrice.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariantPrice/ProductVariantPrice.tsx
@@ -23,8 +23,8 @@ const styles = (theme: Theme) =>
interface ProductVariantPriceProps extends WithStyles<typeof styles> {
currencySymbol?: string;
- priceOverride?: number;
- costPrice?: number;
+ priceOverride?: string;
+ costPrice?: string;
errors: { [key: string]: string };
loading?: boolean;
onChange(event: any);
diff --git a/saleor/static/dashboard-next/products/utils/data.ts b/saleor/static/dashboard-next/products/utils/data.ts
index 5061da71f2..4b8638c8bf 100644
--- a/saleor/static/dashboard-next/products/utils/data.ts
+++ b/saleor/static/dashboard-next/products/utils/data.ts
@@ -12,6 +12,7 @@ import { ProductAttributeInput } from "../components/ProductAttributes";
import { VariantAttributeInput } from "../components/ProductVariantAttributes";
import { ProductCreateData_productTypes_edges_node_productAttributes } from "../types/ProductCreateData";
import { ProductVariant } from "../types/ProductVariant";
+import { ProductVariantCreateData_product } from "../types/ProductVariantCreateData";
export interface Collection {
id: string;
@@ -99,6 +100,21 @@ export function getAttributeInputFromVariant(
);
}
+export function getVariantAttributeInputFromProduct(
+ product: ProductVariantCreateData_product
+): VariantAttributeInput[] {
+ return maybe(() =>
+ product.productType.variantAttributes.map(attribute => ({
+ data: {
+ values: attribute.values
+ },
+ id: attribute.id,
+ label: attribute.name,
+ value: ""
+ }))
+ );
+}
+
export function getCollectionInput(
productCollections: ProductDetails_product_collections[]
): Collection[] {