department-of-veterans-affairs/vets-website

View on GitHub
src/platform/forms/pages/applicantInformation.jsx

Summary

Maintainability
A
3 hrs
Test Coverage
import { pick } from 'lodash';
import applicantDescription from '../components/ApplicantDescription';

import currentOrPastDateUI from 'platform/forms-system/src/js/definitions/currentOrPastDate';
import fullNameUI from '../definitions/fullName';
import * as personId from '../definitions/personId';

import { relationshipLabels, genderLabels } from '../../static-data/labels';

const defaults = prefix => ({
  fields: [
    `${prefix}FullName`,
    `${prefix}SocialSecurityNumber`,
    'view:noSSN',
    `${prefix}DateOfBirth`,
    'gender',
    'relationship',
  ],
  required: [`${prefix}FullName`, `${prefix}DateOfBirth`, 'relationship'],
  labels: {},
  isVeteran: false,
});

/**
 * Returns an applicantInformation page based on the options passed.
 *
 * @param {Object} schema   The full schema for the form
 * @param {Object} options  Options to override the defaults above
 */
export default function applicantInformation(schema, options) {
  // Use the defaults as necessary, but override with the options given
  const prefix = options && options.isVeteran ? 'veteran' : 'relative';
  const mergedOptions = Object.assign({}, defaults(prefix), options);
  const { fields, required, labels } = mergedOptions;

  const possibleProperties = Object.assign({}, schema.properties, {
    'view:noSSN': {
      type: 'boolean',
    },
  });

  return {
    path: 'applicant/information',
    title: 'Applicant information',
    initialData: {},
    uiSchema: Object.assign(
      {},
      {
        'ui:order': fields,
        'ui:description': applicantDescription,
        [`${prefix}FullName`]: fullNameUI,
        [`${prefix}DateOfBirth`]: Object.assign(
          {},
          currentOrPastDateUI('Your date of birth'),
          {
            'ui:errorMessages': {
              pattern: 'Please provide a valid date',
              required: 'Please enter a date',
              futureDate: 'Please provide a valid date',
            },
          },
        ),
        gender: {
          'ui:widget': 'radio',
          'ui:title': 'Gender',
          'ui:options': {
            labels: labels.gender || genderLabels,
          },
        },
        relationship: {
          'ui:widget': 'radio',
          'ui:title':
            'What’s your relationship to the service member whose benefit is being transferred to you?',
          'ui:options': {
            labels: labels.relationship || relationshipLabels,
          },
        },
      },
      personId.uiSchema(prefix, 'view:noSSN'),
    ),
    schema: {
      type: 'object',
      definitions: pick(schema.definitions, [
        'fullName',
        'relationship',
        'ssn',
        'gender',
        'date',
        'vaFileNumber',
      ]),
      required,
      properties: pick(possibleProperties, fields),
    },
  };
}