1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-17 06:52:27 +01:00

(feat) always check cart items when user in cart page

This commit is contained in:
Du Peng 2022-09-28 12:14:58 +02:00
parent c7559e3857
commit 274d661a58
5 changed files with 32 additions and 18 deletions

View File

@ -35,7 +35,7 @@ interface StoreCartProps {
const StoreCart: React.FC<StoreCartProps> = ({ onSuccess, onError, currentUser, userLogin }) => {
const { t } = useTranslation('public');
const { cart, setCart } = useCart(currentUser);
const { cart, setCart, reloadCart } = useCart(currentUser);
const [cartErrors, setCartErrors] = useState<OrderErrors>(null);
const [itemsQuantity, setItemsQuantity] = useState<{ id: number; quantity: number; }[]>();
const [paymentModal, setPaymentModal] = useState<boolean>(false);
@ -45,6 +45,9 @@ const StoreCart: React.FC<StoreCartProps> = ({ onSuccess, onError, currentUser,
return { id: i.id, quantity: i.quantity };
});
setItemsQuantity(quantities);
if (cart) {
checkCart();
}
}, [cart]);
/**
@ -54,10 +57,14 @@ const StoreCart: React.FC<StoreCartProps> = ({ onSuccess, onError, currentUser,
return (e: React.BaseSyntheticEvent) => {
e.preventDefault();
e.stopPropagation();
CartAPI.removeItem(cart, item.orderable_id).then(data => {
setCart(data);
checkCart();
}).catch(onError);
const errors = getItemErrors(item);
if (errors.length === 1 && errors[0].error === 'not_found') {
reloadCart().catch(onError);
} else {
CartAPI.removeItem(cart, item.orderable_id).then(data => {
setCart(data);
}).catch(onError);
}
};
};
@ -68,7 +75,6 @@ const StoreCart: React.FC<StoreCartProps> = ({ onSuccess, onError, currentUser,
CartAPI.setQuantity(cart, item.orderable_id, e.target.value)
.then(data => {
setCart(data);
checkCart();
})
.catch(() => onError(t('app.public.store_cart.stock_limit')));
};
@ -77,7 +83,6 @@ const StoreCart: React.FC<StoreCartProps> = ({ onSuccess, onError, currentUser,
CartAPI.setQuantity(cart, item.orderable_id, direction === 'up' ? item.quantity + 1 : item.quantity - 1)
.then(data => {
setCart(data);
checkCart();
})
.catch(() => onError(t('app.public.store_cart.stock_limit')));
};
@ -91,7 +96,6 @@ const StoreCart: React.FC<StoreCartProps> = ({ onSuccess, onError, currentUser,
e.stopPropagation();
CartAPI.refreshItem(cart, item.orderable_id).then(data => {
setCart(data);
checkCart();
}).catch(onError);
};
};
@ -138,7 +142,7 @@ const StoreCart: React.FC<StoreCartProps> = ({ onSuccess, onError, currentUser,
const getItemErrors = (item) => {
if (!cartErrors) return [];
const errors = _.find(cartErrors.details, (e) => e.item_id === item.id);
return errors.errors || [{ error: 'not_found' }];
return errors?.errors || [{ error: 'not_found' }];
};
/**
@ -207,21 +211,21 @@ const StoreCart: React.FC<StoreCartProps> = ({ onSuccess, onError, currentUser,
*/
const itemError = (item, error) => {
if (error.error === 'is_active' || error.error === 'not_found') {
return <div className='error'>This product is not available, please remove it in cart</div>;
return <div className='error'>{t('app.public.store_cart.errors.product_not_found')}</div>;
}
if (error.error === 'stock' && error.value === 0) {
return <div className='error'>This product is out of stock. Please remove this item to before checkout the cart.</div>;
return <div className='error'>{t('app.public.store_cart.errors.out_of_stock')}</div>;
}
if (error.error === 'stock' && error.value > 0) {
return <div className='error'>This product has only {error.value} unit in stock, please change the quantity of item.</div>;
return <div className='error'>{t('app.public.store_cart.errors.stock_limit_QUANTITY', { QUANTITY: error.value })}</div>;
}
if (error.error === 'quantity_min') {
return <div className='error'>Minimum number of product was changed to {error.value}, please change the quantity of item</div>;
return <div className='error'>{t('app.public.store_cart.errors.quantity_min_QUANTITY', { QUANTITY: error.value })}</div>;
}
if (error.error === 'amount') {
return <div className='error'>
The price of product was modified to {FormatLib.price(error.value)} / {t('app.public.store_cart.unit')}
<span onClick={refreshItem(item)}>Update</span>
{t('app.public.store_cart.errors.price_changed_PRICE', { PRICE: `${FormatLib.price(error.value)} / ${t('app.public.store_cart.unit')}` })}
<span onClick={refreshItem(item)}>{t('app.public.store_cart.update_item')}</span>
</div>;
}
};

View File

@ -66,7 +66,7 @@ export const StoreProductItem: React.FC<StoreProductItemProps> = ({ product, car
* Return product's stock status
*/
const productStockStatus = (product: Product) => {
if (product.stock.external < (product.quantity_min || 1)) {
if (product.stock.external === 0 || product.stock.external < (product.quantity_min || 1)) {
return <span>{t('app.public.store_product_item.out_of_stock')}</span>;
}
if (product.low_stock_threshold && product.stock.external < product.low_stock_threshold) {

View File

@ -7,7 +7,10 @@ class Cart::CheckCartService
order.order_items.each do |item|
errors = []
errors.push({ error: 'is_active', value: false }) unless item.orderable.is_active
errors.push({ error: 'stock', value: item.orderable.stock['external'] }) if item.quantity > item.orderable.stock['external']
if item.quantity > item.orderable.stock['external'] || item.orderable.stock['external'] < item.orderable.quantity_min
value = item.orderable.stock['external'] < item.orderable.quantity_min ? 0 : item.orderable.stock['external']
errors.push({ error: 'stock', value: value })
end
orderable_amount = item.orderable.amount || 0
errors.push({ error: 'amount', value: orderable_amount / 100.0 }) if item.amount != orderable_amount
errors.push({ error: 'quantity_min', value: item.orderable.quantity_min }) if item.quantity < item.orderable.quantity_min

View File

@ -44,7 +44,7 @@ class Orders::OrderService
def in_stock?(order, stock_type = 'external')
order.order_items.each do |item|
return false if item.orderable.stock[stock_type] < item.quantity
return false if item.orderable.stock[stock_type] < item.quantity || item.orderable.stock[stock_type] < item.orderable.quantity_min
end
true
end

View File

@ -434,6 +434,13 @@ en:
checkout_total: "Cart total"
checkout_error: "An unexpected error occurred. Please contact the administrator."
checkout_success: "Purchase confirmed. Thanks!"
update_item: "Update"
errors:
product_not_found: "This product is not available, please remove it in cart."
out_of_stock: "This product is out of stock. Please remove this item to before checkout the cart."
stock_limit_QUANTITY: "This product has only {QUANTITY} {QUANTITY, plural, =1{unit} other{units}} in stock, please change the quantity of item."
quantity_min_QUANTITY: "Minimum number of product was changed to {QUANTITY}, please change the quantity of item."
price_changed_PRICE: "The price of product was modified to {PRICE}"
orders_dashboard:
heading: "My orders"
sort: