Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/typings/product.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type ProductType = {
id: string;
name: string;
price: number;
stock: number[][]; // stock[colorway][size] = qty
stock: { [colorway: string]: { [sizeIndex: string]: number } }; // stock[colorway][size] = qty
sizes: string[];
colorways: string[];
images?: string[];
Expand Down
46 changes: 18 additions & 28 deletions src/utils/functions/stock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import { ProductType } from "../../typings/product"

export const getQtyInStock = (product: ProductType, colorway: string, size: string): number => {
// returns remaining stock for specified colorway and size
const colorwayIndex = product.colorways.indexOf(colorway);
const sizeIndex = product.sizes.indexOf(size);
if (colorwayIndex !== -1 && sizeIndex !== -1) {
return product.stock[colorwayIndex][sizeIndex];
if (product.stock[colorway] && product.stock[colorway][size]) {
return product.stock[colorway][size]
}
return 0;
}

export const displayStock = (product: ProductType, colorway: string, size: string): string => {
// returns string describing remaining stock
const colorwayIndex = product.colorways.indexOf(colorway);
const sizeIndex = product.sizes.indexOf(size);
if (colorwayIndex !== -1 && sizeIndex !== -1) {
const qty = product.stock[colorwayIndex][sizeIndex];
if (product.stock[colorway] && product.stock[colorway][size]) {
const qty = product.stock[colorway][size];
if (qty > 0) {
return `${qty} available`;
}
Expand All @@ -27,33 +23,27 @@ export const displayStock = (product: ProductType, colorway: string, size: strin

export const isOutOfStock = (product: ProductType): boolean => {
// returns true if product is out of stock in all colorways and sizes
const totalQty = product.stock.reduce((a,b) => { return a.concat(b) }) // flatten array
.reduce((a,b) => { return a + b }); // sum elements
return (totalQty <= 0);
const totalQty = Object.values(product.stock).reduce((acc, stockByColorway)=>{
const colorQty = Object.values(stockByColorway).reduce((acc2, qty)=>acc2+qty, 0);
return acc+colorQty
}, 0);
return totalQty <= 0;

}

export const isColorwayAvailable = (product: ProductType, colorway: string): boolean => {
// returns true if colorway is available in any size
// returns false if colorway is out of stock in all sizes
const index = product.colorways.indexOf(colorway);
if (index === -1) { // no such colorway
return false;
}
const colorwayStock = product.stock[index];
const totalQty = colorwayStock.reduce((a, b) => {
return a + b;
}, 0);
return (totalQty > 0);
}
const colorwayStock = Object.values(product.stock[colorway]).reduce(
(acc: any, size: any)=>acc+size, 0
);
return (colorwayStock > 0);
}

export const isSizeAvailable = (product: ProductType, size: string): boolean => {
// returns true if size is available in any colorway
// returns false if size is out of stock in all colorways
const index = product.sizes.indexOf(size);
if (index === -1) { // no such size
return false;
}
const sizeStock = product.stock.map(d => d[index]);
const sizeStock = Object.values(product.stock).map(d => d[size]||0);
const totalQty = sizeStock.reduce((a, b) => {
return a + b;
}, 0);
Expand All @@ -73,7 +63,7 @@ export const getDefaultColorway = (product: ProductType, size: string): string =
if (sizeIndex === -1) { // no such size
return "";
}
const colorwayStock = product.stock.map(d => d[sizeIndex]);
const colorwayStock = Object.values(product.stock).map(d => d[sizeIndex]);
const availColorwayIndex = colorwayStock.map((qty, idx) => qty > 0 ? idx : -1).filter(idx => idx !== -1);
if (availColorwayIndex.length > 0) {
return product.colorways[availColorwayIndex[0]];
Expand All @@ -85,4 +75,4 @@ export const getDefaults = (product: ProductType): [string, string] => {
const size = getDefaultSize(product);
const colorway = getDefaultColorway(product, size);
return [colorway, size];
}
}