Last active
April 15, 2022 15:23
-
-
Save asaumet230/c4a3d0649c67e180c8bbc0a2d9c4fd7e to your computer and use it in GitHub Desktop.
Typescript: reducer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useReducer, useEffect } from 'react'; | |
interface AuthState { | |
validando: boolean; | |
token: null | string; | |
username: string; | |
nombre: string; | |
} | |
type LoginPayload = { | |
username: string, | |
nombre: string | |
} | |
type AuthAction = | |
| { type: 'logout' } | |
| { type: 'login', payload: LoginPayload } | |
const initialState: AuthState = { | |
validando: true, | |
token: null, | |
username: '', | |
nombre: '' | |
} | |
const authReducer = (state: AuthState, action: AuthAction): AuthState => { | |
switch (action.type) { | |
case 'logout': | |
return { | |
validando: false, | |
token: null, | |
username: '', | |
nombre: '' | |
} | |
case 'login': | |
return { | |
validando: false, | |
token: 'ABC123', | |
username: action.payload.username, | |
nombre: action.payload.nombre | |
} | |
default: | |
return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment