Commit f5a378c9 authored by Emily Jensen's avatar Emily Jensen

Created actions and states in reducer

parent 6202370f
Pipeline #60778476 passed with stage
in 6 minutes and 39 seconds
......@@ -12,6 +12,7 @@ import * as fromResetPassword from "./reset-password/reset-password.reducer";
import * as fromResetPasswordVerify from "./reset-password/reset-password-verify/reset-password-verify.reducer";
import * as fromSetPassword from "./reset-password/set-password/set-password.reducer";
import * as fromManageBackupCode from "./manage-backup-code/manage-backup-code.reducer";
import * as fromManageMfa from "./manage-mfa/manage-mfa.reducer";
import * as fromRoot from "../app.reducers";
export interface IAccountState {
......@@ -24,6 +25,7 @@ export interface IAccountState {
resetPasswordVerify: fromResetPasswordVerify.IResetPasswordVerifyState;
manageBackupCode: fromManageBackupCode.IState;
setPassword: fromSetPassword.ISetPasswordState;
enableMfa: fromManageMfa.IEnbleMfaState;
}
export const initialState: IAccountState = {
......@@ -35,7 +37,8 @@ export const initialState: IAccountState = {
resetPassword: fromResetPassword.initialState,
resetPasswordVerify: fromResetPasswordVerify.initialState,
manageBackupCode: fromManageBackupCode.initialState,
setPassword: fromSetPassword.initialState
setPassword: fromSetPassword.initialState,
enableMfa: fromManageMfa.initialState
};
export const reducers: ActionReducerMap<IAccountState> = {
......@@ -47,7 +50,8 @@ export const reducers: ActionReducerMap<IAccountState> = {
resetPassword: fromResetPassword.reducer,
resetPasswordVerify: fromResetPasswordVerify.reducer,
setPassword: fromSetPassword.reducer,
manageBackupCode: fromManageBackupCode.reducer
manageBackupCode: fromManageBackupCode.reducer,
enableMfa: fromManageMfa.reducer
};
export interface IState extends fromRoot.IState {
......@@ -296,3 +300,20 @@ export const getSetPasswordBackupCode = createSelector(
selectSetPasswordState,
fromSetPassword.getBackupCode
);
export const selectManageMfaState = createSelector(
selectAccountState,
(state: IAccountState) => state.enableMfa
);
export const getEnableMfaForm = createSelector(
selectManageMfaState,
fromManageMfa.getForm
);
export const getMfaEnabled = createSelector(
selectManageMfaState,
fromManageMfa.getMfaEnabled
);
export const getMfaErrorMessage = createSelector(
selectManageMfaState,
fromManageMfa.getErrorMessage
);
......@@ -4,9 +4,7 @@ export enum ManageMfaActionTypes {
ENABLE_MFA = "[ENABLE MFA] Enable",
ENABLE_MFA_SUCCESS = "[ENABLE MFA] Enable Success",
ENABLE_MFA_FAILURE = "[ENABLE MFA] Enable Failure",
DISABLE_MFA = "[DISABLE MFA] Disable",
DISABLE_MFA_SUCCESS = "[DISABLE MFA] Disable Success",
DISABLE_MFA_FAILURE = "[DISABLE MFA] Disable Failure"
RESET_FORM = "[Reset] = Reset Form"
}
export class EnableMfa implements Action {
......@@ -19,24 +17,16 @@ export class EnableMfaSuccess implements Action {
export class EnableMfaFailure implements Action {
readonly type = ManageMfaActionTypes.ENABLE_MFA_FAILURE;
}
export class DisableMfa implements Action {
readonly type = ManageMfaActionTypes.DISABLE_MFA;
}
export class DisableMfaSuccess implements Action {
readonly type = ManageMfaActionTypes.DISABLE_MFA_SUCCESS;
constructor(public payload: string[]) {}
}
export class DisableMfaFailure implements Action {
readonly type = ManageMfaActionTypes.DISABLE_MFA_FAILURE;
export class ResetForm implements Action {
readonly type = ManageMfaActionTypes.RESET_FORM;
}
export type ManageMfaActions =
| EnableMfa
| EnableMfaSuccess
| EnableMfaFailure
| DisableMfa
| DisableMfaSuccess
| DisableMfaFailure;
| ResetForm;
......@@ -2,31 +2,74 @@ import {
FormGroupState,
validate,
updateGroup,
createFormGroupState
createFormGroupState,
createFormStateReducerWithUpdate
} from "ngrx-forms";
import { required } from "ngrx-forms/validation";
import { IBaseFormState } from "~/app/utils/interfaces";
import { ManageMfaActions, ManageMfaActionTypes } from "./manage-mfa.actions";
export const FORM_ID = "Enable MFA Form";
export interface IEnableMfaForm {
verificationCode: string;
verificationCode: string | null;
}
export interface IState extends IBaseFormState {
enableForm: FormGroupState<IEnableMfaForm>;
export interface IEnbleMfaState {
form: FormGroupState<IEnableMfaForm>;
mfaEnabled: boolean;
errorMessage: string[] | null;
mfaEnabled: boolean | null;
}
const validateAndUpdateFormState = updateGroup<IEnableMfaForm>({
verificationCode: validate(required)
});
export const mfaEnabled = (state: IState) => state.mfaEnabled;
export const initialFormState = validateAndUpdateFormState(
createFormGroupState<IEnableMfaForm>(FORM_ID, {
verificationCode: ""
})
);
export const initialState: IEnbleMfaState = {
form: initialFormState,
errorMessage: null,
mfaEnabled: false
};
export const formReducer = createFormStateReducerWithUpdate<IEnableMfaForm>(
validateAndUpdateFormState
);
export function reducer(
state = initialState,
action: ManageMfaActions
): IEnbleMfaState {
const form = formReducer(state.form, action);
state = { ...state, form };
switch (action.type) {
case ManageMfaActionTypes.ENABLE_MFA:
return {
...state
};
case ManageMfaActionTypes.ENABLE_MFA_SUCCESS:
return {
...state,
mfaEnabled: true
};
case ManageMfaActionTypes.ENABLE_MFA_FAILURE:
return {
...state,
errorMessage: action.payload
};
case ManageMfaActionTypes.RESET_FORM:
return initialState;
}
return state;
}
export const getForm = (state: IEnbleMfaState) => state.form;
export const getMfaEnabled = (state: IEnbleMfaState) => state.mfaEnabled;
export const getErrorMessage = (state: IEnbleMfaState) => state.errorMessage;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment