summaryrefslogtreecommitdiff
path: root/src-migrate/modules/cart/stores/useCartStore.ts
blob: d211304a7018071227c46d6ce59601babae86b30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { create } from 'zustand';
import { CartItem, CartProps } from '~/types/cart';
import { getUserCart } from '~/services/cart';
import {
  syncCartWithCookie,
  getCartDataFromCookie,
  getSelectedItemsFromCookie,
  updateSelectedItemInCookie,
  setAllSelectedInCookie,
  removeCartItemsFromCookie,
  forceResetAllSelectedItems,
} from '~/utils/cart';

type State = {
  cart: CartProps | null;
  isLoadCart: boolean;
  summary: {
    subtotal: number;
    discount: number;
    total: number;
    tax: number;
    grandTotal: number;
  };
};

type Action = {
  loadCart: (userId: number) => Promise<void>;
  updateCartItem: (updateCart: CartProps) => void;
  syncCartWithCookieAndUpdate: (cart: CartProps) => { needsUpdate: boolean };
  forceResetSelection: () => void;
};

export const useCartStore = create<State & Action>((set, get) => ({
  cart: null,
  isLoadCart: false,
  summary: {
    subtotal: 0,
    discount: 0,
    total: 0,
    tax: 0,
    grandTotal: 0,
  },

  loadCart: async (userId) => {
    if (get().isLoadCart === true) return;

    set({ isLoadCart: true });
    try {
      // Fetch cart from API
      const cart: CartProps = (await getUserCart(userId)) as CartProps;

      // Sync with cookie and get updated data if needed
      const { needsUpdate } = get().syncCartWithCookieAndUpdate(cart);

      // If no update needed from cookie, just set the cart directly
      if (!needsUpdate) {
        set({ cart });
      }

      // Update summary with current cart
      const summary = computeSummary(get().cart!);
      set({ summary });
    } catch (error) {
      console.error('Failed to load cart from API:', error);

      // Fallback to cookie if API fails
      try {
        const cartData = getCartDataFromCookie();
        if (Object.keys(cartData).length > 0) {
          // Transform cart data from cookie to expected format
          const products = Object.values(cartData).map((item) => ({
            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,
            package_price: item.package_price,
            source: item.source,
          }));

          const fallbackCart: CartProps = {
            product_total: products.length,
            products,
          };

          set({ cart: fallbackCart });
          const summary = computeSummary(fallbackCart);
          set({ summary });
        }
      } catch (cookieError) {
        console.error('Failed to fallback to cookie:', cookieError);
      }
    } finally {
      set({ isLoadCart: false });
    }
  },

  updateCartItem: (updatedCart) => {
    const cart = get().cart;
    if (!cart) return;

    set({ cart: updatedCart });

    // Sync updated cart with cookie
    syncCartWithCookie(updatedCart);

    const summary = computeSummary(updatedCart);
    set({ summary });
  },

  syncCartWithCookieAndUpdate: (cart) => {
    if (!cart) return { needsUpdate: false };

    // Sync cart with cookie
    const result = syncCartWithCookie(cart);

    // If we need to update the cart based on cookie data
    if (result.needsUpdate && cart.products) {
      // Create updated cart with selections from cookie
      const selectedItems = getSelectedItemsFromCookie();

      const updatedCart = {
        ...cart,
        products: cart.products.map((item) => ({
          ...item,
          selected:
            selectedItems[item.id] !== undefined
              ? selectedItems[item.id]
              : item.selected,
        })),
      };

      // Update the store
      set({ cart: updatedCart });
    }

    return result;
  },

  forceResetSelection: () => {
    const { cart } = get();
    if (!cart) return;

    // Reset all selections in cookie
    forceResetAllSelectedItems();

    // Update the cart in state
    const updatedCart = {
      ...cart,
      products: cart.products.map((item) => ({
        ...item,
        selected: false,
      })),
    };

    set({ cart: updatedCart });

    // Update summary
    const summary = computeSummary(updatedCart);
    set({ summary });
  },
}));

const computeSummary = (cart: CartProps) => {
  let subtotal = 0;
  let discount = 0;

  const PPN: number = process.env.NEXT_PUBLIC_PPN
    ? parseFloat(process.env.NEXT_PUBLIC_PPN)
    : 0;

  for (const item of cart?.products) {
    if (!item.selected) continue;

    let price = 0;
    if (item.cart_type === 'promotion')
      price = (item?.package_price || 0) * item.quantity;
    else if (item.cart_type === 'product')
      price = item.price.price * item.quantity;

    subtotal += price;
    discount += price - item.price.price_discount * item.quantity;
  }
  let total = subtotal - discount;
  let grandTotal = total * PPN;
  let tax = grandTotal - total;
  // let grandTotal = total + tax;

  return { subtotal, discount, total, grandTotal, tax };
};