chore: refactor auth reducer to use rtk

This commit is contained in:
2024-08-17 18:14:19 +02:00
parent d741e6d0dc
commit 7cd60b5c61

View File

@@ -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
>