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;