From 8c75b5fdd43ff30a942dc5bf65cfa63b24e91773 Mon Sep 17 00:00:00 2001 From: Du Peng Date: Fri, 16 Sep 2022 16:50:05 +0200 Subject: [PATCH] (bug) product stock status and quantity min to cart --- .../javascript/components/cart/store-cart.tsx | 3 ++- .../components/store/product-item.tsx | 2 +- .../components/store/store-product-item.tsx | 6 ++--- .../components/store/store-product.tsx | 24 ++++++++++++------- app/frontend/src/javascript/models/order.ts | 1 + app/views/api/orders/_order.json.jbuilder | 1 + 6 files changed, 23 insertions(+), 14 deletions(-) diff --git a/app/frontend/src/javascript/components/cart/store-cart.tsx b/app/frontend/src/javascript/components/cart/store-cart.tsx index c30749490..da1404418 100644 --- a/app/frontend/src/javascript/components/cart/store-cart.tsx +++ b/app/frontend/src/javascript/components/cart/store-cart.tsx @@ -1,6 +1,7 @@ import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { react2angular } from 'react2angular'; +import _ from 'lodash'; import { Loader } from '../base/loader'; import { IApplication } from '../../models/application'; import { FabButton } from '../base/fab-button'; @@ -151,7 +152,7 @@ const StoreCart: React.FC = ({ onSuccess, onError, currentUser, / {t('app.public.store_cart.unit')} diff --git a/app/frontend/src/javascript/components/store/product-item.tsx b/app/frontend/src/javascript/components/store/product-item.tsx index 9c85219ab..177f460e7 100644 --- a/app/frontend/src/javascript/components/store/product-item.tsx +++ b/app/frontend/src/javascript/components/store/product-item.tsx @@ -49,7 +49,7 @@ export const ProductItem: React.FC = ({ product, onEdit, onDel * Returns CSS class from stock status */ const statusColor = (product: Product) => { - if (product.stock.external === 0 && product.stock.internal === 0) { + if (product.stock.external < 1 && product.stock.internal < 1) { return 'out-of-stock'; } if (product.low_stock_threshold && (product.stock.external < product.low_stock_threshold || product.stock.internal < product.low_stock_threshold)) { diff --git a/app/frontend/src/javascript/components/store/store-product-item.tsx b/app/frontend/src/javascript/components/store/store-product-item.tsx index 4545c22cd..fef8d883a 100644 --- a/app/frontend/src/javascript/components/store/store-product-item.tsx +++ b/app/frontend/src/javascript/components/store/store-product-item.tsx @@ -53,10 +53,10 @@ export const StoreProductItem: React.FC = ({ product, car * Returns CSS class from stock status */ const statusColor = (product: Product) => { - if (product.stock.external === 0 && product.stock.internal === 0) { + if (product.stock.external < (product.quantity_min || 1)) { return 'out-of-stock'; } - if (product.low_stock_alert) { + if (product.low_stock_threshold && product.stock.external < product.low_stock_threshold) { return 'low'; } return ''; @@ -90,7 +90,7 @@ export const StoreProductItem: React.FC = ({ product, car {productStockStatus(product)} - {product.stock.external > 0 && + {product.stock.external > (product.quantity_min || 1) && } className="main-action-btn" onClick={addProductToCart}> {t('app.public.store_product_item.add')} diff --git a/app/frontend/src/javascript/components/store/store-product.tsx b/app/frontend/src/javascript/components/store/store-product.tsx index cccca03aa..ac02b0931 100644 --- a/app/frontend/src/javascript/components/store/store-product.tsx +++ b/app/frontend/src/javascript/components/store/store-product.tsx @@ -68,10 +68,10 @@ export const StoreProduct: React.FC = ({ productSlug, current * Returns CSS class from stock status */ const statusColor = (product: Product) => { - if (product.stock.external === 0 && product.stock.internal === 0) { + if (product.stock.external < (product.quantity_min || 1)) { return 'out-of-stock'; } - if (product.low_stock_alert) { + if (product.low_stock_threshold && product.stock.external < product.low_stock_threshold) { return 'low'; } }; @@ -80,7 +80,7 @@ export const StoreProduct: React.FC = ({ productSlug, current * Return product's stock status */ const productStockStatus = (product: Product) => { - if (product.stock.external === 0) { + if (product.stock.external < (product.quantity_min || 1)) { return {t('app.public.store_product_item.out_of_stock')}; } if (product.low_stock_threshold && product.stock.external < product.low_stock_threshold) { @@ -95,7 +95,9 @@ export const StoreProduct: React.FC = ({ productSlug, current const setCount = (type: 'add' | 'remove') => { switch (type) { case 'add': - setToCartCount(toCartCount + 1); + if (toCartCount < product.stock.external) { + setToCartCount(toCartCount + 1); + } break; case 'remove': if (toCartCount > product.quantity_min) { @@ -116,10 +118,12 @@ export const StoreProduct: React.FC = ({ productSlug, current * Add product to cart */ const addToCart = () => { - CartAPI.addItem(cart, product.id, toCartCount).then(data => { - setCart(data); - onSuccess(t('app.public.store.add_to_cart_success')); - }).catch(onError); + if (toCartCount <= product.stock.external) { + CartAPI.addItem(cart, product.id, toCartCount).then(data => { + setCart(data); + onSuccess(t('app.public.store.add_to_cart_success')); + }).catch(onError); + } }; if (product) { @@ -179,11 +183,13 @@ export const StoreProduct: React.FC = ({ productSlug, current

{FormatLib.price(product.amount)} TTC

/ {t('app.public.store_product_item.unit')} - {product.stock.external > 0 && + {product.stock.external > (product.quantity_min || 1) &&
setCount('remove')} icon={} className="minus" /> typeCount(evt)} /> setCount('add')} icon={} className="plus" /> addToCart()} icon={} diff --git a/app/frontend/src/javascript/models/order.ts b/app/frontend/src/javascript/models/order.ts index 5b0380421..cf986e86a 100644 --- a/app/frontend/src/javascript/models/order.ts +++ b/app/frontend/src/javascript/models/order.ts @@ -32,6 +32,7 @@ export interface Order { orderable_name: string, orderable_ref?: string, orderable_main_image_url?: string, + orderable_external_stock: number, quantity: number, quantity_min: number, amount: number, diff --git a/app/views/api/orders/_order.json.jbuilder b/app/views/api/orders/_order.json.jbuilder index 7bc6ca3e0..7c4836fae 100644 --- a/app/views/api/orders/_order.json.jbuilder +++ b/app/views/api/orders/_order.json.jbuilder @@ -27,6 +27,7 @@ json.order_items_attributes order.order_items.order(created_at: :asc) do |item| json.orderable_name item.orderable.name json.orderable_ref item.orderable.sku json.orderable_main_image_url item.orderable.main_image&.attachment_url + json.orderable_external_stock item.orderable.stock['external'] json.quantity item.quantity json.quantity_min item.orderable.quantity_min json.amount item.amount / 100.0