How to use the ngrx-forms.createFormStateReducerWithUpdate function in ngrx-forms

To help you get started, we’ve selected a few ngrx-forms examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github MrWolfZ / ngrx-forms / example-app / src / app / material-example / material.reducer.ts View on Github external
export const INITIAL_STATE = createFormGroupState(FORM_ID, {
  userName: '',
  createAccount: true,
  password: {
    password: '',
    confirmPassword: '',
  },
  sex: '',
  favoriteColor: '',
  hobbies: box([]),
  dateOfBirth: new Date(Date.UTC(1970, 0, 1)).toISOString(),
  agreeToTermsOfUse: false,
});

const validationFormGroupReducer = createFormStateReducerWithUpdate(updateGroup({
  userName: validate(required),
  password: (state, parentState) => {
    if (!parentState.value.createAccount) {
      return disable(state);
    }

    state = enable(state);
    return updateGroup(state, {
      password: validate(required, minLength(8)),
      confirmPassword: validate(equalTo(state.value.password)),
    });
  },
  agreeToTermsOfUse: validate(requiredTrue),
}));

const reducers = combineReducers({
github MrWolfZ / ngrx-forms / example-app / src / app / async-validation / async-validation.reducer.ts View on Github external
}

export class SetSearchResultAction implements Action {
  static readonly TYPE = 'asyncValidation/SET_SEARCH_RESULT';
  readonly type = SetSearchResultAction.TYPE;
  constructor(public results: string[]) { }
}

export const FORM_ID = 'asyncValidation';

export const INITIAL_STATE = createFormGroupState(FORM_ID, {
  searchTerm: '',
  numberOfResultsToShow: 5,
});

const formGroupReducerWithUpdate = createFormStateReducerWithUpdate(updateGroup({
  searchTerm: validate(required),
  numberOfResultsToShow: validate(required, greaterThan(0)),
}));

const reducers = combineReducers({
  formState(s = INITIAL_STATE, a: Action) {
    return formGroupReducerWithUpdate(s, a);
  },
  searchResults(s: string[] = [], a: Action) {
    if (a.type === SetSearchResultAction.TYPE) {
      return (a as SetSearchResultAction).results;
    }

    return s;
  },
});