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'; import { Coupon } from '../../models/coupon'; import { computePriceWithCoupon } from '../../lib/coupon'; import noImage from '../../../../images/no_image.png'; declare const Application: IApplication; interface StoreCartProps { onSuccess: (message: string) => void, onError: (message: string) => void, userLogin: () => void, currentUser?: User } /** * This component shows user's cart */ const StoreCart: React.FC = ({ onSuccess, onError, currentUser, userLogin }) => { const { t } = useTranslation('public'); const { cart, setCart } = useCart(currentUser); console.log('cart: ', cart); 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 = () => { if (!currentUser) { userLogin(); } else { setPaymentModal(true); } }; /** * Open/closes the payment modal */ const togglePaymentModal = (): void => { setPaymentModal(!paymentModal); }; /** * Handle payment */ const handlePaymentSuccess = (data: Order): void => { if (data.payment_state === 'paid') { setPaymentModal(false); window.location.href = '/#!/store'; onSuccess(t('app.public.store_cart.checkout_success')); } else { onError(t('app.public.store_cart.checkout_error')); } }; /** * Change cart's customer by admin/manger */ const handleChangeMember = (userId: number): void => { setCart({ ...cart, user: { id: userId, role: 'member' } }); }; /** * 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; }; /** * Apply coupon to current cart */ const applyCoupon = (coupon?: Coupon): void => { if (coupon !== cart.coupon) { setCart({ ...cart, coupon }); } }; /** * Get the offered item total */ const offeredAmount = (): number => { return cart.order_items_attributes .filter(i => i.is_offered) .map(i => i.amount) .reduce((acc, curr) => acc + curr, 0); }; return (
{cart && cartIsEmpty() &&

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

} {cart && cart.order_items_attributes.map(item => (
{t('app.public.store_cart.reference_short')}

{item.orderable_name}

{FormatLib.price(item.amount)}

/ {t('app.public.store_cart.unit')}
{t('app.public.store_cart.total')}

{FormatLib.price(item.quantity * item.amount)}

))}

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

[TODO: texte venant des paramètres de la boutique…]

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