chore: refactor todolists reducer to use mutable operations

This commit is contained in:
2024-08-17 19:54:22 +02:00
parent a34813c667
commit edbb247555

View File

@@ -17,13 +17,11 @@ const todolistsSlice = createSlice({
initialState, initialState,
reducers: { reducers: {
removeTodolist(state, action: PayloadAction<string>) { removeTodolist(state, action: PayloadAction<string>) {
return state.filter((tl) => tl.id != action.payload) const index = state.findIndex((todo) => todo.id === action.payload)
if (index !== -1) state.splice(index, 1)
}, },
addTodolist(state, action: PayloadAction<Todolist>) { addTodolist(state, action: PayloadAction<Todolist>) {
return [ state.unshift({ ...action.payload, filter: 'all', entityStatus: 'idle' })
{ ...action.payload, filter: 'all', entityStatus: 'idle' },
...state,
]
}, },
changeTodolistTitle( changeTodolistTitle(
state, state,
@@ -32,11 +30,8 @@ const todolistsSlice = createSlice({
title: string title: string
}> }>
) { ) {
return state.map((tl) => const index = state.findIndex((todo) => todo.id === action.payload.id)
tl.id === action.payload.id if (index !== -1) state[index].title = action.payload.title
? { ...tl, title: action.payload.title }
: tl
)
}, },
changeTodolistFilter( changeTodolistFilter(
state, state,
@@ -45,11 +40,8 @@ const todolistsSlice = createSlice({
filter: FilterValues filter: FilterValues
}> }>
) { ) {
return state.map((tl) => const index = state.findIndex((todo) => todo.id === action.payload.id)
tl.id === action.payload.id if (index !== -1) state[index].filter = action.payload.filter
? { ...tl, filter: action.payload.filter }
: tl
)
}, },
changeTodolistEntityStatus( changeTodolistEntityStatus(
state, state,
@@ -58,13 +50,10 @@ const todolistsSlice = createSlice({
status: RequestStatus status: RequestStatus
}> }>
) { ) {
return state.map((tl) => const index = state.findIndex((todo) => todo.id === action.payload.id)
tl.id === action.payload.id if (index !== -1) state[index].entityStatus = action.payload.status
? { ...tl, entityStatus: action.payload.status }
: tl
)
}, },
setTodolists(state, action: PayloadAction<Array<Todolist>>) { setTodolists(_state, action: PayloadAction<Array<Todolist>>) {
return action.payload.map((tl) => ({ return action.payload.map((tl) => ({
...tl, ...tl,
filter: 'all', filter: 'all',