From edbb247555da3b7e4279d9a455f808eb2bd6e7c1 Mon Sep 17 00:00:00 2001 From: andres Date: Sat, 17 Aug 2024 19:54:22 +0200 Subject: [PATCH] chore: refactor todolists reducer to use mutable operations --- .../TodolistsList/todolists-reducer.ts | 31 ++++++------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/features/TodolistsList/todolists-reducer.ts b/src/features/TodolistsList/todolists-reducer.ts index b72f32a..139ae5e 100644 --- a/src/features/TodolistsList/todolists-reducer.ts +++ b/src/features/TodolistsList/todolists-reducer.ts @@ -17,13 +17,11 @@ const todolistsSlice = createSlice({ initialState, reducers: { removeTodolist(state, action: PayloadAction) { - 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) { - return [ - { ...action.payload, filter: 'all', entityStatus: 'idle' }, - ...state, - ] + state.unshift({ ...action.payload, filter: 'all', entityStatus: 'idle' }) }, changeTodolistTitle( state, @@ -32,11 +30,8 @@ const todolistsSlice = createSlice({ title: string }> ) { - return state.map((tl) => - tl.id === action.payload.id - ? { ...tl, title: action.payload.title } - : tl - ) + const index = state.findIndex((todo) => todo.id === action.payload.id) + if (index !== -1) state[index].title = action.payload.title }, changeTodolistFilter( state, @@ -45,11 +40,8 @@ const todolistsSlice = createSlice({ filter: FilterValues }> ) { - return state.map((tl) => - tl.id === action.payload.id - ? { ...tl, filter: action.payload.filter } - : tl - ) + const index = state.findIndex((todo) => todo.id === action.payload.id) + if (index !== -1) state[index].filter = action.payload.filter }, changeTodolistEntityStatus( state, @@ -58,13 +50,10 @@ const todolistsSlice = createSlice({ status: RequestStatus }> ) { - return state.map((tl) => - tl.id === action.payload.id - ? { ...tl, entityStatus: action.payload.status } - : tl - ) + const index = state.findIndex((todo) => todo.id === action.payload.id) + if (index !== -1) state[index].entityStatus = action.payload.status }, - setTodolists(state, action: PayloadAction>) { + setTodolists(_state, action: PayloadAction>) { return action.payload.map((tl) => ({ ...tl, filter: 'all',