All files / @tech/auth/store reducers.js

100% Statements 25/25
100% Branches 7/7
100% Functions 11/11
100% Lines 15/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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69                            1x                                 1x 1x 1x 1x 1x 1x 1x 1x     2x         1x   1x                           13x 11x     2x    
import {
  SET_AUTH_USERNAME,
  SET_AUTH_PASSWORD,
  SET_AUTH_ACTIVATED,
  SET_AUTH_BIOMETRY_ENABLED,
  SET_AUTH_UUID,
  SET_AUTH_LOADING,
  SET_AUTH_STATE,
  SET_AUTH_ERROR,
  SET_ACCESS_TOKEN,
  SET_AUTH_OTP,
  AUTH_STATE_UNAUTHORIZED,
} from './constants';
 
const initialState = {
  username: '',
  password: '',
  activated: false,
  accessToken: null,
  otp: null,
  uuid: null,
  loading: false,
  biometry: false,
  state: AUTH_STATE_UNAUTHORIZED,
  error: {
    code: 0,
    type: null,
    message: null,
  },
};
 
const setUsername = (state, action) => ({ ...state, username: action.payload.username });
const setPassword = (state, action) => ({ ...state, password: action.payload.password });
const setOtp = (state, action) => ({ ...state, otp: action.payload.otp });
const setUuid = (state, action) => ({ ...state, uuid: action.payload.uuid });
const setActivated = (state, action) => ({ ...state, activated: action.payload.activated });
const setBiometryEnabled = (state, action) => ({ ...state, biometry: action.payload.biometry });
const setAuthLoading = (state, action) => ({ ...state, loading: action.payload.loading });
const setAccessToken = (state, action) => ({ ...state, accessToken: action.payload.accessToken });
 
// lastAlive will stop being tracked when going unauthorized
const setAuthState = (state, action) => ({
  ...state,
  state: action.payload.state,
  lastAlive: action.payload.state === AUTH_STATE_UNAUTHORIZED ? null : state.lastAlive,
});
const setAuthError = (state, action) => ({ ...state, error: action.payload.error });
 
const FUNCTION_BY_ACTION = {
  [SET_AUTH_USERNAME]: setUsername,
  [SET_AUTH_PASSWORD]: setPassword,
  [SET_AUTH_ACTIVATED]: setActivated,
  [SET_ACCESS_TOKEN]: setAccessToken,
  [SET_AUTH_LOADING]: setAuthLoading,
  [SET_AUTH_STATE]: setAuthState,
  [SET_AUTH_ERROR]: setAuthError,
  [SET_AUTH_UUID]: setUuid,
  [SET_AUTH_OTP]: setOtp,
  [SET_AUTH_BIOMETRY_ENABLED]: setBiometryEnabled,
};
 
export default (state = initialState, action) => {
  if (action && action.type in FUNCTION_BY_ACTION) {
    return FUNCTION_BY_ACTION[action.type](state, action);
  }
 
  return state;
};