chore: prepare tasks reducer for rtk (use single argument in action creators)

This commit is contained in:
2024-08-17 17:54:07 +02:00
parent 39bd7fa798
commit d741e6d0dc
2 changed files with 50 additions and 15 deletions

View File

@@ -97,7 +97,7 @@ beforeEach(() => {
}) })
test('correct task should be deleted from correct array', () => { 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) 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) expect(endState['todolistId2'][0].status).toBe(TaskStatuses.New)
}) })
test('status of specified task should be changed', () => { 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) 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) expect(endState['todolistId2'][1].status).toBe(TaskStatuses.New)
}) })
test('title of specified task should be changed', () => { 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) const endState = tasksReducer(startState, action)
@@ -190,7 +198,10 @@ test('empty arrays should be added when we set todolists', () => {
expect(endState['2']).toBeDefined() expect(endState['2']).toBeDefined()
}) })
test('tasks should be added for todolist', () => { test('tasks should be added for todolist', () => {
const action = setTasksAC(startState['todolistId1'], 'todolistId1') const action = setTasksAC({
tasks: startState['todolistId1'],
todolistId: 'todolistId1',
})
const endState = tasksReducer( const endState = tasksReducer(
{ {

View File

@@ -72,17 +72,34 @@ export const tasksReducer = (
} }
// actions // actions
export const removeTaskAC = (taskId: string, todolistId: string) => export const removeTaskAC = ({
({ type: 'REMOVE-TASK', taskId, todolistId }) as const taskId,
todolistId,
}: {
taskId: string
todolistId: string
}) => ({ type: 'REMOVE-TASK', taskId, todolistId }) as const
export const addTaskAC = (task: TaskType) => export const addTaskAC = (task: TaskType) =>
({ type: 'ADD-TASK', task }) as const ({ type: 'ADD-TASK', task }) as const
export const updateTaskAC = (
taskId: string, export const updateTaskAC = ({
model: UpdateDomainTaskModelType, taskId,
model,
todolistId,
}: {
taskId: string
model: UpdateDomainTaskModelType
todolistId: string todolistId: string
) => ({ type: 'UPDATE-TASK', model, todolistId, taskId }) as const }) => ({ type: 'UPDATE-TASK', model, todolistId, taskId }) as const
export const setTasksAC = (tasks: Array<TaskType>, todolistId: string) =>
({ type: 'SET-TASKS', tasks, todolistId }) as const export const setTasksAC = ({
tasks,
todolistId,
}: {
tasks: Array<TaskType>
todolistId: string
}) => ({ type: 'SET-TASKS', tasks, todolistId }) as const
// thunks // thunks
export const fetchTasksTC = export const fetchTasksTC =
@@ -91,17 +108,19 @@ export const fetchTasksTC =
dispatch(setAppStatusAC('loading')) dispatch(setAppStatusAC('loading'))
todolistsAPI.getTasks(todolistId).then((res) => { todolistsAPI.getTasks(todolistId).then((res) => {
const tasks = res.data.items const tasks = res.data.items
dispatch(setTasksAC(tasks, todolistId)) dispatch(setTasksAC({ tasks: tasks, todolistId: todolistId }))
dispatch(setAppStatusAC('succeeded')) dispatch(setAppStatusAC('succeeded'))
}) })
} }
export const removeTaskTC = export const removeTaskTC =
(taskId: string, todolistId: string) => (dispatch: Dispatch<ActionsType>) => { (taskId: string, todolistId: string) => (dispatch: Dispatch<ActionsType>) => {
todolistsAPI.deleteTask(todolistId, taskId).then((res) => { todolistsAPI.deleteTask(todolistId, taskId).then((res) => {
const action = removeTaskAC(taskId, todolistId) const action = removeTaskAC({ taskId: taskId, todolistId: todolistId })
dispatch(action) dispatch(action)
}) })
} }
export const addTaskTC = export const addTaskTC =
(title: string, todolistId: string) => (title: string, todolistId: string) =>
( (
@@ -126,6 +145,7 @@ export const addTaskTC =
handleServerNetworkError(error, dispatch) handleServerNetworkError(error, dispatch)
}) })
} }
export const updateTaskTC = export const updateTaskTC =
( (
taskId: string, taskId: string,
@@ -155,7 +175,11 @@ export const updateTaskTC =
.updateTask(todolistId, taskId, apiModel) .updateTask(todolistId, taskId, apiModel)
.then((res) => { .then((res) => {
if (res.data.resultCode === 0) { if (res.data.resultCode === 0) {
const action = updateTaskAC(taskId, domainModel, todolistId) const action = updateTaskAC({
taskId: taskId,
model: domainModel,
todolistId: todolistId,
})
dispatch(action) dispatch(action)
} else { } else {
handleServerAppError(res.data, dispatch) handleServerAppError(res.data, dispatch)