blob: 4a59d630295c0ee0a11c25f00cd04d6ea8c99b09 [file] [log] [blame]
import { Action, ActionReducer, INIT } from '@ngrx/store';
import * as communicationTypesActions from '@shared/store/actions/admin/communication-types.action';
import {
createFormGroupState,
createFormStateReducerWithUpdate,
updateGroup,
FormGroupState,
FormState,
SetValueAction,
validate,
} from 'ngrx-forms';
import { CommunicationType } from '@shared/models';
import { required } from 'ngrx-forms/validation';
export const FORM_ID = 'communicationTypesDetailForm';
/**
* The initial state of the details form.
*/
export const INITIAL_STATE: FormGroupState<CommunicationType> = createFormGroupState<
CommunicationType
>(FORM_ID, new CommunicationType({editable: true}));
/**
* Validation function.
*/
export const validateForm: ActionReducer<FormState<CommunicationType>> = createFormStateReducerWithUpdate<CommunicationType>(
updateGroup<CommunicationType>(
{
type: validate(required),
description: validate(required),
}
)
);
/**
* The reducer function.
*/
export function reducer(
state: FormGroupState<CommunicationType> = INITIAL_STATE,
action: Action
): FormGroupState<CommunicationType> {
if (!action || action.type === INIT) {
return INITIAL_STATE;
}
if (action.type === communicationTypesActions.loadCommunicationTypeSuccess.type) {
const CommunicationType: CommunicationType = <CommunicationType>action['payload'];
const setValueAction: SetValueAction<any> = new SetValueAction<any>(FORM_ID, CommunicationType);
return validateForm(state, setValueAction);
}
return validateForm(state, action);
}
export const getFormState = (state: FormGroupState<CommunicationType>) => state;