department-of-veterans-affairs/vets-website

View on GitHub
src/applications/discharge-wizard/components/v2/questions/Intention.jsx

Summary

Maintainability
C
1 day
Test Coverage
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import {
  QUESTION_MAP,
  RESPONSES,
  SHORT_NAME_MAP,
} from '../../../constants/question-data-map';
import RadioGroup from './shared/RadioGroup';
import { updateIntention } from '../../../actions';
import { pageSetup } from '../../../utilities/page-setup';
import { ROUTES } from '../../../constants';

const Intention = ({
  formResponses,
  setIntention,
  router,
  viewedIntroPage,
}) => {
  const [formError, setFormError] = useState(false);
  const shortName = SHORT_NAME_MAP.INTENTION;
  const H1 = QUESTION_MAP[shortName];
  const intention = formResponses[shortName];
  const { INTENTION_YES, INTENTION_NO } = RESPONSES;

  useEffect(
    () => {
      pageSetup(H1);
    },
    [H1],
  );

  useEffect(
    () => {
      if (!viewedIntroPage) {
        router.push(ROUTES.HOME);
      }
    },
    [router, viewedIntroPage],
  );

  return (
    <RadioGroup
      formError={formError}
      formResponses={formResponses}
      formValue={intention}
      H1={H1}
      responses={[INTENTION_YES, INTENTION_NO]}
      router={router}
      setFormError={setFormError}
      shortName={shortName}
      testId="duw-intention"
      valueSetter={setIntention}
    />
  );
};

Intention.propTypes = {
  formResponses: PropTypes.object,
  router: PropTypes.shape({
    push: PropTypes.func,
  }),
  setIntention: PropTypes.func,
  viewedIntroPage: PropTypes.bool,
};

const mapStateToProps = state => ({
  formResponses: state?.dischargeUpgradeWizard?.duwForm?.form,
  viewedIntroPage: state?.dischargeUpgradeWizard?.duwForm?.viewedIntroPage,
});

const mapDispatchToProps = {
  setIntention: updateIntention,
};

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(Intention);