chore: refactor app reducer to use rtk

This commit is contained in:
2024-08-17 18:33:20 +02:00
parent 7cd60b5c61
commit 5067f64ec3
4 changed files with 46 additions and 78 deletions

View File

@@ -11,12 +11,8 @@ import {
UpdateTaskModelType,
} from 'api/todolists-api'
import { Dispatch } from 'redux'
import { AppRootStateType } from 'app/store'
import {
SetAppErrorActionType,
setAppStatusAC,
SetAppStatusActionType,
} from 'app/app-reducer'
import { AppRootStateType, AppThunk } from 'app/store'
import { setAppStatusAC } from 'app/app-reducer'
import {
handleServerAppError,
handleServerNetworkError,
@@ -103,8 +99,8 @@ export const setTasksAC = ({
// thunks
export const fetchTasksTC =
(todolistId: string) =>
(dispatch: Dispatch<ActionsType | SetAppStatusActionType>) => {
(todolistId: string): AppThunk =>
(dispatch) => {
dispatch(setAppStatusAC('loading'))
todolistsAPI.getTasks(todolistId).then((res) => {
const tasks = res.data.items
@@ -122,12 +118,8 @@ export const removeTaskTC =
}
export const addTaskTC =
(title: string, todolistId: string) =>
(
dispatch: Dispatch<
ActionsType | SetAppErrorActionType | SetAppStatusActionType
>
) => {
(title: string, todolistId: string): AppThunk =>
(dispatch) => {
dispatch(setAppStatusAC('loading'))
todolistsAPI
.createTask(todolistId, title)
@@ -151,8 +143,8 @@ export const updateTaskTC =
taskId: string,
domainModel: UpdateDomainTaskModelType,
todolistId: string
) =>
(dispatch: ThunkDispatch, getState: () => AppRootStateType) => {
): AppThunk =>
(dispatch, getState: () => AppRootStateType) => {
const state = getState()
const task = state.tasks[todolistId].find((t) => t.id === taskId)
if (!task) {
@@ -210,6 +202,3 @@ type ActionsType =
| RemoveTodolistActionType
| SetTodolistsActionType
| ReturnType<typeof setTasksAC>
type ThunkDispatch = Dispatch<
ActionsType | SetAppStatusActionType | SetAppErrorActionType
>