import * as React from 'react' ; import { UserNoPassword } from 'YesterTech/types' ; const initialState = { authenticated : false , user : null , } ; const AuthStateContext = React . createContext < AuthState > ( initialState ) ; const AuthDispatchContext = React . createContext < AuthDispatch > ( function dispatch ( ) { } ) ; export type AuthActionTypes = 'LOGIN' | 'LOGOUT' ; export const AuthStateProvider = ( { children } ) => { const [ state , dispatch ] = React . useReducer ( function authReducer ( state , action ) : AuthState { switch ( action . type ) { case 'LOGIN' : { return { ... state , authenticated : true , user : action . user } ; } case 'LOGOUT' : { return { ... initialState } ; } default : return state ; } } , initialState ) ; return ( < AuthDispatchContext . Provider value = { dispatch } > < AuthStateContext . Provider value = { state } > { children } < /AuthStateContext.Provider> < /AuthDispatchContext.Provider>); } ; export function useAuthDispatch ( ) { return React . useContext ( AuthDispatchContext ) ; } export function useAuthState ( ) { return React . useContext ( AuthStateContext ) ; } type AuthState = { authenticated : boolean ; user : null | UserNoPassword ; } ; type AuthDispatch = React . Dispatch < AuthActions > ; type AuthActions = { type : 'LOGIN' ; user : UserNoPassword ; } | { type : 'LOGOUT' ; } ;