1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-18 07:52:23 +01:00

fix various bugs for cart

This commit is contained in:
Du Peng 2022-08-25 16:23:00 +02:00
parent 8a8ce607b7
commit d9687a007b
7 changed files with 50 additions and 29 deletions

View File

@ -9,10 +9,18 @@ class API::CartController < API::ApiController
def create
authorize :cart, :create?
p '-----------------'
p current_user
@order = Order.find_by(token: order_token)
if @order.nil? && current_user&.member?
@order = Order.where(statistic_profile_id: current_user.statistic_profile.id,
state: 'cart').last
if @order.nil?
if current_user&.member?
@order = Order.where(statistic_profile_id: current_user.statistic_profile.id,
state: 'cart').last
end
if current_user&.privileged?
@order = Order.where(operator_id: current_user.id,
state: 'cart').last
end
end
if @order
@order.update(statistic_profile_id: current_user.statistic_profile.id) if @order.statistic_profile_id.nil? && current_user&.member?

View File

@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { react2angular } from 'react2angular';
import { Loader } from '../base/loader';
@ -26,15 +26,9 @@ interface StoreCartProps {
const StoreCart: React.FC<StoreCartProps> = ({ onError, currentUser }) => {
const { t } = useTranslation('public');
const { cart, setCart, reloadCart } = useCart();
const { cart, setCart } = useCart(currentUser);
const [paymentModal, setPaymentModal] = useState<boolean>(false);
useEffect(() => {
if (currentUser) {
reloadCart();
}
}, [currentUser]);
/**
* Remove the product from cart
*/
@ -97,8 +91,16 @@ const StoreCart: React.FC<StoreCartProps> = ({ onError, currentUser }) => {
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 (
<div className="store-cart">
{cart && cartIsEmpty() && <p>{t('app.public.store_cart.cart_is_empty')}</p>}
{cart && cart.order_items_attributes.map(item => (
<div key={item.id}>
<div>{item.orderable_name}</div>
@ -115,23 +117,23 @@ const StoreCart: React.FC<StoreCartProps> = ({ onError, currentUser }) => {
</FabButton>
</div>
))}
{cart && cart.order_items_attributes.length > 0 && <p>Totale: {FormatLib.price(cart.amount)}</p>}
{cart && isPrivileged() && <MemberSelect defaultUser={cart.user} onSelected={handleChangeMember} />}
{cart &&
{cart && !cartIsEmpty() && <p>Totale: {FormatLib.price(cart.amount)}</p>}
{cart && !cartIsEmpty() && isPrivileged() && <MemberSelect defaultUser={cart.user} onSelected={handleChangeMember} />}
{cart && !cartIsEmpty() &&
<FabButton className="checkout-btn" onClick={checkout} disabled={!cart.user || cart.order_items_attributes.length === 0}>
{t('app.public.store_cart.checkout')}
</FabButton>
}
{cart && cart.order_items_attributes.length > 0 && cart.user && <div>
{cart && !cartIsEmpty() && cart.user && <div>
<PaymentModal isOpen={paymentModal}
toggleModal={togglePaymentModal}
afterSuccess={handlePaymentSuccess}
onError={onError}
cart={{ customer_id: currentUser.id, items: [], payment_method: PaymentMethod.Card }}
cart={{ customer_id: cart.user.id, items: [], payment_method: PaymentMethod.Card }}
order={cart}
operator={currentUser}
customer={cart.user}
updateCart={() => console.log('success')} />
updateCart={() => 'dont need update shopping cart'} />
</div>}
</div>
);

View File

@ -24,7 +24,7 @@ interface StoreProps {
const Store: React.FC<StoreProps> = ({ onError, currentUser }) => {
const { t } = useTranslation('public');
const { cart, setCart, reloadCart } = useCart();
const { cart, setCart } = useCart(currentUser);
const [products, setProducts] = useState<Array<Product>>([]);
@ -40,12 +40,6 @@ const Store: React.FC<StoreProps> = ({ onError, currentUser }) => {
emitCustomEvent('CartUpdate', cart);
}, [cart]);
useEffect(() => {
if (currentUser) {
reloadCart();
}
}, [currentUser]);
return (
<div className="store">
<div className='layout'>

View File

@ -2,8 +2,9 @@ import { useState, useEffect } from 'react';
import { Order } from '../models/order';
import CartAPI from '../api/cart';
import { getCartToken, setCartToken } from '../lib/cart-token';
import { User } from '../models/user';
export default function useCart () {
export default function useCart (user?: User) {
const [cart, setCart] = useState<Order>(null);
const [loading, setLoading] = useState<boolean>(true);
const [error, setError] = useState(null);
@ -33,5 +34,11 @@ export default function useCart () {
setLoading(false);
};
useEffect(() => {
if (user && cart && (!cart.statistic_profile_id || !cart.operator_id)) {
reloadCart();
}
}, [user]);
return { loading, cart, error, setCart, reloadCart };
}

View File

@ -2,10 +2,12 @@
json.extract! order, :id, :token, :statistic_profile_id, :operator_id, :reference, :state, :created_at
json.amount order.amount / 100.0 if order.amount.present?
json.user do
json.extract! order&.statistic_profile&.user, :id
json.role order&.statistic_profile&.user&.roles&.first&.name
json.name order&.statistic_profile&.user&.profile&.full_name
if order&.statistic_profile&.user
json.user do
json.id order.statistic_profile.user.id
json.role order.statistic_profile.user.roles.first.name
json.name order.statistic_profile.user.profile.full_name
end
end
json.order_items_attributes order.order_items do |item|

View File

@ -391,6 +391,10 @@ en:
my_cart: "My Cart"
store_cart:
checkout: "Checkout"
cart_is_empty: "Your cart is empty"
member_select:
select_a_member: "Select a member"
start_typing: "Start typing..."
tour:
conclusion:
title: "Thank you for your attention"

View File

@ -391,6 +391,10 @@ fr:
my_cart: "Mon Panier"
store_cart:
checkout: "Valider mon panier"
cart_is_empty: "Votre panier est vide"
member_select:
select_a_member: "Sélectionnez un membre"
start_typing: "Commencez à écrire..."
tour:
conclusion:
title: "Merci de votre attention"