2022-08-20 20:49:51 +02:00
|
|
|
import React, { useEffect } from 'react';
|
2022-08-19 19:59:13 +02:00
|
|
|
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';
|
2022-08-20 20:49:51 +02:00
|
|
|
import { User } from '../../models/user';
|
2022-08-19 19:59:13 +02:00
|
|
|
|
|
|
|
declare const Application: IApplication;
|
|
|
|
|
|
|
|
interface StoreCartProps {
|
|
|
|
onError: (message: string) => void,
|
2022-08-20 20:49:51 +02:00
|
|
|
currentUser: User,
|
2022-08-19 19:59:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This component shows user's cart
|
|
|
|
*/
|
2022-08-20 20:49:51 +02:00
|
|
|
const StoreCart: React.FC<StoreCartProps> = ({ onError, currentUser }) => {
|
2022-08-19 19:59:13 +02:00
|
|
|
const { t } = useTranslation('public');
|
|
|
|
|
2022-08-20 20:49:51 +02:00
|
|
|
const { cart, setCart, reloadCart } = useCart();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (currentUser) {
|
|
|
|
reloadCart();
|
|
|
|
}
|
|
|
|
}, [currentUser]);
|
2022-08-19 19:59:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-08-20 18:47:15 +02:00
|
|
|
/**
|
|
|
|
* Change product quantity
|
|
|
|
*/
|
|
|
|
const changeProductQuantity = (item) => {
|
|
|
|
return (e: React.BaseSyntheticEvent) => {
|
|
|
|
CartAPI.setQuantity(cart, item.orderable_id, e.target.value).then(data => {
|
|
|
|
setCart(data);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checkout cart
|
|
|
|
*/
|
|
|
|
const checkout = () => {
|
|
|
|
console.log('checkout .....');
|
|
|
|
};
|
|
|
|
|
2022-08-19 19:59:13 +02:00
|
|
|
return (
|
|
|
|
<div className="store-cart">
|
|
|
|
{cart && cart.order_items_attributes.map(item => (
|
|
|
|
<div key={item.id}>
|
|
|
|
<div>{item.orderable_name}</div>
|
|
|
|
<div>{FormatLib.price(item.amount)}</div>
|
|
|
|
<div>{item.quantity}</div>
|
2022-08-20 18:47:15 +02:00
|
|
|
<select value={item.quantity} onChange={changeProductQuantity(item)}>
|
|
|
|
{Array.from({ length: 100 }, (_, i) => i + 1).map(v => (
|
|
|
|
<option key={v} value={v}>{v}</option>
|
|
|
|
))}
|
|
|
|
</select>
|
2022-08-19 19:59:13 +02:00
|
|
|
<div>{FormatLib.price(item.quantity * item.amount)}</div>
|
|
|
|
<FabButton className="delete-btn" onClick={removeProductFromCart(item)}>
|
2022-08-20 18:47:15 +02:00
|
|
|
<i className="fa fa-trash" />
|
2022-08-19 19:59:13 +02:00
|
|
|
</FabButton>
|
|
|
|
</div>
|
|
|
|
))}
|
2022-08-20 18:47:15 +02:00
|
|
|
{cart && <p>Totale: {FormatLib.price(cart.amount)}</p>}
|
|
|
|
<FabButton className="checkout-btn" onClick={checkout}>
|
|
|
|
{t('app.public.store_cart.checkout')}
|
|
|
|
</FabButton>
|
2022-08-19 19:59:13 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-08-20 20:49:51 +02:00
|
|
|
const StoreCartWrapper: React.FC<StoreCartProps> = (props) => {
|
2022-08-19 19:59:13 +02:00
|
|
|
return (
|
|
|
|
<Loader>
|
2022-08-20 20:49:51 +02:00
|
|
|
<StoreCart {...props} />
|
2022-08-19 19:59:13 +02:00
|
|
|
</Loader>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-08-20 20:49:51 +02:00
|
|
|
Application.Components.component('storeCart', react2angular(StoreCartWrapper, ['onError', 'currentUser']));
|