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
| import { createSlice } from '@reduxjs/toolkit';
|
| const initialState = {
| volunteers: [],
| currentVolunteer: null,
| volunteerPoints: [],
| loading: false,
| error: null,
| };
|
| const volunteerSlice = createSlice({
| name: 'volunteer',
| initialState,
| reducers: {
| setLoading: (state, action) => {
| state.loading = action.payload;
| },
| setError: (state, action) => {
| state.error = action.payload;
| state.loading = false;
| },
| setVolunteers: (state, action) => {
| state.volunteers = action.payload;
| state.loading = false;
| },
| setCurrentVolunteer: (state, action) => {
| state.currentVolunteer = action.payload;
| },
| setVolunteerPoints: (state, action) => {
| state.volunteerPoints = action.payload;
| state.loading = false;
| },
| addVolunteer: (state, action) => {
| state.volunteers.unshift(action.payload);
| },
| updateVolunteer: (state, action) => {
| const index = state.volunteers.findIndex(volunteer => volunteer.id === action.payload.id);
| if (index !== -1) {
| state.volunteers[index] = action.payload;
| }
| },
| deleteVolunteer: (state, action) => {
| state.volunteers = state.volunteers.filter(volunteer => volunteer.id !== action.payload);
| },
| updateVolunteerPoints: (state, action) => {
| const { volunteerId, points } = action.payload;
| const volunteer = state.volunteers.find(v => v.id === volunteerId);
| if (volunteer) {
| volunteer.totalPoints = points;
| }
| },
| },
| });
|
| export const {
| setLoading,
| setError,
| setVolunteers,
| setCurrentVolunteer,
| setVolunteerPoints,
| addVolunteer,
| updateVolunteer,
| deleteVolunteer,
| updateVolunteerPoints,
| } = volunteerSlice.actions;
|
| export default volunteerSlice.reducer;
|
|