add Cart model + add store fot save all methods
This commit is contained in:
@@ -10,13 +10,9 @@ import { useCart } from '../hook/useCart';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useGetProfile } from '@/app/[name]/(Profile)/profile/hooks/userProfileData';
|
||||
import { useGetCartItems } from '../hooks/useCartData';
|
||||
import { useCartStore } from '../store/Store';
|
||||
|
||||
interface CartSummaryProps {
|
||||
description?: string;
|
||||
onDescriptionChange?: (value: string) => void;
|
||||
}
|
||||
|
||||
const CartSummary = ({ description = '', onDescriptionChange }: CartSummaryProps) => {
|
||||
const CartSummary = () => {
|
||||
const params = useParams();
|
||||
const router = useRouter();
|
||||
const pathname = usePathname();
|
||||
@@ -27,6 +23,7 @@ const CartSummary = ({ description = '', onDescriptionChange }: CartSummaryProps
|
||||
const { items } = useCart();
|
||||
const { data: cartData } = useGetCartItems(isSuccess);
|
||||
const { t } = useTranslation('parallels', { keyPrefix: 'Cart' });
|
||||
const { description, setDescription } = useCartStore();
|
||||
|
||||
const cartFoods = React.useMemo(() => {
|
||||
if (!foods.length) return [];
|
||||
@@ -72,7 +69,7 @@ const CartSummary = ({ description = '', onDescriptionChange }: CartSummaryProps
|
||||
id='description'
|
||||
name='description'
|
||||
value={description}
|
||||
onChange={(e) => onDescriptionChange?.(e.target.value)}
|
||||
onChange={(e) => setDescription(e.target.value)}
|
||||
></textarea>
|
||||
<span className='absolute top-4 right-2 px-2 bg-background text-foreground text-xs'></span>
|
||||
</div>
|
||||
|
||||
@@ -158,3 +158,9 @@ export const useGetCartItems = (enabled = true) => {
|
||||
enabled,
|
||||
});
|
||||
};
|
||||
|
||||
export const useSaveAllMethod = () => {
|
||||
return useMutation({
|
||||
mutationFn: api.saveAllMethod,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -13,7 +13,6 @@ const CartIndex = () => {
|
||||
const { t } = useTranslation('parallels', { keyPrefix: 'Cart' });
|
||||
const router = useRouter();
|
||||
const { clearCart } = useCart();
|
||||
const [description, setDescription] = React.useState('');
|
||||
const [showClearConfirm, setShowClearConfirm] = React.useState(false);
|
||||
|
||||
const handleClearCart = (e: React.MouseEvent) => {
|
||||
@@ -49,7 +48,7 @@ const CartIndex = () => {
|
||||
|
||||
<div className='flex overflow-y-auto noscrollbar flex-col h-full pt-4 gap-4 pb-24' dir='rtl'>
|
||||
<CartItemsList />
|
||||
<CartSummary description={description} onDescriptionChange={setDescription} />
|
||||
<CartSummary />
|
||||
</div>
|
||||
|
||||
<div className={showClearConfirm ? 'fixed inset-0 z-1001' : 'pointer-events-none fixed inset-0 z-1001'}>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { api } from "@/config/axios";
|
||||
import { BulkCartItem, CartResponse } from "../types/Types";
|
||||
import { BulkCartItem, CartResponse, SaveAllMethod } from "../types/Types";
|
||||
|
||||
export const bulkCart = async (params: BulkCartItem) => {
|
||||
const { data } = await api.post("/public/cart/items/bulk", params);
|
||||
@@ -30,3 +30,8 @@ export const getCartSummary = async () => {
|
||||
const { data } = await api.get("/public/cart/summary");
|
||||
return data;
|
||||
};
|
||||
|
||||
export const saveAllMethod = async (params: SaveAllMethod) => {
|
||||
const { data } = await api.patch("/public/cart/all", params);
|
||||
return data;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import { create } from "zustand";
|
||||
import { createJSONStorage, persist } from "zustand/middleware";
|
||||
import { CartStoreType, SaveAllMethod } from "../types/Types";
|
||||
|
||||
export const useCartStore = create<CartStoreType>()(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
description: "",
|
||||
setDescription: (value) => set({ description: value }),
|
||||
deliveryMethodId: null,
|
||||
setDeliveryMethodId: (value) => set({ deliveryMethodId: value }),
|
||||
addressId: null,
|
||||
setAddressId: (value) => set({ addressId: value }),
|
||||
paymentMethodId: null,
|
||||
setPaymentMethodId: (value) => set({ paymentMethodId: value }),
|
||||
carAddress: null,
|
||||
setCarAddress: (value) => set({ carAddress: value }),
|
||||
reset: () =>
|
||||
set({
|
||||
description: "",
|
||||
deliveryMethodId: null,
|
||||
addressId: null,
|
||||
paymentMethodId: null,
|
||||
carAddress: null,
|
||||
}),
|
||||
getSaveAllMethodData: (): SaveAllMethod | null => {
|
||||
const state = get();
|
||||
if (!state.deliveryMethodId || !state.paymentMethodId) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
description: state.description,
|
||||
deliveryMethodId: state.deliveryMethodId,
|
||||
...(state.addressId && { addressId: state.addressId }),
|
||||
paymentMethodId: state.paymentMethodId,
|
||||
...(state.carAddress && { carAddress: state.carAddress }),
|
||||
};
|
||||
},
|
||||
}),
|
||||
{
|
||||
name: "cart-storage",
|
||||
storage: createJSONStorage(() => localStorage),
|
||||
}
|
||||
)
|
||||
);
|
||||
@@ -85,3 +85,40 @@ export interface CartFood {
|
||||
}
|
||||
|
||||
export type CartResponse = BaseResponse<CartData>;
|
||||
|
||||
export type SaveAllMethod = {
|
||||
description: string;
|
||||
deliveryMethodId: string;
|
||||
addressId?: string;
|
||||
paymentMethodId: string;
|
||||
carAddress?: {
|
||||
carModel: string;
|
||||
carColor: string;
|
||||
plateNumber: string;
|
||||
};
|
||||
};
|
||||
|
||||
export type CartStoreType = {
|
||||
description: string;
|
||||
setDescription: (value: string) => void;
|
||||
deliveryMethodId: string | null;
|
||||
setDeliveryMethodId: (value: string | null) => void;
|
||||
addressId: string | null;
|
||||
setAddressId: (value: string | null) => void;
|
||||
paymentMethodId: string | null;
|
||||
setPaymentMethodId: (value: string | null) => void;
|
||||
carAddress: {
|
||||
carModel: string;
|
||||
carColor: string;
|
||||
plateNumber: string;
|
||||
} | null;
|
||||
setCarAddress: (
|
||||
value: {
|
||||
carModel: string;
|
||||
carColor: string;
|
||||
plateNumber: string;
|
||||
} | null
|
||||
) => void;
|
||||
reset: () => void;
|
||||
getSaveAllMethodData: () => SaveAllMethod | null;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user