Skip to content

Commit

Permalink
Merge pull request #1746 from GSA/1737-date-validation-not-required
Browse files Browse the repository at this point in the history
1737 date validation not required
  • Loading branch information
scottqueen-bixal authored Aug 30, 2024
2 parents f09828f + 55da7fe commit 33c9964
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ exports[`Date > renders a match to the previous snapshot 1`] = `
name="applicant_date_of_birth_0_month"
>
<option
disabled=""
value=""
>
-Select-
Expand Down
4 changes: 2 additions & 2 deletions benefit-finder/src/shared/components/Date/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const Date = ({
aria-errormessage={`month-error-description-${id}`}
data-datetype="month"
>
<option value="" key="default" disabled>
<option value="" key="default">
{dateDefaultValue}
</option>
{monthOptions.map((option, i) => (
Expand Down Expand Up @@ -165,7 +165,7 @@ Date.propTypes = {
value: PropTypes.object,
ui: PropTypes.object,
id: PropTypes.string,
invalid: PropTypes.array,
invalid: PropTypes.oneOfType([PropTypes.bool, PropTypes.array]),
}

export default Date
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ exports[`LifeEventSection > renders a match to the previous snapshot 1`] = `
name="applicant_date_of_birth_0_month"
>
<option
disabled=""
value=""
>
-Select-
Expand Down Expand Up @@ -338,7 +337,6 @@ exports[`LifeEventSection > renders a match to the previous snapshot 1`] = `
name="applicant_relationship_to_the_deceased_0"
>
<option
disabled=""
value=""
>
-Select-
Expand Down Expand Up @@ -408,7 +406,6 @@ exports[`LifeEventSection > renders a match to the previous snapshot 1`] = `
name="applicant_marital_status_0"
>
<option
disabled=""
value=""
>
-Select-
Expand Down
45 changes: 27 additions & 18 deletions benefit-finder/src/shared/components/LifeEventSection/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,22 +203,37 @@ const LifeEventSection = ({
* @return {object} object as state
*/
const handleDateChanged = (event, criteriaKey) => {
// if event target is empty check if all values in date are empty
event.target.value.length > 0 && setHasData(true)
window.history.replaceState({}, '', window.location.pathname)
if (dateInputValidation(event) === true) {
apiCalls.PUT.DataDate(
criteriaKey,
currentData,
setCurrentData,
event.target.value,
event.target.id
)
hasError.length > 0 &&
errorHandling.handleCheckForRequiredValues(
requiredFieldsets,
setHasError

async function validUpdate() {
if (dateInputValidation(event) === true) {
apiCalls.PUT.DataDate(
criteriaKey,
currentData,
setCurrentData,
event.target.value,
event.target.id
)
hasError.length > 0 &&
errorHandling.handleCheckForRequiredValues(
requiredFieldsets,
setHasError
)
}
}

validUpdate().then(() => {
errorHandling.getNonRequiredFieldsets(
criteriaKey,
requiredFieldsets,
setRequiredFieldsets,
setHasError,
hasError,
apiCalls.GET.SelectedValueAll(data)
)
})
}

// manage the display of our modal initializer
Expand Down Expand Up @@ -316,7 +331,6 @@ const LifeEventSection = ({
hidden={hidden && hidden}
id={item.fieldset.criteriaKey}
invalid={errorHandling.handleInvalid({
required: item.fieldset.required,
hasError,
criteriaKey: item.fieldset?.criteriaKey,
})}
Expand Down Expand Up @@ -346,7 +360,6 @@ const LifeEventSection = ({
)
}
invalid={errorHandling.handleInvalid({
required: item.fieldset.required,
hasError,
criteriaKey: item.fieldset?.criteriaKey,
fieldSetId,
Expand Down Expand Up @@ -385,14 +398,12 @@ const LifeEventSection = ({
hidden={hidden && hidden}
ui={ui.errorText}
invalid={errorHandling.handleInvalid({
required: item.fieldset.required,
hasError,
criteriaKey: item.fieldset?.criteriaKey,
})}
>
<RadioGroup
invalid={errorHandling.handleInvalid({
required: item.fieldset.required,
hasError,
criteriaKey: item.fieldset?.criteriaKey,
})}
Expand Down Expand Up @@ -430,7 +441,6 @@ const LifeEventSection = ({
hidden={hidden && hidden}
id={item.fieldset.criteriaKey}
invalid={errorHandling.handleInvalid({
required: item.fieldset.required,
hasError,
criteriaKey: item.fieldset?.criteriaKey,
})}
Expand All @@ -454,7 +464,6 @@ const LifeEventSection = ({
parentLegend={item.fieldset.legend}
id={fieldSetId}
invalid={errorHandling.handleInvalid({
required: item.fieldset.required,
hasError,
criteriaKey: item.fieldset?.criteriaKey,
fieldSetId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ exports[`Select > renders a match to the previous snapshot 1`] = `
name="applicant_relationship_to_the_deceased"
>
<option
disabled=""
value=""
/>
<option
Expand Down
2 changes: 1 addition & 1 deletion benefit-finder/src/shared/components/Select/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function Select({
data-errormessage={errorMessageValue}
aria-errormessage={`error-description-${htmlFor}`}
>
<option value="" key="default" disabled>
<option value="" key="default">
{select?.defaultValue}
</option>
<Options options={options} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ test('month value should be determined if valid', async () => {
id: monthElement.id,
},
}

expect(dateInputValidation(customEvent)).toBe(true)
})

test('day value should be determined if valid', async () => {
render(<IndexHTML />)
const dayElement = screen.getByTestId('input-day')

expect(dateInputValidation(customEvent)).toBe(false)

customEvent = {
target: {
value: '33',
Expand All @@ -46,6 +46,8 @@ test('day value should be determined if valid', async () => {
id: dayElement.id,
},
}

expect(dateInputValidation(customEvent)).toBe(true)
})

test('year value should be determined if valid', async () => {
Expand Down
3 changes: 2 additions & 1 deletion benefit-finder/src/shared/utils/dateInputValidation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ const dateInputValidation = event => {
}
}

// always return true for month values so we can select default options
if (event.target.id.includes('month')) {
return event.target.value.length >= 1
return true
}
}

Expand Down
55 changes: 53 additions & 2 deletions benefit-finder/src/shared/utils/errorHandling/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// import * as apiCalls from '../../api/apiCalls'

/**
* a function that collect all the required fields in the current step
* @function
Expand All @@ -10,6 +12,54 @@ export const getRequiredFieldsets = (document, setHandler) => {
setHandler(Array.from(requiredNodeList))
}

/**
* a function that collect all the non required fields in the current step which still need to be validated if there are requirements
* @function
*/

// if a date fieldset has values, add it to required fieldsets array even if not attributed as required
export const getNonRequiredFieldsets = (
criteriaKey,
requiredFieldsets,
setHandler,
setHasError,
hasError,
data
) => {
const node = document.getElementById(`${criteriaKey}`)

const dateDataObj = data.filter(item => item.criteriaKey === criteriaKey)

const dateValues = dateDataObj[0].values.value

const isEmpty = obj => {
for (const key in obj) {
if (obj[key] !== '') {
return false // returns false if object has a non-empty string value
}
}
return true // returns true if all values are empty strings
}

const addToRequiredFields = [...requiredFieldsets, node]

const makeUniq = [...new Set(addToRequiredFields)]

const removeFromRequiredFields = makeUniq.filter(
item => !item.id === criteriaKey
)

const removeFromErrorArray = hasError.filter(
item => !item.id.includes(criteriaKey)
)

isEmpty(dateValues) && setHasError(removeFromErrorArray)

isEmpty(dateValues)
? setHandler(removeFromRequiredFields)
: setHandler(makeUniq)
}

export const handleCheckForRequiredValues = async (
requiredFieldsets,
setHasError
Expand Down Expand Up @@ -64,7 +114,7 @@ export const handleCheckForRequiredValues = async (
}

export const handleInvalid = ({
required,
// required,
hasError,
criteriaKey,
fieldSetId,
Expand All @@ -85,11 +135,12 @@ export const handleInvalid = ({
return errorItem.id !== undefined && errorItem.id.includes(fieldSetId)
})

return required && useFilter === true ? hanldeFilter : handleMap
return useFilter === true ? hanldeFilter : handleMap
}

export default {
getRequiredFieldsets,
getNonRequiredFieldsets,
handleCheckForRequiredValues,
handleInvalid,
}

0 comments on commit 33c9964

Please sign in to comment.