|
| 1 | +import { PayloadAction, createAsyncThunk, createSlice } from '@reduxjs/toolkit'; |
| 2 | + |
| 3 | +import * as giftService from '@/features/gifts/giftService'; |
| 4 | +import { CreateGiftDTO, GiftIdea, GiftState } from '@/features/gifts/types'; |
| 5 | + |
| 6 | +const initialState: GiftState = { |
| 7 | + gifts: [], |
| 8 | + loading: false, |
| 9 | + error: null, |
| 10 | +}; |
| 11 | + |
| 12 | +export const fetchGifts = createAsyncThunk( |
| 13 | + 'gifts/fetchGifts', |
| 14 | + async (_, { rejectWithValue }) => { |
| 15 | + try { |
| 16 | + return await giftService.fetchGifts(); |
| 17 | + } catch (error) { |
| 18 | + return rejectWithValue((error as Error).message); |
| 19 | + } |
| 20 | + }, |
| 21 | +); |
| 22 | + |
| 23 | +export const addGift = createAsyncThunk( |
| 24 | + 'gifts/addGift', |
| 25 | + async (gift: CreateGiftDTO, { rejectWithValue }) => { |
| 26 | + try { |
| 27 | + return await giftService.addGift(gift); |
| 28 | + } catch (error) { |
| 29 | + return rejectWithValue((error as Error).message); |
| 30 | + } |
| 31 | + }, |
| 32 | +); |
| 33 | + |
| 34 | +export const deleteGift = createAsyncThunk( |
| 35 | + 'gifts/deleteGift', |
| 36 | + async (id: string, { rejectWithValue }) => { |
| 37 | + try { |
| 38 | + await giftService.deleteGift(id); |
| 39 | + return id; |
| 40 | + } catch (error) { |
| 41 | + return rejectWithValue((error as Error).message); |
| 42 | + } |
| 43 | + }, |
| 44 | +); |
| 45 | + |
| 46 | +// Thêm action updateGift |
| 47 | +export const updateGift = createAsyncThunk( |
| 48 | + 'gifts/updateGift', |
| 49 | + async (gift: Partial<GiftIdea>, { rejectWithValue }) => { |
| 50 | + try { |
| 51 | + return await giftService.updateGift(gift); |
| 52 | + } catch (error) { |
| 53 | + return rejectWithValue((error as Error).message); |
| 54 | + } |
| 55 | + }, |
| 56 | +); |
| 57 | + |
| 58 | +const giftSlice = createSlice({ |
| 59 | + name: 'gifts', |
| 60 | + initialState, |
| 61 | + reducers: {}, |
| 62 | + extraReducers: (builder) => { |
| 63 | + builder |
| 64 | + // Fetch Gifts |
| 65 | + .addCase(fetchGifts.pending, (state) => { |
| 66 | + state.loading = true; |
| 67 | + state.error = null; |
| 68 | + }) |
| 69 | + .addCase( |
| 70 | + fetchGifts.fulfilled, |
| 71 | + (state, action: PayloadAction<GiftIdea[]>) => { |
| 72 | + state.loading = false; |
| 73 | + state.gifts = action.payload; |
| 74 | + }, |
| 75 | + ) |
| 76 | + .addCase(fetchGifts.rejected, (state, action) => { |
| 77 | + state.loading = false; |
| 78 | + state.error = action.payload as string; |
| 79 | + }) |
| 80 | + |
| 81 | + // Add Gift |
| 82 | + .addCase(addGift.pending, (state) => { |
| 83 | + state.loading = true; |
| 84 | + state.error = null; |
| 85 | + }) |
| 86 | + .addCase(addGift.fulfilled, (state, action: PayloadAction<GiftIdea>) => { |
| 87 | + state.loading = false; |
| 88 | + state.gifts.push(action.payload); |
| 89 | + }) |
| 90 | + .addCase(addGift.rejected, (state, action) => { |
| 91 | + state.loading = false; |
| 92 | + state.error = action.payload as string; |
| 93 | + }) |
| 94 | + |
| 95 | + // Delete Gift |
| 96 | + .addCase(deleteGift.pending, (state) => { |
| 97 | + state.loading = true; |
| 98 | + state.error = null; |
| 99 | + }) |
| 100 | + .addCase(deleteGift.fulfilled, (state, action: PayloadAction<string>) => { |
| 101 | + state.loading = false; |
| 102 | + state.gifts = state.gifts.filter((gift) => gift.id !== action.payload); |
| 103 | + }) |
| 104 | + .addCase(deleteGift.rejected, (state, action) => { |
| 105 | + state.loading = false; |
| 106 | + state.error = action.payload as string; |
| 107 | + }) |
| 108 | + |
| 109 | + // Update Gift |
| 110 | + .addCase(updateGift.pending, (state) => { |
| 111 | + state.loading = true; |
| 112 | + state.error = null; |
| 113 | + }) |
| 114 | + .addCase( |
| 115 | + updateGift.fulfilled, |
| 116 | + (state, action: PayloadAction<GiftIdea>) => { |
| 117 | + state.loading = false; |
| 118 | + const index = state.gifts.findIndex( |
| 119 | + (gift) => gift.id === action.payload.id, |
| 120 | + ); |
| 121 | + if (index !== -1) { |
| 122 | + state.gifts[index] = action.payload; |
| 123 | + } |
| 124 | + }, |
| 125 | + ) |
| 126 | + .addCase(updateGift.rejected, (state, action) => { |
| 127 | + state.loading = false; |
| 128 | + state.error = action.payload as string; |
| 129 | + }); |
| 130 | + }, |
| 131 | +}); |
| 132 | + |
| 133 | +export default giftSlice.reducer; |
0 commit comments