From d741e6d0dc5ff5e10f6536c4192fbea42f60d467 Mon Sep 17 00:00:00 2001 From: andres Date: Sat, 17 Aug 2024 17:54:07 +0200 Subject: [PATCH] chore: prepare tasks reducer for rtk (use single argument in action creators) --- .../TodolistsList/tasks-reducer.test.ts | 19 ++++++-- src/features/TodolistsList/tasks-reducer.ts | 46 ++++++++++++++----- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/features/TodolistsList/tasks-reducer.test.ts b/src/features/TodolistsList/tasks-reducer.test.ts index ca96885..1e21111 100644 --- a/src/features/TodolistsList/tasks-reducer.test.ts +++ b/src/features/TodolistsList/tasks-reducer.test.ts @@ -97,7 +97,7 @@ beforeEach(() => { }) test('correct task should be deleted from correct array', () => { - const action = removeTaskAC('2', 'todolistId2') + const action = removeTaskAC({ taskId: '2', todolistId: 'todolistId2' }) const endState = tasksReducer(startState, action) @@ -129,7 +129,11 @@ test('correct task should be added to correct array', () => { expect(endState['todolistId2'][0].status).toBe(TaskStatuses.New) }) test('status of specified task should be changed', () => { - const action = updateTaskAC('2', { status: TaskStatuses.New }, 'todolistId2') + const action = updateTaskAC({ + taskId: '2', + model: { status: TaskStatuses.New }, + todolistId: 'todolistId2', + }) const endState = tasksReducer(startState, action) @@ -137,7 +141,11 @@ test('status of specified task should be changed', () => { expect(endState['todolistId2'][1].status).toBe(TaskStatuses.New) }) test('title of specified task should be changed', () => { - const action = updateTaskAC('2', { title: 'yogurt' }, 'todolistId2') + const action = updateTaskAC({ + taskId: '2', + model: { title: 'yogurt' }, + todolistId: 'todolistId2', + }) const endState = tasksReducer(startState, action) @@ -190,7 +198,10 @@ test('empty arrays should be added when we set todolists', () => { expect(endState['2']).toBeDefined() }) test('tasks should be added for todolist', () => { - const action = setTasksAC(startState['todolistId1'], 'todolistId1') + const action = setTasksAC({ + tasks: startState['todolistId1'], + todolistId: 'todolistId1', + }) const endState = tasksReducer( { diff --git a/src/features/TodolistsList/tasks-reducer.ts b/src/features/TodolistsList/tasks-reducer.ts index 6030374..393000a 100644 --- a/src/features/TodolistsList/tasks-reducer.ts +++ b/src/features/TodolistsList/tasks-reducer.ts @@ -72,17 +72,34 @@ export const tasksReducer = ( } // actions -export const removeTaskAC = (taskId: string, todolistId: string) => - ({ type: 'REMOVE-TASK', taskId, todolistId }) as const +export const removeTaskAC = ({ + taskId, + todolistId, +}: { + taskId: string + todolistId: string +}) => ({ type: 'REMOVE-TASK', taskId, todolistId }) as const + export const addTaskAC = (task: TaskType) => ({ type: 'ADD-TASK', task }) as const -export const updateTaskAC = ( - taskId: string, - model: UpdateDomainTaskModelType, + +export const updateTaskAC = ({ + taskId, + model, + todolistId, +}: { + taskId: string + model: UpdateDomainTaskModelType todolistId: string -) => ({ type: 'UPDATE-TASK', model, todolistId, taskId }) as const -export const setTasksAC = (tasks: Array, todolistId: string) => - ({ type: 'SET-TASKS', tasks, todolistId }) as const +}) => ({ type: 'UPDATE-TASK', model, todolistId, taskId }) as const + +export const setTasksAC = ({ + tasks, + todolistId, +}: { + tasks: Array + todolistId: string +}) => ({ type: 'SET-TASKS', tasks, todolistId }) as const // thunks export const fetchTasksTC = @@ -91,17 +108,19 @@ export const fetchTasksTC = dispatch(setAppStatusAC('loading')) todolistsAPI.getTasks(todolistId).then((res) => { const tasks = res.data.items - dispatch(setTasksAC(tasks, todolistId)) + dispatch(setTasksAC({ tasks: tasks, todolistId: todolistId })) dispatch(setAppStatusAC('succeeded')) }) } + export const removeTaskTC = (taskId: string, todolistId: string) => (dispatch: Dispatch) => { todolistsAPI.deleteTask(todolistId, taskId).then((res) => { - const action = removeTaskAC(taskId, todolistId) + const action = removeTaskAC({ taskId: taskId, todolistId: todolistId }) dispatch(action) }) } + export const addTaskTC = (title: string, todolistId: string) => ( @@ -126,6 +145,7 @@ export const addTaskTC = handleServerNetworkError(error, dispatch) }) } + export const updateTaskTC = ( taskId: string, @@ -155,7 +175,11 @@ export const updateTaskTC = .updateTask(todolistId, taskId, apiModel) .then((res) => { if (res.data.resultCode === 0) { - const action = updateTaskAC(taskId, domainModel, todolistId) + const action = updateTaskAC({ + taskId: taskId, + model: domainModel, + todolistId: todolistId, + }) dispatch(action) } else { handleServerAppError(res.data, dispatch)