diff --git a/app/frontend/src/javascript/components/cart/abstract-item.tsx b/app/frontend/src/javascript/components/cart/abstract-item.tsx index ede6f95df..611575b1c 100644 --- a/app/frontend/src/javascript/components/cart/abstract-item.tsx +++ b/app/frontend/src/javascript/components/cart/abstract-item.tsx @@ -4,16 +4,21 @@ import FormatLib from '../../lib/format'; import OrderLib from '../../lib/order'; import { FabButton } from '../base/fab-button'; import Switch from 'react-switch'; -import type { OrderItem } from '../../models/order'; +import type { ItemError, OrderItem } from '../../models/order'; import { useTranslation } from 'react-i18next'; import { ReactNode } from 'react'; +import { Order } from '../../models/order'; +import CartAPI from '../../api/cart'; interface AbstractItemProps { item: OrderItem, - hasError: boolean, + errors: Array, + cart: Order, + setCart: (cart: Order) => void, + reloadCart: () => Promise, + onError: (message: string) => void, className?: string, - removeItemFromCart: (item: OrderItem) => void, - toggleItemOffer: (item: OrderItem, checked: boolean) => void, + offerItemLabel?: string, privilegedOperator: boolean, actions?: ReactNode } @@ -21,7 +26,7 @@ interface AbstractItemProps { /** * This component shares the common code for items in the cart (product, cart-item, etc) */ -export const AbstractItem: React.FC = ({ item, hasError, className, removeItemFromCart, toggleItemOffer, privilegedOperator, actions, children }) => { +export const AbstractItem: React.FC = ({ item, errors, cart, setCart, reloadCart, onError, className, offerItemLabel, privilegedOperator, actions, children }) => { const { t } = useTranslation('public'); /** @@ -32,7 +37,13 @@ export const AbstractItem: React.FC = ({ item, hasError, clas e.preventDefault(); e.stopPropagation(); - removeItemFromCart(item); + if (errors.length === 1 && errors[0].error === 'not_found') { + reloadCart().catch(onError); + } else { + CartAPI.removeItem(cart, item.orderable_id, item.orderable_type).then(data => { + setCart(data); + }).catch(onError); + } }; }; @@ -40,11 +51,21 @@ export const AbstractItem: React.FC = ({ item, hasError, clas * Return the callback triggered when the privileged user enable/disable the offered attribute for the given item */ const handleToggleOffer = (item: OrderItem) => { - return (checked: boolean) => toggleItemOffer(item, checked); + return (checked: boolean) => { + CartAPI.setOffer(cart, item.orderable_id, item.orderable_type, checked).then(data => { + setCart(data); + }).catch(e => { + if (e.match(/code 403/)) { + onError(t('app.public.abstract_item.errors.unauthorized_offering_product')); + } else { + onError(e); + } + }); + }; }; return ( -
+
0 ? 'error' : ''}`}>
@@ -62,7 +83,7 @@ export const AbstractItem: React.FC = ({ item, hasError, clas {privilegedOperator &&