import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { react2angular } from 'react2angular'; import { Loader } from '../base/loader'; import { IApplication } from '../../models/application'; import { FabButton } from '../base/fab-button'; import useCart from '../../hooks/use-cart'; import FormatLib from '../../lib/format'; import CartAPI from '../../api/cart'; import { User } from '../../models/user'; import { PaymentModal } from '../payment/stripe/payment-modal'; import { PaymentMethod } from '../../models/payment'; import { Order } from '../../models/order'; import { MemberSelect } from '../user/member-select'; import { CouponInput } from '../coupon/coupon-input'; declare const Application: IApplication; interface StoreCartProps { onError: (message: string) => void, currentUser?: User, } /** * This component shows user's cart */ const StoreCart: React.FC = ({ onError, currentUser }) => { const { t } = useTranslation('public'); const { cart, setCart } = useCart(currentUser); const [paymentModal, setPaymentModal] = useState(false); /** * Remove the product from cart */ const removeProductFromCart = (item) => { return (e: React.BaseSyntheticEvent) => { e.preventDefault(); e.stopPropagation(); CartAPI.removeItem(cart, item.orderable_id).then(data => { setCart(data); }).catch(onError); }; }; /** * Change product quantity */ const changeProductQuantity = (item) => { return (e: React.BaseSyntheticEvent) => { CartAPI.setQuantity(cart, item.orderable_id, e.target.value).then(data => { setCart(data); }).catch(onError); }; }; /** * Checkout cart */ const checkout = () => { setPaymentModal(true); }; /** * Open/closes the payment modal */ const togglePaymentModal = (): void => { setPaymentModal(!paymentModal); }; /** * Open/closes the payment modal */ const handlePaymentSuccess = (data: Order): void => { if (data.payment_state === 'paid') { setPaymentModal(false); window.location.href = '/#!/store'; } else { onError('Erreur inconnue after payment, please conntact admin'); } }; /** * Change cart's customer by admin/manger */ const handleChangeMember = (userId: number): void => { CartAPI.setCustomer(cart, userId).then(data => { setCart(data); }).catch(onError); }; /** * Check if the current operator has administrative rights or is a normal member */ const isPrivileged = (): boolean => { return (currentUser?.role === 'admin' || currentUser?.role === 'manager'); }; /** * Check if the current cart is empty ? */ const cartIsEmpty = (): boolean => { return cart && cart.order_items_attributes.length === 0; }; return (
{cart && cartIsEmpty() &&

{t('app.public.store_cart.cart_is_empty')}

} {cart && cart.order_items_attributes.map(item => (
{item.orderable_name}
{FormatLib.price(item.amount)}
{item.quantity}
{FormatLib.price(item.quantity * item.amount)}
))} {cart && !cartIsEmpty() && } {cart && !cartIsEmpty() &&

Totale: {FormatLib.price(cart.total)}

} {cart && !cartIsEmpty() && isPrivileged() && } {cart && !cartIsEmpty() && {t('app.public.store_cart.checkout')} } {cart && !cartIsEmpty() && cart.user &&
'dont need update shopping cart'} />
}
); }; const StoreCartWrapper: React.FC = (props) => { return ( ); }; Application.Components.component('storeCart', react2angular(StoreCartWrapper, ['onError', 'currentUser']));