mirror of
https://github.com/ershisan99/it-incubator-todolist-ts-17-live-2024-08-17.git
synced 2026-02-04 21:02:13 +00:00
chore: refactor app reducer to use rtk
This commit is contained in:
@@ -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
|
||||
>
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
import { todolistsAPI, TodolistType } from 'api/todolists-api'
|
||||
import { Dispatch } from 'redux'
|
||||
import {
|
||||
RequestStatusType,
|
||||
SetAppErrorActionType,
|
||||
setAppStatusAC,
|
||||
SetAppStatusActionType,
|
||||
} from 'app/app-reducer'
|
||||
import { RequestStatusType, setAppStatusAC } from 'app/app-reducer'
|
||||
import { handleServerNetworkError } from 'utils/error-utils'
|
||||
import { AppThunk } from 'app/store'
|
||||
|
||||
@@ -110,8 +105,8 @@ export const fetchTodolistsTC = (): AppThunk => {
|
||||
})
|
||||
}
|
||||
}
|
||||
export const removeTodolistTC = (todolistId: string) => {
|
||||
return (dispatch: ThunkDispatch) => {
|
||||
export const removeTodolistTC = (todolistId: string): AppThunk => {
|
||||
return (dispatch) => {
|
||||
//изменим глобальный статус приложения, чтобы вверху полоса побежала
|
||||
dispatch(setAppStatusAC('loading'))
|
||||
//изменим статус конкретного тудулиста, чтобы он мог задизеблить что надо
|
||||
@@ -125,8 +120,8 @@ export const removeTodolistTC = (todolistId: string) => {
|
||||
})
|
||||
}
|
||||
}
|
||||
export const addTodolistTC = (title: string) => {
|
||||
return (dispatch: ThunkDispatch) => {
|
||||
export const addTodolistTC = (title: string): AppThunk => {
|
||||
return (dispatch) => {
|
||||
dispatch(setAppStatusAC('loading'))
|
||||
todolistsAPI.createTodolist(title).then((res) => {
|
||||
dispatch(addTodolistAC(res.data.data.item))
|
||||
@@ -158,6 +153,3 @@ export type TodolistDomainType = TodolistType & {
|
||||
filter: FilterValuesType
|
||||
entityStatus: RequestStatusType
|
||||
}
|
||||
type ThunkDispatch = Dispatch<
|
||||
ActionsType | SetAppStatusActionType | SetAppErrorActionType
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user