login.reducer.ts 2.19 KB
Newer Older
David Burke's avatar
David Burke committed
1 2 3
import {
  FormGroupState,
  createFormGroupState,
David Burke's avatar
David Burke committed
4 5
  formGroupReducer,
  validate,
David Burke's avatar
David Burke committed
6 7
  updateGroup,
  markAsSubmitted
David Burke's avatar
David Burke committed
8
} from "ngrx-forms";
David Burke's avatar
David Burke committed
9
import { required, minLength, pattern } from "ngrx-forms/validation";
David Burke's avatar
David Burke committed
10
import { environment } from "../../../environments/environment";
David Burke's avatar
David Burke committed
11

David Burke's avatar
David Burke committed
12
import { AccountActions, AccountActionTypes } from "../account.actions";
David Burke's avatar
David Burke committed
13
import { ILoginForm } from ".";
David Burke's avatar
David Burke committed
14
import { DEFAULT_API } from "../../constants";
David Burke's avatar
David Burke committed
15 16 17 18 19 20

const FORM_ID = "Login Form";

const initialFormState = createFormGroupState<ILoginForm>(FORM_ID, {
  email: "",
  password: "",
David Burke's avatar
David Burke committed
21 22
  rememberMe: environment.extension || environment.nativescript ? true : false,
  url: environment.extension || environment.nativescript ? DEFAULT_API : ""  
David Burke's avatar
David Burke committed
23
});
24 25

export interface ILoginState {
David Burke's avatar
David Burke committed
26
  form: FormGroupState<ILoginForm>;
27 28 29 30 31 32
  hasStarted: boolean;
  hasFinished: boolean;
  errorMessage: string | null;
}

export const initialState: ILoginState = {
David Burke's avatar
David Burke committed
33
  form: initialFormState,
34 35
  hasStarted: false,
  hasFinished: false,
36
  errorMessage: null
37 38
};

David Burke's avatar
David Burke committed
39 40 41 42 43
const updateFormGroup = updateGroup<ILoginForm>({
  email: validate([required, pattern(/^[^\s@][email protected][^\s@]+\.[^\s@]{2,}$/)]),
  password: validate([required, minLength(6)])
});

44 45 46 47
export function reducer(
  state = initialState,
  action: AccountActions
): ILoginState {
David Burke's avatar
David Burke committed
48
  const form = formGroupReducer(updateFormGroup(state.form), action);
David Burke's avatar
David Burke committed
49 50 51 52
  if (form !== state.form) {
    state = { ...state, form };
  }

53
  switch (action.type) {
David Burke's avatar
David Burke committed
54
    case AccountActionTypes.LOGIN:
55 56
      return {
        ...state,
David Burke's avatar
David Burke committed
57
        form: markAsSubmitted(state.form),
58 59
        hasStarted: true,
        hasFinished: false,
60
        errorMessage: null
61 62
      };

David Burke's avatar
David Burke committed
63
    case AccountActionTypes.LOGIN_SUCCESS:
64 65 66
      return {
        ...state,
        hasStarted: false,
67
        hasFinished: true
68 69
      };

David Burke's avatar
David Burke committed
70
    case AccountActionTypes.LOGIN_FAILURE:
71 72 73
      return {
        ...state,
        hasStarted: false,
74
        errorMessage: action.payload
75 76 77 78 79 80 81 82 83 84
      };

    default:
      return state;
  }
}

export const getHasStarted = (state: ILoginState) => state.hasStarted;
export const getHasFinished = (state: ILoginState) => state.hasFinished;
export const getErrorMessage = (state: ILoginState) => state.errorMessage;
David Burke's avatar
David Burke committed
85
export const getForm = (state: ILoginState) => state.form;