Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit 99ce956

Browse files
committed
add: slice for object
1 parent b03800a commit 99ce956

File tree

2 files changed

+221
-0
lines changed

2 files changed

+221
-0
lines changed

src/features/gifts/giftSlice.ts

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { PayloadAction, createAsyncThunk, createSlice } from '@reduxjs/toolkit';
2+
3+
import * as recipientService from '@/features/recipients/recipientService';
4+
import {
5+
CreateRecipientDTO,
6+
Recipient,
7+
RecipientState,
8+
} from '@/features/recipients/types';
9+
10+
const initialState: RecipientState = {
11+
recipients: [],
12+
loading: false,
13+
error: null,
14+
};
15+
16+
export const fetchRecipients = createAsyncThunk(
17+
'recipients/fetchRecipients',
18+
async (_, { rejectWithValue }) => {
19+
try {
20+
return await recipientService.fetchRecipients();
21+
} catch (error) {
22+
return rejectWithValue((error as Error).message);
23+
}
24+
},
25+
);
26+
27+
export const addRecipient = createAsyncThunk(
28+
'recipients/addRecipient',
29+
async (recipient: CreateRecipientDTO, { rejectWithValue }) => {
30+
try {
31+
return await recipientService.addRecipient(recipient);
32+
} catch (error) {
33+
return rejectWithValue((error as Error).message);
34+
}
35+
},
36+
);
37+
38+
export const deleteRecipient = createAsyncThunk(
39+
'recipients/deleteRecipient',
40+
async (id: string, { rejectWithValue }) => {
41+
try {
42+
await recipientService.deleteRecipient(id);
43+
return id;
44+
} catch (error) {
45+
return rejectWithValue((error as Error).message);
46+
}
47+
},
48+
);
49+
50+
const recipientSlice = createSlice({
51+
name: 'recipients',
52+
initialState,
53+
reducers: {},
54+
extraReducers: (builder) => {
55+
builder
56+
.addCase(fetchRecipients.pending, (state) => {
57+
state.loading = true;
58+
state.error = null;
59+
})
60+
.addCase(
61+
fetchRecipients.fulfilled,
62+
(state, action: PayloadAction<Recipient[]>) => {
63+
state.loading = false;
64+
state.recipients = action.payload;
65+
},
66+
)
67+
.addCase(fetchRecipients.rejected, (state, action) => {
68+
state.loading = false;
69+
state.error = action.payload as string;
70+
})
71+
.addCase(
72+
addRecipient.fulfilled,
73+
(state, action: PayloadAction<Recipient>) => {
74+
state.recipients.push(action.payload);
75+
},
76+
)
77+
.addCase(
78+
deleteRecipient.fulfilled,
79+
(state, action: PayloadAction<string>) => {
80+
state.recipients = state.recipients.filter(
81+
(recipient) => recipient.id !== action.payload,
82+
);
83+
},
84+
);
85+
},
86+
});
87+
88+
export default recipientSlice.reducer;

0 commit comments

Comments
 (0)