Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
dex 1417 - require both sighting time fields when reporting sighting (#…
Browse files Browse the repository at this point in the history
…456)

* Add asterisks to SpecifiedTimeEditor fields

* Update report sighting validation of specifiedTime fields

* Move time validations to variables for readability
  • Loading branch information
Emily-Ke authored Sep 28, 2022
1 parent b3a46e8 commit 4aa18da
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 18 deletions.
1 change: 1 addition & 0 deletions locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@
"BACK_TO_PHOTOS": "Back to photographs",
"BACK_TO_SELECTION": "Back to selection",
"INCOMPLETE_FIELD": "{fieldName} is a required field.",
"INCOMPLETE_TIME_SPECIFICITY": "Enter the Year information as well as the Time Specificity.",
"CONTINUE_WITHOUT_PHOTOGRAPHS": "Continue without photographs",
"LAST_SIGHTING_DATE_RANGE": "Last sighting date",
"LAST_SIGHTING_DATE_RANGE_DESCRIPTION": "Date of this individual's most recent sighting.",
Expand Down
3 changes: 2 additions & 1 deletion src/components/fields/edit/SpecifiedTimeEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default function SpecifiedTimeEditor(props) {
* which messes up display if this component is a flex child. */
return (
<div>
<FormCore schema={{ ...schema, required: false }} width={width}>
<FormCore schema={schema} width={width}>
<InputLabel>
{intl.formatMessage({ id: 'SIGHTING_TIME_SPECIFICITY' })}
</InputLabel>
Expand Down Expand Up @@ -120,6 +120,7 @@ export default function SpecifiedTimeEditor(props) {
'aria-label': intl.formatMessage({ id: 'CHANGE_DATE' }),
}}
views={dateInputViews}
required={get(schema, 'required', false)}
{...rest}
/>
</div>
Expand Down
82 changes: 65 additions & 17 deletions src/pages/reportSighting/ReportForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export default function ReportForm({
const [dialogOpen, setDialogOpen] = useState(false);
const [incompleteFields, setIncompleteFields] = useState([]);
const [termsError, setTermsError] = useState(false);
const [formErrorId, setFormErrorId] = useState(null);

const [sightingFormValues, setSightingFormValues] = useState({});
const [customSightingFormValues, setCustomSightingFormValues] =
Expand Down Expand Up @@ -169,7 +170,10 @@ export default function ReportForm({
} = usePostAssetGroup();

const showErrorAlertBox =
incompleteFields.length > 0 || termsError || postAssetGroupError;
incompleteFields.length > 0 ||
termsError ||
postAssetGroupError ||
formErrorId;

const hasSightingTypeAndNotAuthenticated =
sightingType && !authenticated;
Expand Down Expand Up @@ -246,21 +250,18 @@ export default function ReportForm({
<Text variant="body2">{postAssetGroupError}</Text>
)}
{termsError && <Text variant="body2" id="TERMS_ERROR" />}
{formErrorId && <Text variant="body2" id={formErrorId} />}
{incompleteFields.map(incompleteField => (
<p
style={{ margin: '4px 0' }}
<Text
key={incompleteField.name}
>
<Text
variant="body2"
id="INCOMPLETE_FIELD"
values={{
fieldName: intl.formatMessage({
id: incompleteField.labelId,
}),
}}
/>
</p>
variant="body2"
id="INCOMPLETE_FIELD"
values={{
fieldName: intl.formatMessage({
id: incompleteField.labelId,
}),
}}
/>
))}
</CustomAlert>
</Grid>
Expand All @@ -276,21 +277,68 @@ export default function ReportForm({
>
<Button
onClick={async () => {
// check that required fields are complete
// check that required fields are complete.
// specifiedTime field is required, but the logic and message
// are different from the other fields
const nextIncompleteFields =
defaultSightingSchemas.filter(
field =>
field.required &&
field.defaultValue ===
sightingFormValues[field.name],
sightingFormValues[field.name] &&
field.name !== 'specifiedTime',
);
setIncompleteFields(nextIncompleteFields);

// check that specifiedTime fields are complete
let nextFormErrorId = null;
const formTimeSpecificity = get(sightingFormValues, [
'specifiedTime',
'timeSpecificity',
]);
const formTime = get(sightingFormValues, [
'specifiedTime',
'time',
]);

const specifiedTimeField =
defaultSightingSchemas.find(
field => field.name === 'specifiedTime',
) || {};

const defaultTimeSpecificity = get(specifiedTimeField, [
'defaultValue',
'timeSpecificity',
]);

const defaultTime = get(specifiedTimeField, [
'defaultValue',
'time',
]);

const isTimeSpecificityDefault =
formTimeSpecificity === defaultTimeSpecificity;

const isTimeDefault = formTime === defaultTime;

if (
!formTimeSpecificity ||
isTimeSpecificityDefault ||
!formTime ||
isTimeDefault
) {
nextFormErrorId = 'INCOMPLETE_TIME_SPECIFICITY';
}

setFormErrorId(nextFormErrorId);

// check that terms and conditions were accepted
setTermsError(!acceptedTerms);

const formValid =
nextIncompleteFields.length === 0 && acceptedTerms;
nextIncompleteFields.length === 0 &&
acceptedTerms &&
!nextFormErrorId;

if (formValid) {
const report =
Expand Down

0 comments on commit 4aa18da

Please sign in to comment.