mirror of
https://github.com/ershisan99/it-incubator-todolist-ts-17-live-2024-08-17.git
synced 2025-12-16 20:59:30 +00:00
chore: refactor tasks reducer to use mutable operations
This commit is contained in:
@@ -44,21 +44,13 @@ const tasksSlice = createSlice({
|
|||||||
todolistId: string
|
todolistId: string
|
||||||
}>
|
}>
|
||||||
) {
|
) {
|
||||||
return {
|
const tasks = state[action.payload.todolistId]
|
||||||
...state,
|
const index = tasks.findIndex((task) => task.id === action.payload.taskId)
|
||||||
[action.payload.todolistId]: state[action.payload.todolistId].filter(
|
if (index !== -1) tasks.splice(index, 1)
|
||||||
(t) => t.id != action.payload.taskId
|
|
||||||
),
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
addTask(state, action: PayloadAction<TaskEntity>) {
|
addTask(state, action: PayloadAction<TaskEntity>) {
|
||||||
return {
|
const tasks = state[action.payload.todoListId]
|
||||||
...state,
|
tasks.unshift(action.payload)
|
||||||
[action.payload.todoListId]: [
|
|
||||||
action.payload,
|
|
||||||
...state[action.payload.todoListId],
|
|
||||||
],
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
updateTask(
|
updateTask(
|
||||||
state,
|
state,
|
||||||
@@ -68,14 +60,10 @@ const tasksSlice = createSlice({
|
|||||||
todolistId: string
|
todolistId: string
|
||||||
}>
|
}>
|
||||||
) {
|
) {
|
||||||
return {
|
const tasks = state[action.payload.todolistId]
|
||||||
...state,
|
const task = tasks.find((task) => task.id === action.payload.taskId)
|
||||||
[action.payload.todolistId]: state[action.payload.todolistId].map(
|
if (task) {
|
||||||
(t) =>
|
Object.assign(task, action.payload.model)
|
||||||
t.id === action.payload.taskId
|
|
||||||
? { ...t, ...action.payload.model }
|
|
||||||
: t
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
setTasks(
|
setTasks(
|
||||||
@@ -85,25 +73,21 @@ const tasksSlice = createSlice({
|
|||||||
todolistId: string
|
todolistId: string
|
||||||
}>
|
}>
|
||||||
) {
|
) {
|
||||||
return { ...state, [action.payload.todolistId]: action.payload.tasks }
|
state[action.payload.todolistId] = action.payload.tasks
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
extraReducers(builder) {
|
extraReducers(builder) {
|
||||||
builder
|
builder
|
||||||
.addCase(addTodolist, (state, action) => {
|
.addCase(addTodolist, (state, action) => {
|
||||||
return { ...state, [action.payload.id]: [] }
|
state[action.payload.id] = []
|
||||||
})
|
})
|
||||||
.addCase(removeTodolist, (state, action) => {
|
.addCase(removeTodolist, (state, action) => {
|
||||||
const copyState = { ...state }
|
delete state[action.payload]
|
||||||
delete copyState[action.payload]
|
|
||||||
return copyState
|
|
||||||
})
|
})
|
||||||
.addCase(setTodolists, (state, action) => {
|
.addCase(setTodolists, (state, action) => {
|
||||||
const copyState = { ...state }
|
|
||||||
action.payload.forEach((tl) => {
|
action.payload.forEach((tl) => {
|
||||||
copyState[tl.id] = []
|
state[tl.id] = []
|
||||||
})
|
})
|
||||||
return copyState
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user