| import { createSlice, PayloadAction } from '@reduxjs/toolkit'; | |
| export interface User { | |
| id: string; | |
| name: string; | |
| email: string; | |
| } | |
| interface UserState { | |
| currentUser: User | null; | |
| users: User[]; | |
| loading: boolean; | |
| error: string | null; | |
| } | |
| const initialState: UserState = { | |
| currentUser: null, | |
| users: [], | |
| loading: false, | |
| error: null, | |
| }; | |
| const userSlice = createSlice({ | |
| name: 'user', | |
| initialState, | |
| reducers: { | |
| setCurrentUser: (state, action: PayloadAction<User>) => { | |
| state.currentUser = action.payload; | |
| }, | |
| addUser: (state, action: PayloadAction<User>) => { | |
| state.users.push(action.payload); | |
| }, | |
| removeUser: (state, action: PayloadAction<string>) => { | |
| state.users = state.users.filter(user => user.id !== action.payload); | |
| }, | |
| setLoading: (state, action: PayloadAction<boolean>) => { | |
| state.loading = action.payload; | |
| }, | |
| setError: (state, action: PayloadAction<string | null>) => { | |
| state.error = action.payload; | |
| }, | |
| }, | |
| }); | |
| export const { setCurrentUser, addUser, removeUser, setLoading, setError } = userSlice.actions; | |
| export default userSlice.reducer; | |