diff options
| author | Miqdad <ahmadmiqdad27@gmail.com> | 2025-06-11 14:00:19 +0700 |
|---|---|---|
| committer | Miqdad <ahmadmiqdad27@gmail.com> | 2025-06-11 14:00:19 +0700 |
| commit | 65cc2af8ba7bf6ced66572cdec779bef8cace564 (patch) | |
| tree | 4ca7e0ee59da9b61a2999d116bb8ab28ff675472 /src-migrate/modules/cart/stores/useCartStore.ts | |
| parent | 54636b53685422d495adce95b491f7b10585cb5c (diff) | |
| parent | 5669295b8cff1a9c9e559dd263599123a2ad6e92 (diff) | |
Merge branch 'new-release' of https://bitbucket.org/altafixco/next-indoteknik into biteship-merge
Diffstat (limited to 'src-migrate/modules/cart/stores/useCartStore.ts')
| -rw-r--r-- | src-migrate/modules/cart/stores/useCartStore.ts | 107 |
1 files changed, 71 insertions, 36 deletions
diff --git a/src-migrate/modules/cart/stores/useCartStore.ts b/src-migrate/modules/cart/stores/useCartStore.ts index dc47b011..be48b1ed 100644 --- a/src-migrate/modules/cart/stores/useCartStore.ts +++ b/src-migrate/modules/cart/stores/useCartStore.ts @@ -8,24 +8,32 @@ import { forceResetAllSelectedItems, } from '~/utils/cart'; -type State = { +interface Summary { + subtotal: number; + discount: number; + total: number; + tax: number; + grandTotal: number; +} + +interface SyncResult { + cartData?: Record<string, any>; + selectedItems?: Record<number, boolean>; + needsUpdate: boolean; +} + +interface State { cart: CartProps | null; isLoadCart: boolean; - summary: { - subtotal: number; - discount: number; - total: number; - tax: number; - grandTotal: number; - }; -}; + summary: Summary; +} -type Action = { +interface Action { loadCart: (userId: number) => Promise<void>; updateCartItem: (updateCart: CartProps) => void; forceResetSelection: () => void; clearCart: () => void; -}; +} export const useCartStore = create<State & Action>((set, get) => ({ cart: null, @@ -38,7 +46,7 @@ export const useCartStore = create<State & Action>((set, get) => ({ grandTotal: 0, }, - loadCart: async (userId) => { + loadCart: async (userId: number): Promise<void> => { if (get().isLoadCart) return; set({ isLoadCart: true }); @@ -47,12 +55,15 @@ export const useCartStore = create<State & Action>((set, get) => ({ const cart: CartProps = (await getUserCart(userId)) as CartProps; // Sync with cookie data - const syncResult = syncCartWithCookie(cart); + const syncResult = syncCartWithCookie(cart) as SyncResult; if (syncResult?.needsUpdate && cart.products) { - const selectedItems = getSelectedItemsFromCookie(); + const selectedItems = getSelectedItemsFromCookie() as Record< + number, + boolean + >; - const updatedCart = { + const updatedCart: CartProps = { ...cart, products: cart.products.map((item) => ({ ...item, @@ -81,7 +92,7 @@ export const useCartStore = create<State & Action>((set, get) => ({ } }, - updateCartItem: (updatedCart) => { + updateCartItem: (updatedCart: CartProps): void => { set({ cart: updatedCart }); syncCartWithCookie(updatedCart); @@ -89,13 +100,13 @@ export const useCartStore = create<State & Action>((set, get) => ({ set({ summary }); }, - forceResetSelection: () => { + forceResetSelection: (): void => { const { cart } = get(); if (!cart) return; forceResetAllSelectedItems(); - const updatedCart = { + const updatedCart: CartProps = { ...cart, products: cart.products.map((item) => ({ ...item, selected: false })), }; @@ -106,7 +117,7 @@ export const useCartStore = create<State & Action>((set, get) => ({ set({ summary }); }, - clearCart: () => { + clearCart: (): void => { set({ cart: null, summary: { @@ -121,13 +132,15 @@ export const useCartStore = create<State & Action>((set, get) => ({ })); // Helper function for cookie fallback -const handleFallbackFromCookie = async () => { +const handleFallbackFromCookie = async (): Promise<void> => { try { - const cartData = getCartDataFromCookie(); + const cartData = getCartDataFromCookie() as Record<string, any>; if (Object.keys(cartData).length === 0) return; - const products = Object.values(cartData).map(transformCookieItemToProduct); + const products: CartItem[] = Object.values(cartData).map( + transformCookieItemToProduct + ); const fallbackCart: CartProps = { product_total: products.length, @@ -145,27 +158,47 @@ const handleFallbackFromCookie = async () => { // Helper function to transform cookie item to product format const transformCookieItemToProduct = (item: any): CartItem => ({ + image_program: item.image_program || '', cart_id: item.cart_id, - id: item.id, - cart_type: item.cart_type, - product_id: item.product?.id, - product_name: item.product?.name, - program_line_id: item.program_line?.id, - program_line_name: item.program_line?.name, quantity: item.quantity, selected: item.selected, - price: item.price, + can_buy: true, + cart_type: item.cart_type, + id: item.id, + name: item.product?.name || item.program_line?.name || '', + stock: 0, + is_in_bu: false, + on_hand_qty: 0, + available_quantity: 0, + weight: 0, + attributes: [], + parent: { + id: 0, + name: '', + image: '', + }, + price: item.price || { + price: 0, + discount_percentage: 0, + price_discount: 0, + }, + manufacture: { + id: 0, + name: '', + }, + has_flashsale: false, + subtotal: 0, + code: item.code, + image: item.image, package_price: item.package_price, - source: item.source, }); -// Helper function to compute cart summary -const computeSummary = (cart: CartProps) => { +const computeSummary = (cart: CartProps): Summary => { if (!cart?.products) { return { subtotal: 0, discount: 0, total: 0, grandTotal: 0, tax: 0 }; } - const PPN = parseFloat(process.env.NEXT_PUBLIC_PPN || '0'); + const PPN = parseFloat(process.env.NEXT_PUBLIC_PPN || '1.11'); let subtotal = 0; let discount = 0; @@ -182,8 +215,10 @@ const computeSummary = (cart: CartProps) => { } const total = subtotal - discount; - const grandTotal = total * (1 + PPN); - const tax = grandTotal - total; + + // PERBAIKAN: + const tax = total * (PPN - 1); + const grandTotal = total + tax; return { subtotal, discount, total, grandTotal, tax }; -};
\ No newline at end of file +}; |
