Skip to content
Snippets Groups Projects
userReducer.ts 1 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { AnyAction } from 'redux'
    
    import Types from '../actions/types'
    
    
    Björn Modée's avatar
    Björn Modée committed
    // Define a type for users info
    
    interface UserInfo {
      name: string
      email: string
    
    robban64's avatar
    robban64 committed
      role_id: number
      city_id: number
    
      id: number
    
    Björn Modée's avatar
    Björn Modée committed
    // Define a type for the users state
    
    interface UserState {
      authenticated: boolean
      userInfo: UserInfo | null
      loading: boolean
    }
    
    
    Björn Modée's avatar
    Björn Modée committed
    // Define the initial values for the users state
    
    const initialState: UserState = {
    
      authenticated: false,
    
      loading: false,
    
      userInfo: null,
    
    export default function (state = initialState, action: AnyAction) {
    
      switch (action.type) {
        case Types.SET_AUTHENTICATED:
          return {
            ...state,
            authenticated: true,
          }
        case Types.SET_UNAUTHENTICATED:
          return initialState
        case Types.SET_USER:
          return {
            authenticated: true,
            loading: false,
    
            userInfo: action.payload as UserInfo,
    
          }
        case Types.LOADING_USER:
          return {
            ...state,
            loading: true,
          }
        default:
          return state
      }
    }