diff --git a/integrationTests/cypress/integration/02-apd/01-apd-basics.spec.js b/integrationTests/cypress/integration/02-apd/01-apd-basics.spec.js index 6fafc80167..370f6c25c7 100644 --- a/integrationTests/cypress/integration/02-apd/01-apd-basics.spec.js +++ b/integrationTests/cypress/integration/02-apd/01-apd-basics.spec.js @@ -581,7 +581,10 @@ describe('APD Basics', { tags: ['@apd', '@default'] }, () => { cy.get(`.ds-c-field--day`).click().type(element.dateDay); - cy.get(`.ds-c-field--year`).click().type(element.dateYear); + cy.get(`.ds-c-field--year`) + .click() + .type(element.dateYear) + .blur(); cy.findByRole('button', { name: /Save/i }).click(); diff --git a/web/src/pages/apd/activities/oms/Milestone/MilestoneForm.js b/web/src/pages/apd/activities/oms/Milestone/MilestoneForm.js index 516b6d116b..416b0aeb4d 100644 --- a/web/src/pages/apd/activities/oms/Milestone/MilestoneForm.js +++ b/web/src/pages/apd/activities/oms/Milestone/MilestoneForm.js @@ -1,59 +1,87 @@ import { TextField } from '@cmsgov/design-system'; import PropTypes from 'prop-types'; -import React, { useReducer, forwardRef } from 'react'; +import React, { useEffect, forwardRef } from 'react'; import { connect } from 'react-redux'; +import { useForm, Controller } from 'react-hook-form'; +import { joiResolver } from '@hookform/resolvers/joi'; + import DateField from '../../../../../components/DateField'; +import milestonesSchema from '../../../../../static/schemas/milestones'; + import { saveMilestone as actualSaveMilestone } from '../../../../../redux/actions/editActivity'; const MilestoneForm = forwardRef( - ({ activityIndex, index, item, saveMilestone }, ref) => { + ({ activityIndex, index, item, saveMilestone, setFormValid }, ref) => { MilestoneForm.displayName = 'MilestoneForm'; - function reducer(state, action) { - switch (action.type) { - case 'updateField': - return { - ...state, - [action.field]: action.value - }; - default: - throw new Error( - 'Unrecognized action type provided to OutcomesAndMetricForm reducer' - ); - } - } - - const [state, dispatch] = useReducer(reducer, item); - - const changeDate = (_, dateStr) => - dispatch({ type: 'updateField', field: 'endDate', value: dateStr }); + const { + control, + formState: { errors, isValid }, + getFieldState, + trigger, + getValues + } = useForm({ + defaultValues: { + ...item + }, + mode: 'onBlur', + reValidateMode: 'onBlur', + resolver: joiResolver(milestonesSchema) + }); - const changeName = ({ target: { value } }) => - dispatch({ type: 'updateField', field: 'milestone', value }); + useEffect(() => { + console.log("something changed") + setFormValid(isValid); + }, [isValid, errors]); // eslint-disable-line react-hooks/exhaustive-deps - const handleSubmit = e => { + const onSubmit = e => { e.preventDefault(); - saveMilestone(activityIndex, index, state); + saveMilestone(activityIndex, index, getValues()); }; return ( -