mirror of
https://github.com/ershisan99/it-incubator-todolist-ts-17-live-2024-08-17.git
synced 2025-12-16 20:59:30 +00:00
chore: refactor auth reducer to use rtk
This commit is contained in:
@@ -1,44 +1,38 @@
|
|||||||
import { Dispatch } from 'redux'
|
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
||||||
import {
|
|
||||||
SetAppErrorActionType,
|
|
||||||
setAppStatusAC,
|
|
||||||
SetAppStatusActionType,
|
|
||||||
} from 'app/app-reducer'
|
|
||||||
import { authAPI, LoginParamsType } from 'api/todolists-api'
|
import { authAPI, LoginParamsType } from 'api/todolists-api'
|
||||||
|
import { setAppStatusAC } from 'app/app-reducer'
|
||||||
import {
|
import {
|
||||||
handleServerAppError,
|
handleServerAppError,
|
||||||
handleServerNetworkError,
|
handleServerNetworkError,
|
||||||
} from 'utils/error-utils'
|
} from 'utils/error-utils'
|
||||||
|
import { AppThunk } from 'app/store'
|
||||||
|
|
||||||
|
type InitialStateType = {
|
||||||
|
isLoggedIn: boolean
|
||||||
|
}
|
||||||
|
|
||||||
const initialState: InitialStateType = {
|
const initialState: InitialStateType = {
|
||||||
isLoggedIn: false,
|
isLoggedIn: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const authReducer = (
|
const authSlice = createSlice({
|
||||||
state: InitialStateType = initialState,
|
name: 'auth',
|
||||||
action: ActionsType
|
initialState,
|
||||||
): InitialStateType => {
|
reducers: {
|
||||||
switch (action.type) {
|
setIsLoggedInAC(state, action: PayloadAction<boolean>) {
|
||||||
case 'login/SET-IS-LOGGED-IN':
|
return { ...state, isLoggedIn: action.payload }
|
||||||
return { ...state, isLoggedIn: action.value }
|
},
|
||||||
default:
|
},
|
||||||
return state
|
})
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// actions
|
export const authReducer = authSlice.reducer
|
||||||
|
|
||||||
export const setIsLoggedInAC = (value: boolean) =>
|
export const { setIsLoggedInAC } = authSlice.actions
|
||||||
({ type: 'login/SET-IS-LOGGED-IN', value }) as const
|
|
||||||
|
|
||||||
// thunks
|
// thunks
|
||||||
export const loginTC =
|
export const loginTC =
|
||||||
(data: LoginParamsType) =>
|
(data: LoginParamsType): AppThunk =>
|
||||||
(
|
(dispatch) => {
|
||||||
dispatch: Dispatch<
|
|
||||||
ActionsType | SetAppStatusActionType | SetAppErrorActionType
|
|
||||||
>
|
|
||||||
) => {
|
|
||||||
dispatch(setAppStatusAC('loading'))
|
dispatch(setAppStatusAC('loading'))
|
||||||
authAPI
|
authAPI
|
||||||
.login(data)
|
.login(data)
|
||||||
@@ -54,36 +48,20 @@ export const loginTC =
|
|||||||
handleServerNetworkError(error, dispatch)
|
handleServerNetworkError(error, dispatch)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
export const logoutTC =
|
|
||||||
() =>
|
|
||||||
(
|
|
||||||
dispatch: Dispatch<
|
|
||||||
ActionsType | SetAppStatusActionType | SetAppErrorActionType
|
|
||||||
>
|
|
||||||
) => {
|
|
||||||
dispatch(setAppStatusAC('loading'))
|
|
||||||
authAPI
|
|
||||||
.logout()
|
|
||||||
.then((res) => {
|
|
||||||
if (res.data.resultCode === 0) {
|
|
||||||
dispatch(setIsLoggedInAC(false))
|
|
||||||
dispatch(setAppStatusAC('succeeded'))
|
|
||||||
} else {
|
|
||||||
handleServerAppError(res.data, dispatch)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
handleServerNetworkError(error, dispatch)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// types
|
export const logoutTC = (): AppThunk => (dispatch) => {
|
||||||
|
dispatch(setAppStatusAC('loading'))
|
||||||
type ActionsType = ReturnType<typeof setIsLoggedInAC>
|
authAPI
|
||||||
type InitialStateType = {
|
.logout()
|
||||||
isLoggedIn: boolean
|
.then((res) => {
|
||||||
|
if (res.data.resultCode === 0) {
|
||||||
|
dispatch(setIsLoggedInAC(false))
|
||||||
|
dispatch(setAppStatusAC('succeeded'))
|
||||||
|
} else {
|
||||||
|
handleServerAppError(res.data, dispatch)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
handleServerNetworkError(error, dispatch)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type ThunkDispatch = Dispatch<
|
|
||||||
ActionsType | SetAppStatusActionType | SetAppErrorActionType
|
|
||||||
>
|
|
||||||
|
|||||||
Reference in New Issue
Block a user