Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 85x 85x 85x 85x 85x 85x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 63x 63x 63x 63x 63x 63x 3x 63x 60x 60x 60x 60x 60x 60x 60x 60x 63x 63x 63x 61x 61x 61x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 14x 14x 15x 15x 15x 12x 12x 12x 1x 1x 1x 10x 10x 10x 10x 3x 3x 3x 1x 1x 1x 1x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 4x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 36x 36x 2x 2x 36x 36x 2x 2x 2x 2x 36x 36x 1x 1x 1x 36x 1x 1x 1x 1x 1x 11x 11x 14x 11x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { createAsyncThunk, createSelector, createSlice, PayloadAction } from "@reduxjs/toolkit";
import { RootState } from "../store";
import { setCartCookie, deleteCartCookie } from "@/lib/cart";
import * as cartApi from "@/lib/api/cart";
type InitialState = {
items: CartItem[];
isAnonymous: boolean;
isLoaded: boolean;
syncStatus: 'idle' | 'syncing';
lastSyncTime: number | null;
loading: boolean;
error: string | null;
};
export type CartItem = {
id: number;
title: string;
price: number;
discountedPrice: number;
quantity: number;
imgs?: {
thumbnails: string[];
previews: string[];
};
};
const initialState: InitialState = {
items: [],
isAnonymous: true,
isLoaded: false,
syncStatus: 'idle',
lastSyncTime: null,
loading: false,
error: null};
/**
* Helper to ensure items is always an array
* Handles edge cases where state might be corrupted or undefined
*/
function ensureItemsArray(state: InitialState): CartItem[] {
// Handle case where state itself might be malformed
if (!state || typeof state !== 'object') {
return [];
}
if (!Array.isArray(state.items)) {
state.items = [];
}
return state.items;
}
// Async thunk for fetching cart from API
export const fetchCart = createAsyncThunk(
"cart/fetchCart",
async (_, { rejectWithValue }) => {
try {
const cart = await cartApi.getCart();
return cart;
} catch (error) {
return rejectWithValue(error instanceof Error ? error.message : 'Failed to fetch cart');
}
}
);
export const cart = createSlice({
name: "cart",
initialState,
reducers: {
addItemToCart: (state, action: PayloadAction<CartItem>) => {
const items = ensureItemsArray(state);
const { id, title, price, quantity, discountedPrice, imgs } =
action.payload;
const existingItem = items.find((item) => item.id === id);
if (existingItem) {
existingItem.quantity += quantity;
} else {
items.push({
id,
title,
price,
quantity,
discountedPrice,
imgs});
}
// Sync to cookie if anonymous
if (state.isAnonymous) {
setCartCookie(items);
state.lastSyncTime = Date.now();
}
},
removeItemFromCart: (state, action: PayloadAction<number>) => {
ensureItemsArray(state);
const itemId = action.payload;
state.items = state.items.filter((item) => item.id !== itemId);
// Sync to cookie if anonymous
if (state.isAnonymous) {
setCartCookie(state.items);
state.lastSyncTime = Date.now();
}
},
updateCartItemQuantity: (
state,
action: PayloadAction<{ id: number; quantity: number }>
) => {
const items = ensureItemsArray(state);
const { id, quantity } = action.payload;
const existingItem = items.find((item) => item.id === id);
if (existingItem) {
existingItem.quantity = quantity;
}
// Sync to cookie if anonymous
if (state.isAnonymous) {
setCartCookie(items);
state.lastSyncTime = Date.now();
}
},
removeAllItemsFromCart: (state) => {
state.items = [];
// Clear cookie if anonymous
if (state.isAnonymous) {
deleteCartCookie();
state.lastSyncTime = Date.now();
}
},
// New actions for cart persistence
initializeAnonymousCart: (state, action: PayloadAction<CartItem[]>) => {
state.items = Array.isArray(action.payload) ? action.payload : [];
state.isAnonymous = true;
state.isLoaded = true;
state.lastSyncTime = Date.now();
},
initializeUserCart: (state, action: PayloadAction<CartItem[]>) => {
state.items = Array.isArray(action.payload) ? action.payload : [];
state.isAnonymous = false;
state.isLoaded = true;
state.lastSyncTime = Date.now();
},
syncAnonymousCartToCookie: (state) => {
if (state.isAnonymous) {
const items = ensureItemsArray(state);
setCartCookie(items);
state.lastSyncTime = Date.now();
}
},
clearAnonymousCart: (state) => {
deleteCartCookie();
state.items = [];
state.lastSyncTime = Date.now();
},
setCartLoaded: (state, action: PayloadAction<boolean>) => {
state.isLoaded = action.payload;
},
setCartSyncStatus: (state, action: PayloadAction<'idle' | 'syncing'>) => {
state.syncStatus = action.payload;
},
setIsAnonymous: (state, action: PayloadAction<boolean>) => {
state.isAnonymous = action.payload;
},
clearCart: (state) => {
state.items = [];
}},
extraReducers: (builder) => {
// Fetch cart
builder.addCase(fetchCart.pending, (state) => {
state.loading = true;
state.error = null;
});
builder.addCase(fetchCart.fulfilled, (state, action) => {
state.loading = false;
state.items = Array.isArray(action.payload) ? action.payload : [];
state.isLoaded = true;
state.lastSyncTime = Date.now();
});
builder.addCase(fetchCart.rejected, (state, action) => {
state.loading = false;
state.error = action.payload as string;
state.isLoaded = true;
});
}});
export const selectCartItems = (state: RootState) => state.cartReducer?.items ?? [];
export const selectTotalPrice = createSelector([selectCartItems], (items) => {
if (!Array.isArray(items)) return 0;
return items.reduce((total, item) => {
return total + item.discountedPrice * item.quantity;
}, 0);
});
export const selectTotalItemCount = createSelector([selectCartItems], (items) => {
if (!Array.isArray(items)) return 0;
return items.reduce((total, item) => {
return total + item.quantity;
}, 0);
});
export const {
addItemToCart,
removeItemFromCart,
updateCartItemQuantity,
removeAllItemsFromCart,
initializeAnonymousCart,
initializeUserCart,
syncAnonymousCartToCookie,
clearAnonymousCart,
setCartLoaded,
setCartSyncStatus,
setIsAnonymous,
clearCart} = cart.actions;
// Additional selectors
export const selectIsAnonymous = (state: RootState) => state.cartReducer?.isAnonymous ?? true;
export const selectIsCartLoaded = (state: RootState) => state.cartReducer?.isLoaded ?? false;
export const selectCartSyncStatus = (state: RootState) => state.cartReducer?.syncStatus ?? 'idle';
export const selectLastSyncTime = (state: RootState) => state.cartReducer?.lastSyncTime ?? null;
export default cart.reducer;
|