From ee36d6046c7c88b32980f31593f20b87a51dd72e Mon Sep 17 00:00:00 2001 From: dominik-zeglen Date: Fri, 7 Feb 2020 17:44:42 +0100 Subject: [PATCH] Add variant stock filtering by warehouse --- src/components/LinkChoice/LinkChoice.tsx | 6 +- .../ProductVariants/ProductVariants.tsx | 117 +++++++++++++++--- src/products/queries.ts | 4 + src/products/types/Product.ts | 7 ++ src/products/types/ProductCreate.ts | 7 ++ src/products/types/ProductDetails.ts | 7 ++ src/products/types/ProductImageCreate.ts | 7 ++ src/products/types/ProductImageUpdate.ts | 7 ++ src/products/types/ProductUpdate.ts | 7 ++ src/products/types/SimpleProductUpdate.ts | 7 ++ 10 files changed, 160 insertions(+), 16 deletions(-) diff --git a/src/components/LinkChoice/LinkChoice.tsx b/src/components/LinkChoice/LinkChoice.tsx index 9cfe19663..8916ea2fa 100644 --- a/src/components/LinkChoice/LinkChoice.tsx +++ b/src/components/LinkChoice/LinkChoice.tsx @@ -53,13 +53,15 @@ const useStyles = makeStyles( ); export interface LinkChoiceProps { + className?: string; choices: SingleAutocompleteChoiceType[]; - name: "country"; + name?: string; value: string; onChange: FormChange; } const LinkChoice: React.FC = ({ + className, choices, name, value, @@ -107,7 +109,7 @@ const LinkChoice: React.FC = ({ return ( ( + (warehouses, variant) => [ + ...warehouses, + ...variant.stock.reduce< + ProductDetails_product_variants_stock_warehouse[] + >((variantStocks, stock) => { + if (!!warehouses.find(w => w.id === stock.warehouse.id)) { + return variantStocks; + } + + return [...variantStocks, stock.warehouse]; + }, []) + ], + [] + ) + .map(w => ({ + label: w.name, + value: w.id + })) + ]; +} + const useStyles = makeStyles( theme => ({ [theme.breakpoints.up("lg")]: { @@ -57,6 +97,13 @@ const useStyles = makeStyles( }, textRight: { textAlign: "right" as "right" + }, + warehouseLabel: { + display: "inline-block", + marginRight: theme.spacing() + }, + warehouseSelectContainer: { + paddingTop: theme.spacing(2) } }), { name: "ProductVariants" } @@ -90,6 +137,7 @@ export const ProductVariants: React.FC = props => { const classes = useStyles(props); const intl = useIntl(); + const [warehouse, setWarehouse] = React.useState(null); const hasVariants = maybe(() => variants.length > 0, true); return ( @@ -127,7 +175,23 @@ export const ProductVariants: React.FC = props => { ) } /> - {!variants.length && ( + + {variants.length > 0 ? ( + + + + + setWarehouse(event.target.value)} + /> + + ) : ( @@ -146,7 +210,7 @@ export const ProductVariants: React.FC = props => { > @@ -175,6 +239,9 @@ export const ProductVariants: React.FC = props => { variant && variant.stock ? variant.stock.reduce((acc, s) => acc + s.quantity, 0) : null; + const variantStock = variant.stock.find( + s => s.warehouse.id === warehouse + ); return ( = props => { > {numAvailable === null ? ( - ) : numAvailable === 0 ? ( - + ) : warehouse === null ? ( + numAvailable === 0 ? ( + + ) : ( + + ) + ) : !!variantStock ? ( + variantStock.quantity > 0 ? ( + + ) : ( + + ) ) : ( )} diff --git a/src/products/queries.ts b/src/products/queries.ts index 5e5f58977..1e292428b 100644 --- a/src/products/queries.ts +++ b/src/products/queries.ts @@ -149,6 +149,10 @@ export const productFragmentDetails = gql` stock { id quantity + warehouse { + id + name + } } } productType { diff --git a/src/products/types/Product.ts b/src/products/types/Product.ts index e0cafa652..0dfd6a748 100644 --- a/src/products/types/Product.ts +++ b/src/products/types/Product.ts @@ -127,10 +127,17 @@ export interface Product_variants_priceOverride { currency: string; } +export interface Product_variants_stock_warehouse { + __typename: "Warehouse"; + id: string; + name: string; +} + export interface Product_variants_stock { __typename: "Stock"; id: string; quantity: number; + warehouse: Product_variants_stock_warehouse; } export interface Product_variants { diff --git a/src/products/types/ProductCreate.ts b/src/products/types/ProductCreate.ts index b1409b4d8..e22239d5a 100644 --- a/src/products/types/ProductCreate.ts +++ b/src/products/types/ProductCreate.ts @@ -133,10 +133,17 @@ export interface ProductCreate_productCreate_product_variants_priceOverride { currency: string; } +export interface ProductCreate_productCreate_product_variants_stock_warehouse { + __typename: "Warehouse"; + id: string; + name: string; +} + export interface ProductCreate_productCreate_product_variants_stock { __typename: "Stock"; id: string; quantity: number; + warehouse: ProductCreate_productCreate_product_variants_stock_warehouse; } export interface ProductCreate_productCreate_product_variants { diff --git a/src/products/types/ProductDetails.ts b/src/products/types/ProductDetails.ts index 78013430e..a880cdccc 100644 --- a/src/products/types/ProductDetails.ts +++ b/src/products/types/ProductDetails.ts @@ -127,10 +127,17 @@ export interface ProductDetails_product_variants_priceOverride { currency: string; } +export interface ProductDetails_product_variants_stock_warehouse { + __typename: "Warehouse"; + id: string; + name: string; +} + export interface ProductDetails_product_variants_stock { __typename: "Stock"; id: string; quantity: number; + warehouse: ProductDetails_product_variants_stock_warehouse; } export interface ProductDetails_product_variants { diff --git a/src/products/types/ProductImageCreate.ts b/src/products/types/ProductImageCreate.ts index 45f62c860..8efec0618 100644 --- a/src/products/types/ProductImageCreate.ts +++ b/src/products/types/ProductImageCreate.ts @@ -133,10 +133,17 @@ export interface ProductImageCreate_productImageCreate_product_variants_priceOve currency: string; } +export interface ProductImageCreate_productImageCreate_product_variants_stock_warehouse { + __typename: "Warehouse"; + id: string; + name: string; +} + export interface ProductImageCreate_productImageCreate_product_variants_stock { __typename: "Stock"; id: string; quantity: number; + warehouse: ProductImageCreate_productImageCreate_product_variants_stock_warehouse; } export interface ProductImageCreate_productImageCreate_product_variants { diff --git a/src/products/types/ProductImageUpdate.ts b/src/products/types/ProductImageUpdate.ts index 0be498717..c98fe2909 100644 --- a/src/products/types/ProductImageUpdate.ts +++ b/src/products/types/ProductImageUpdate.ts @@ -133,10 +133,17 @@ export interface ProductImageUpdate_productImageUpdate_product_variants_priceOve currency: string; } +export interface ProductImageUpdate_productImageUpdate_product_variants_stock_warehouse { + __typename: "Warehouse"; + id: string; + name: string; +} + export interface ProductImageUpdate_productImageUpdate_product_variants_stock { __typename: "Stock"; id: string; quantity: number; + warehouse: ProductImageUpdate_productImageUpdate_product_variants_stock_warehouse; } export interface ProductImageUpdate_productImageUpdate_product_variants { diff --git a/src/products/types/ProductUpdate.ts b/src/products/types/ProductUpdate.ts index 7fce89379..3fab87f99 100644 --- a/src/products/types/ProductUpdate.ts +++ b/src/products/types/ProductUpdate.ts @@ -133,10 +133,17 @@ export interface ProductUpdate_productUpdate_product_variants_priceOverride { currency: string; } +export interface ProductUpdate_productUpdate_product_variants_stock_warehouse { + __typename: "Warehouse"; + id: string; + name: string; +} + export interface ProductUpdate_productUpdate_product_variants_stock { __typename: "Stock"; id: string; quantity: number; + warehouse: ProductUpdate_productUpdate_product_variants_stock_warehouse; } export interface ProductUpdate_productUpdate_product_variants { diff --git a/src/products/types/SimpleProductUpdate.ts b/src/products/types/SimpleProductUpdate.ts index c49bd09a6..c7ffb7b9f 100644 --- a/src/products/types/SimpleProductUpdate.ts +++ b/src/products/types/SimpleProductUpdate.ts @@ -133,10 +133,17 @@ export interface SimpleProductUpdate_productUpdate_product_variants_priceOverrid currency: string; } +export interface SimpleProductUpdate_productUpdate_product_variants_stock_warehouse { + __typename: "Warehouse"; + id: string; + name: string; +} + export interface SimpleProductUpdate_productUpdate_product_variants_stock { __typename: "Stock"; id: string; quantity: number; + warehouse: SimpleProductUpdate_productUpdate_product_variants_stock_warehouse; } export interface SimpleProductUpdate_productUpdate_product_variants {