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 | 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 17x 17x 10x 17x 6x 6x 17x 1x 1x 1x 1x 1x 7x 7x 2x 2x 2x 7x 4x 4x 7x 1x 1x 1x 1x 1x 6x 6x 2x 2x 2x 6x 3x 3x 6x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 19x 19x 19x 19x 19x 19x 19x 19x 19x 1x 1x 5x 5x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 17x 17x 1x 1x 10x 10x 1x 1x 6x 6x 1x 1x 1x 1x 7x 7x 1x 1x 2x 2x 1x 1x 4x 4x 1x 1x 1x 1x 6x 6x 1x 1x 2x 2x 1x 1x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x | import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit";
import { RootState } from "../store";
import * as wishlistApi from "@/lib/api/wishlist";
type InitialState = {
items: WishListItem[];
loading: boolean;
error: string | null;
};
export type WishListItem = {
id: number;
title: string;
price: number;
discountedPrice: number;
quantity?: number;
status?: string;
reviews?: number;
averageRating?: number;
imgs?: {
thumbnails: string[];
previews: string[];
};
};
const initialState: InitialState = {
items: [],
loading: false,
error: null};
// Async thunks for API calls
export const fetchWishlist = createAsyncThunk(
"wishlist/fetchWishlist",
async (_, { rejectWithValue }) => {
try {
const wishlist = await wishlistApi.getWishlist();
return wishlist;
} catch (error) {
return rejectWithValue(error instanceof Error ? error.message : 'Failed to fetch wishlist');
}
}
);
export const addToWishlistAsync = createAsyncThunk(
"wishlist/addToWishlist",
async (productId: number, { rejectWithValue }) => {
try {
await wishlistApi.addToWishlist(productId);
// Re-fetch wishlist to get updated state
const wishlist = await wishlistApi.getWishlist();
return wishlist;
} catch (error) {
return rejectWithValue(error instanceof Error ? error.message : 'Failed to add to wishlist');
}
}
);
export const removeFromWishlistAsync = createAsyncThunk(
"wishlist/removeFromWishlist",
async (productId: number, { rejectWithValue }) => {
try {
await wishlistApi.removeFromWishlist(productId);
// Re-fetch wishlist to get updated state
const wishlist = await wishlistApi.getWishlist();
return wishlist;
} catch (error) {
return rejectWithValue(error instanceof Error ? error.message : 'Failed to remove from wishlist');
}
}
);
export const wishlistApiSlice = createSlice({
name: "wishlistApi",
initialState,
reducers: {
// Keep synchronous actions for backward compatibility
addItemToWishlist: (state, action: PayloadAction<WishListItem>) => {
const { id, title, price, quantity, imgs, discountedPrice, status } =
action.payload;
const existingItem = state.items.find((item) => item.id === id);
if (!existingItem) {
state.items.push({
id,
title,
price,
quantity,
imgs,
discountedPrice,
status});
}
},
removeItemFromWishlist: (state, action: PayloadAction<number>) => {
const itemId = action.payload;
state.items = state.items.filter((item) => item.id !== itemId);
},
removeAllItemsFromWishlist: (state) => {
state.items = [];
},
clearError: (state) => {
state.error = null;
}},
extraReducers: (builder) => {
// Fetch wishlist
builder.addCase(fetchWishlist.pending, (state) => {
state.loading = true;
state.error = null;
});
builder.addCase(fetchWishlist.fulfilled, (state, action) => {
state.loading = false;
state.items = action.payload;
});
builder.addCase(fetchWishlist.rejected, (state, action) => {
state.loading = false;
state.error = action.payload as string;
});
// Add to wishlist
builder.addCase(addToWishlistAsync.pending, (state) => {
state.loading = true;
state.error = null;
});
builder.addCase(addToWishlistAsync.fulfilled, (state, action) => {
state.loading = false;
state.items = action.payload;
});
builder.addCase(addToWishlistAsync.rejected, (state, action) => {
state.loading = false;
state.error = action.payload as string;
});
// Remove from wishlist
builder.addCase(removeFromWishlistAsync.pending, (state) => {
state.loading = true;
state.error = null;
});
builder.addCase(removeFromWishlistAsync.fulfilled, (state, action) => {
state.loading = false;
state.items = action.payload;
});
builder.addCase(removeFromWishlistAsync.rejected, (state, action) => {
state.loading = false;
state.error = action.payload as string;
});
}});
// Selectors
export const selectWishlistItems = (state: RootState) => state.wishlistReducer.items;
// Note: loading and error are not in the original wishlist reducer
// export const selectWishlistLoading = (state: RootState) => state.wishlistReducer.loading;
// export const selectWishlistError = (state: RootState) => state.wishlistReducer.error;
export const selectIsInWishlist = (productId: number) => (state: RootState) => {
return state.wishlistReducer.items.some((item) => item.id === productId);
};
export const {
addItemToWishlist,
removeItemFromWishlist,
removeAllItemsFromWishlist,
clearError} = wishlistApiSlice.actions;
export default wishlistApiSlice.reducer;
|