mirror of
https://github.com/ershisan99/it-incubator-todolist-ts-17-live-2024-08-17.git
synced 2026-02-05 12:35:18 +00:00
chore: add prettier and run it on all files
This commit is contained in:
@@ -1,109 +1,142 @@
|
||||
import {todolistsAPI, TodolistType} from '../../api/todolists-api'
|
||||
import {Dispatch} from 'redux'
|
||||
import {RequestStatusType, SetAppErrorActionType, setAppStatusAC, SetAppStatusActionType} from '../../app/app-reducer'
|
||||
import {handleServerNetworkError} from '../../utils/error-utils'
|
||||
import { AppThunk } from '../../app/store';
|
||||
import { todolistsAPI, TodolistType } from '../../api/todolists-api'
|
||||
import { Dispatch } from 'redux'
|
||||
import {
|
||||
RequestStatusType,
|
||||
SetAppErrorActionType,
|
||||
setAppStatusAC,
|
||||
SetAppStatusActionType,
|
||||
} from '../../app/app-reducer'
|
||||
import { handleServerNetworkError } from '../../utils/error-utils'
|
||||
import { AppThunk } from '../../app/store'
|
||||
|
||||
const initialState: Array<TodolistDomainType> = []
|
||||
|
||||
export const todolistsReducer = (state: Array<TodolistDomainType> = initialState, action: ActionsType): Array<TodolistDomainType> => {
|
||||
switch (action.type) {
|
||||
case 'REMOVE-TODOLIST':
|
||||
return state.filter(tl => tl.id != action.id)
|
||||
case 'ADD-TODOLIST':
|
||||
return [{...action.todolist, filter: 'all', entityStatus: 'idle'}, ...state]
|
||||
export const todolistsReducer = (
|
||||
state: Array<TodolistDomainType> = initialState,
|
||||
action: ActionsType
|
||||
): Array<TodolistDomainType> => {
|
||||
switch (action.type) {
|
||||
case 'REMOVE-TODOLIST':
|
||||
return state.filter((tl) => tl.id != action.id)
|
||||
case 'ADD-TODOLIST':
|
||||
return [
|
||||
{ ...action.todolist, filter: 'all', entityStatus: 'idle' },
|
||||
...state,
|
||||
]
|
||||
|
||||
case 'CHANGE-TODOLIST-TITLE':
|
||||
return state.map(tl => tl.id === action.id ? {...tl, title: action.title} : tl)
|
||||
case 'CHANGE-TODOLIST-FILTER':
|
||||
return state.map(tl => tl.id === action.id ? {...tl, filter: action.filter} : tl)
|
||||
case 'CHANGE-TODOLIST-ENTITY-STATUS':
|
||||
return state.map(tl => tl.id === action.id ? {...tl, entityStatus: action.status} : tl)
|
||||
case 'SET-TODOLISTS':
|
||||
return action.todolists.map(tl => ({...tl, filter: 'all', entityStatus: 'idle'}))
|
||||
default:
|
||||
return state
|
||||
}
|
||||
case 'CHANGE-TODOLIST-TITLE':
|
||||
return state.map((tl) =>
|
||||
tl.id === action.id ? { ...tl, title: action.title } : tl
|
||||
)
|
||||
case 'CHANGE-TODOLIST-FILTER':
|
||||
return state.map((tl) =>
|
||||
tl.id === action.id ? { ...tl, filter: action.filter } : tl
|
||||
)
|
||||
case 'CHANGE-TODOLIST-ENTITY-STATUS':
|
||||
return state.map((tl) =>
|
||||
tl.id === action.id ? { ...tl, entityStatus: action.status } : tl
|
||||
)
|
||||
case 'SET-TODOLISTS':
|
||||
return action.todolists.map((tl) => ({
|
||||
...tl,
|
||||
filter: 'all',
|
||||
entityStatus: 'idle',
|
||||
}))
|
||||
default:
|
||||
return state
|
||||
}
|
||||
}
|
||||
|
||||
// actions
|
||||
export const removeTodolistAC = (id: string) => ({type: 'REMOVE-TODOLIST', id} as const)
|
||||
export const addTodolistAC = (todolist: TodolistType) => ({type: 'ADD-TODOLIST', todolist} as const)
|
||||
export const changeTodolistTitleAC = (id: string, title: string) => ({
|
||||
export const removeTodolistAC = (id: string) =>
|
||||
({ type: 'REMOVE-TODOLIST', id }) as const
|
||||
export const addTodolistAC = (todolist: TodolistType) =>
|
||||
({ type: 'ADD-TODOLIST', todolist }) as const
|
||||
export const changeTodolistTitleAC = (id: string, title: string) =>
|
||||
({
|
||||
type: 'CHANGE-TODOLIST-TITLE',
|
||||
id,
|
||||
title
|
||||
} as const)
|
||||
export const changeTodolistFilterAC = (id: string, filter: FilterValuesType) => ({
|
||||
title,
|
||||
}) as const
|
||||
export const changeTodolistFilterAC = (id: string, filter: FilterValuesType) =>
|
||||
({
|
||||
type: 'CHANGE-TODOLIST-FILTER',
|
||||
id,
|
||||
filter
|
||||
} as const)
|
||||
export const changeTodolistEntityStatusAC = (id: string, status: RequestStatusType) => ({
|
||||
type: 'CHANGE-TODOLIST-ENTITY-STATUS', id, status } as const)
|
||||
export const setTodolistsAC = (todolists: Array<TodolistType>) => ({type: 'SET-TODOLISTS', todolists} as const)
|
||||
filter,
|
||||
}) as const
|
||||
export const changeTodolistEntityStatusAC = (
|
||||
id: string,
|
||||
status: RequestStatusType
|
||||
) =>
|
||||
({
|
||||
type: 'CHANGE-TODOLIST-ENTITY-STATUS',
|
||||
id,
|
||||
status,
|
||||
}) as const
|
||||
export const setTodolistsAC = (todolists: Array<TodolistType>) =>
|
||||
({ type: 'SET-TODOLISTS', todolists }) as const
|
||||
|
||||
// thunks
|
||||
export const fetchTodolistsTC = (): AppThunk => {
|
||||
return (dispatch) => {
|
||||
dispatch(setAppStatusAC('loading'))
|
||||
todolistsAPI.getTodolists()
|
||||
.then((res) => {
|
||||
dispatch(setTodolistsAC(res.data))
|
||||
dispatch(setAppStatusAC('succeeded'))
|
||||
})
|
||||
.catch(error => {
|
||||
handleServerNetworkError(error, dispatch);
|
||||
})
|
||||
}
|
||||
return (dispatch) => {
|
||||
dispatch(setAppStatusAC('loading'))
|
||||
todolistsAPI
|
||||
.getTodolists()
|
||||
.then((res) => {
|
||||
dispatch(setTodolistsAC(res.data))
|
||||
dispatch(setAppStatusAC('succeeded'))
|
||||
})
|
||||
.catch((error) => {
|
||||
handleServerNetworkError(error, dispatch)
|
||||
})
|
||||
}
|
||||
}
|
||||
export const removeTodolistTC = (todolistId: string) => {
|
||||
return (dispatch: ThunkDispatch) => {
|
||||
//изменим глобальный статус приложения, чтобы вверху полоса побежала
|
||||
dispatch(setAppStatusAC('loading'))
|
||||
//изменим статус конкретного тудулиста, чтобы он мог задизеблить что надо
|
||||
dispatch(changeTodolistEntityStatusAC(todolistId, 'loading'))
|
||||
todolistsAPI.deleteTodolist(todolistId)
|
||||
.then((res) => {
|
||||
dispatch(removeTodolistAC(todolistId))
|
||||
//скажем глобально приложению, что асинхронная операция завершена
|
||||
dispatch(setAppStatusAC('succeeded'))
|
||||
})
|
||||
}
|
||||
return (dispatch: ThunkDispatch) => {
|
||||
//изменим глобальный статус приложения, чтобы вверху полоса побежала
|
||||
dispatch(setAppStatusAC('loading'))
|
||||
//изменим статус конкретного тудулиста, чтобы он мог задизеблить что надо
|
||||
dispatch(changeTodolistEntityStatusAC(todolistId, 'loading'))
|
||||
todolistsAPI.deleteTodolist(todolistId).then((res) => {
|
||||
dispatch(removeTodolistAC(todolistId))
|
||||
//скажем глобально приложению, что асинхронная операция завершена
|
||||
dispatch(setAppStatusAC('succeeded'))
|
||||
})
|
||||
}
|
||||
}
|
||||
export const addTodolistTC = (title: string) => {
|
||||
return (dispatch: ThunkDispatch) => {
|
||||
dispatch(setAppStatusAC('loading'))
|
||||
todolistsAPI.createTodolist(title)
|
||||
.then((res) => {
|
||||
dispatch(addTodolistAC(res.data.data.item))
|
||||
dispatch(setAppStatusAC('succeeded'))
|
||||
})
|
||||
}
|
||||
return (dispatch: ThunkDispatch) => {
|
||||
dispatch(setAppStatusAC('loading'))
|
||||
todolistsAPI.createTodolist(title).then((res) => {
|
||||
dispatch(addTodolistAC(res.data.data.item))
|
||||
dispatch(setAppStatusAC('succeeded'))
|
||||
})
|
||||
}
|
||||
}
|
||||
export const changeTodolistTitleTC = (id: string, title: string) => {
|
||||
return (dispatch: Dispatch<ActionsType>) => {
|
||||
todolistsAPI.updateTodolist(id, title)
|
||||
.then((res) => {
|
||||
dispatch(changeTodolistTitleAC(id, title))
|
||||
})
|
||||
}
|
||||
return (dispatch: Dispatch<ActionsType>) => {
|
||||
todolistsAPI.updateTodolist(id, title).then((res) => {
|
||||
dispatch(changeTodolistTitleAC(id, title))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// types
|
||||
export type AddTodolistActionType = ReturnType<typeof addTodolistAC>;
|
||||
export type RemoveTodolistActionType = ReturnType<typeof removeTodolistAC>;
|
||||
export type SetTodolistsActionType = ReturnType<typeof setTodolistsAC>;
|
||||
export type AddTodolistActionType = ReturnType<typeof addTodolistAC>
|
||||
export type RemoveTodolistActionType = ReturnType<typeof removeTodolistAC>
|
||||
export type SetTodolistsActionType = ReturnType<typeof setTodolistsAC>
|
||||
type ActionsType =
|
||||
| RemoveTodolistActionType
|
||||
| AddTodolistActionType
|
||||
| ReturnType<typeof changeTodolistTitleAC>
|
||||
| ReturnType<typeof changeTodolistFilterAC>
|
||||
| SetTodolistsActionType
|
||||
| ReturnType<typeof changeTodolistEntityStatusAC>
|
||||
export type FilterValuesType = 'all' | 'active' | 'completed';
|
||||
| RemoveTodolistActionType
|
||||
| AddTodolistActionType
|
||||
| ReturnType<typeof changeTodolistTitleAC>
|
||||
| ReturnType<typeof changeTodolistFilterAC>
|
||||
| SetTodolistsActionType
|
||||
| ReturnType<typeof changeTodolistEntityStatusAC>
|
||||
export type FilterValuesType = 'all' | 'active' | 'completed'
|
||||
export type TodolistDomainType = TodolistType & {
|
||||
filter: FilterValuesType
|
||||
entityStatus: RequestStatusType
|
||||
filter: FilterValuesType
|
||||
entityStatus: RequestStatusType
|
||||
}
|
||||
type ThunkDispatch = Dispatch<ActionsType | SetAppStatusActionType | SetAppErrorActionType>
|
||||
type ThunkDispatch = Dispatch<
|
||||
ActionsType | SetAppStatusActionType | SetAppErrorActionType
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user