Add util functions
This commit is contained in:
parent
e4505cf427
commit
422ede89a8
2 changed files with 42 additions and 22 deletions
|
@ -17,6 +17,7 @@ import { ProductDetails_product_productType_variantAttributes } from "@saleor/pr
|
||||||
import CardTitle from "@saleor/components/CardTitle";
|
import CardTitle from "@saleor/components/CardTitle";
|
||||||
import CardSpacer from "@saleor/components/CardSpacer";
|
import CardSpacer from "@saleor/components/CardSpacer";
|
||||||
import { ProductVariantCreateFormData } from "./form";
|
import { ProductVariantCreateFormData } from "./form";
|
||||||
|
import { getPriceAttributeValues, getStockAttributeValues } from "./utils";
|
||||||
|
|
||||||
const useStyles = makeStyles(
|
const useStyles = makeStyles(
|
||||||
theme => ({
|
theme => ({
|
||||||
|
@ -69,28 +70,8 @@ const ProductVariantCreatorPrices: React.FC<ProductVariantCreatorPricesProps> =
|
||||||
label: attribute.name,
|
label: attribute.name,
|
||||||
value: attribute.id
|
value: attribute.id
|
||||||
}));
|
}));
|
||||||
const priceAttributeValues = data.price.all
|
const priceAttributeValues = getPriceAttributeValues(data, attributes);
|
||||||
? null
|
const stockAttributeValues = getStockAttributeValues(data, attributes);
|
||||||
: data.price.attribute
|
|
||||||
? attributes
|
|
||||||
.find(attribute => attribute.id === data.price.attribute)
|
|
||||||
.values.filter(value =>
|
|
||||||
data.attributes
|
|
||||||
.find(attribute => attribute.id === data.price.attribute)
|
|
||||||
.values.includes(value.slug)
|
|
||||||
)
|
|
||||||
: [];
|
|
||||||
const stockAttributeValues = data.stock.all
|
|
||||||
? null
|
|
||||||
: data.stock.attribute
|
|
||||||
? attributes
|
|
||||||
.find(attribute => attribute.id === data.stock.attribute)
|
|
||||||
.values.filter(value =>
|
|
||||||
data.attributes
|
|
||||||
.find(attribute => attribute.id === data.stock.attribute)
|
|
||||||
.values.includes(value.slug)
|
|
||||||
)
|
|
||||||
: [];
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|
39
src/products/components/ProductVariantCreatorPage/utils.ts
Normal file
39
src/products/components/ProductVariantCreatorPage/utils.ts
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
import {
|
||||||
|
ProductDetails_product_productType_variantAttributes_values,
|
||||||
|
ProductDetails_product_productType_variantAttributes
|
||||||
|
} from "@saleor/products/types/ProductDetails";
|
||||||
|
import { ProductVariantCreateFormData } from "./form";
|
||||||
|
|
||||||
|
export function getPriceAttributeValues(
|
||||||
|
data: ProductVariantCreateFormData,
|
||||||
|
attributes: ProductDetails_product_productType_variantAttributes[]
|
||||||
|
): ProductDetails_product_productType_variantAttributes_values[] {
|
||||||
|
return data.price.all
|
||||||
|
? null
|
||||||
|
: data.price.attribute
|
||||||
|
? attributes
|
||||||
|
.find(attribute => attribute.id === data.price.attribute)
|
||||||
|
.values.filter(value =>
|
||||||
|
data.attributes
|
||||||
|
.find(attribute => attribute.id === data.price.attribute)
|
||||||
|
.values.includes(value.slug)
|
||||||
|
)
|
||||||
|
: [];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getStockAttributeValues(
|
||||||
|
data: ProductVariantCreateFormData,
|
||||||
|
attributes: ProductDetails_product_productType_variantAttributes[]
|
||||||
|
): ProductDetails_product_productType_variantAttributes_values[] {
|
||||||
|
return data.stock.all
|
||||||
|
? null
|
||||||
|
: data.stock.attribute
|
||||||
|
? attributes
|
||||||
|
.find(attribute => attribute.id === data.stock.attribute)
|
||||||
|
.values.filter(value =>
|
||||||
|
data.attributes
|
||||||
|
.find(attribute => attribute.id === data.stock.attribute)
|
||||||
|
.values.includes(value.slug)
|
||||||
|
)
|
||||||
|
: [];
|
||||||
|
}
|
Loading…
Reference in a new issue