Fix undefined and null objects handling
This commit is contained in:
parent
16d9d28033
commit
a34d22de8f
5 changed files with 167 additions and 163 deletions
|
@ -128,7 +128,11 @@ export const ProductCreatePage: React.FC<ProductCreatePageProps> = ({
|
||||||
taxTypes={taxTypeChoices}
|
taxTypes={taxTypeChoices}
|
||||||
warehouses={warehouses}
|
warehouses={warehouses}
|
||||||
>
|
>
|
||||||
{({ change, data, handlers, hasChanged, submit }) => (
|
{({ change, data, handlers, hasChanged, submit }) => {
|
||||||
|
// Comparing explicitly to false because `hasVariants` can be undefined
|
||||||
|
const isSimpleProduct = data.productType?.hasVariants === false;
|
||||||
|
|
||||||
|
return (
|
||||||
<Container>
|
<Container>
|
||||||
<AppHeader onBack={onBack}>
|
<AppHeader onBack={onBack}>
|
||||||
{intl.formatMessage(sectionNames.products)}
|
{intl.formatMessage(sectionNames.products)}
|
||||||
|
@ -154,7 +158,7 @@ export const ProductCreatePage: React.FC<ProductCreatePageProps> = ({
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
<CardSpacer />
|
<CardSpacer />
|
||||||
{!data.productType?.hasVariants && (
|
{isSimpleProduct && (
|
||||||
<>
|
<>
|
||||||
<ProductShipping
|
<ProductShipping
|
||||||
data={data}
|
data={data}
|
||||||
|
@ -272,7 +276,8 @@ export const ProductCreatePage: React.FC<ProductCreatePageProps> = ({
|
||||||
disabled={disabled || !onSubmit || !hasChanged}
|
disabled={disabled || !onSubmit || !hasChanged}
|
||||||
/>
|
/>
|
||||||
</Container>
|
</Container>
|
||||||
)}
|
);
|
||||||
|
}}
|
||||||
</ProductCreateForm>
|
</ProductCreateForm>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
export { default } from "./ProductCreatePage";
|
export { default } from "./ProductCreatePage";
|
||||||
export * from "./ProductCreatePage";
|
export * from "./ProductCreatePage";
|
||||||
|
export * from "./form";
|
||||||
|
|
|
@ -127,9 +127,10 @@ const ProductStocks: React.FC<ProductStocksProps> = ({
|
||||||
const anchor = React.useRef<HTMLDivElement>();
|
const anchor = React.useRef<HTMLDivElement>();
|
||||||
const [isExpanded, setExpansionState] = React.useState(false);
|
const [isExpanded, setExpansionState] = React.useState(false);
|
||||||
|
|
||||||
const warehousesToAssign = warehouses.filter(
|
const warehousesToAssign =
|
||||||
|
warehouses?.filter(
|
||||||
warehouse => !stocks.some(stock => stock.id === warehouse.id)
|
warehouse => !stocks.some(stock => stock.id === warehouse.id)
|
||||||
);
|
) || [];
|
||||||
const formErrors = getFormErrors(["sku"], errors);
|
const formErrors = getFormErrors(["sku"], errors);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -187,7 +188,7 @@ const ProductStocks: React.FC<ProductStocksProps> = ({
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</Typography>
|
</Typography>
|
||||||
{!warehouses.length && (
|
{!warehouses?.length && (
|
||||||
<Typography color="textSecondary" className={classes.noWarehouseInfo}>
|
<Typography color="textSecondary" className={classes.noWarehouseInfo}>
|
||||||
{hasVariants ? (
|
{hasVariants ? (
|
||||||
<>
|
<>
|
||||||
|
@ -219,7 +220,7 @@ const ProductStocks: React.FC<ProductStocksProps> = ({
|
||||||
</Typography>
|
</Typography>
|
||||||
)}
|
)}
|
||||||
</CardContent>
|
</CardContent>
|
||||||
{warehouses.length > 0 && (
|
{warehouses?.length > 0 && (
|
||||||
<Table>
|
<Table>
|
||||||
<TableHead>
|
<TableHead>
|
||||||
<TableRow>
|
<TableRow>
|
||||||
|
|
|
@ -90,13 +90,12 @@ const ProductVariantPage: React.FC<ProductVariantPageProps> = ({
|
||||||
const [isModalOpened, setModalStatus] = React.useState(false);
|
const [isModalOpened, setModalStatus] = React.useState(false);
|
||||||
const toggleModal = () => setModalStatus(!isModalOpened);
|
const toggleModal = () => setModalStatus(!isModalOpened);
|
||||||
|
|
||||||
const variantImages = variant?.images?.map(image => image.id) || [];
|
const variantImages = variant?.images?.map(image => image.id);
|
||||||
const productImages =
|
const productImages = variant?.product?.images?.sort((prev, next) =>
|
||||||
variant?.product?.images?.sort((prev, next) =>
|
|
||||||
prev.sortOrder > next.sortOrder ? 1 : -1
|
prev.sortOrder > next.sortOrder ? 1 : -1
|
||||||
) || [];
|
);
|
||||||
const images = productImages
|
const images = productImages
|
||||||
.filter(image => variantImages.indexOf(image.id) !== -1)
|
?.filter(image => variantImages.indexOf(image.id) !== -1)
|
||||||
.sort((prev, next) => (prev.sortOrder > next.sortOrder ? 1 : -1));
|
.sort((prev, next) => (prev.sortOrder > next.sortOrder ? 1 : -1));
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { storiesOf } from "@storybook/react";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
import ProductCreatePage, {
|
import ProductCreatePage, {
|
||||||
ProductCreatePageSubmitData
|
ProductCreateFormData
|
||||||
} from "../../../products/components/ProductCreatePage";
|
} from "../../../products/components/ProductCreatePage";
|
||||||
import { product as productFixture } from "../../../products/fixtures";
|
import { product as productFixture } from "../../../products/fixtures";
|
||||||
import { productTypes } from "../../../productTypes/fixtures";
|
import { productTypes } from "../../../productTypes/fixtures";
|
||||||
|
@ -74,8 +74,7 @@ storiesOf("Views / Products / Create product", module)
|
||||||
"productType",
|
"productType",
|
||||||
"category",
|
"category",
|
||||||
"sku"
|
"sku"
|
||||||
] as Array<keyof ProductCreatePageSubmitData | "attributes">).map(
|
] as Array<keyof ProductCreateFormData | "attributes">).map(field => ({
|
||||||
field => ({
|
|
||||||
__typename: "ProductError",
|
__typename: "ProductError",
|
||||||
attributes:
|
attributes:
|
||||||
field === "attributes"
|
field === "attributes"
|
||||||
|
@ -83,8 +82,7 @@ storiesOf("Views / Products / Create product", module)
|
||||||
: null,
|
: null,
|
||||||
code: ProductErrorCode.INVALID,
|
code: ProductErrorCode.INVALID,
|
||||||
field
|
field
|
||||||
})
|
}))}
|
||||||
)}
|
|
||||||
header="Add product"
|
header="Add product"
|
||||||
collections={product.collections}
|
collections={product.collections}
|
||||||
fetchCategories={() => undefined}
|
fetchCategories={() => undefined}
|
||||||
|
@ -94,7 +92,7 @@ storiesOf("Views / Products / Create product", module)
|
||||||
fetchMoreCollections={fetchMoreProps}
|
fetchMoreCollections={fetchMoreProps}
|
||||||
fetchMoreProductTypes={fetchMoreProps}
|
fetchMoreProductTypes={fetchMoreProps}
|
||||||
initial={{
|
initial={{
|
||||||
productType: productTypes[0].id
|
productType: productTypes[0]
|
||||||
}}
|
}}
|
||||||
productTypes={productTypes}
|
productTypes={productTypes}
|
||||||
categories={[product.category]}
|
categories={[product.category]}
|
||||||
|
|
Loading…
Reference in a new issue