All files / src/apps/authentication/redux reducer.ts

64.71% Statements 11/17
37.5% Branches 3/8
66.67% Functions 2/3
66.67% Lines 10/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                          1x       1x 4x                   3x   1x           1x 1x   1x 2x 1x                    
import { AnyAction } from "redux";
import { errorSelector, loadingSelector } from "../../../redux";
import {
  CLEAR_NEXT_PAGE_ACTION,
  GET_PROFILE_ACTION,
  GET_USER_DETAILS_ACTION,
  GET_USER_ORGS_ACTION,
  LOGIN_ACTION,
  LOGOUT_ACTION,
  SET_NEXT_PAGE_ACTION
} from "./actionTypes";
import { AuthState } from "../types";
 
const initialState: AuthState = {
  isLoggedIn: false
};
 
const reducer = (state = initialState, action: AnyAction) => {
  switch (action.type) {
    case LOGIN_ACTION:
      return { ...state, isLoggedIn: true, token: action.payload.token };
    case LOGOUT_ACTION:
      return { ...initialState };
    case GET_PROFILE_ACTION:
      return { ...state, profile: action.payload };
    case GET_USER_ORGS_ACTION:
      return { ...state, orgs: action.payload };
    default:
      return state;
    case SET_NEXT_PAGE_ACTION:
      return { ...state, nextPage: action.payload };
    case CLEAR_NEXT_PAGE_ACTION:
      return { ...state, nextPage: undefined };
  }
};
 
const authLoadingSelector = loadingSelector(LOGIN_ACTION);
const authErrorsSelector = errorSelector(LOGIN_ACTION);
 
const getUserDetailsLoadingSelector = loadingSelector(GET_USER_DETAILS_ACTION);
const profileSelector = ({ auth }: { auth: AuthState }) => auth.profile;
const orgsSelector = ({ auth }: { auth: AuthState }) => auth.orgs;
 
export {
  reducer as default,
  authLoadingSelector,
  authErrorsSelector,
  getUserDetailsLoadingSelector,
  profileSelector,
  orgsSelector
};