-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
157 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.noticeBox { | ||
max-width: 640px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import i18n from '@dhis2/d2-i18n' | ||
import { NoticeBox } from '@dhis2/ui' | ||
import React, { useEffect, useRef } from 'react' | ||
import { useFormState } from 'react-final-form' | ||
import css from './DefaultFormErrorNotice.module.css' | ||
import { FormState } from 'final-form' | ||
|
||
const formStateSubscriptions = { | ||
errors: true, | ||
submitError: true, | ||
submitFailed: true, | ||
hasValidationErrors: true, | ||
hasSubmitErrors: true, | ||
dirtySinceLastSubmit: true, | ||
} | ||
|
||
type FormStateErrors = Pick< | ||
FormState<unknown>, | ||
keyof typeof formStateSubscriptions | ||
> | ||
|
||
export function DefaultFormErrorNotice() { | ||
const partialFormState: FormStateErrors = useFormState({ | ||
subscription: formStateSubscriptions, | ||
}) | ||
// only show after trying to submit | ||
if ( | ||
!partialFormState.submitFailed || | ||
(partialFormState.submitFailed && partialFormState.dirtySinceLastSubmit) | ||
) { | ||
return null | ||
} | ||
|
||
if (partialFormState.hasValidationErrors) { | ||
return <ValidationErrors formStateErrors={partialFormState} /> | ||
} | ||
|
||
if (partialFormState.hasSubmitErrors) { | ||
return <ServerSubmitError formStateErrors={partialFormState} /> | ||
} | ||
return null | ||
} | ||
|
||
const ValidationErrors = ({ | ||
formStateErrors, | ||
}: { | ||
formStateErrors: FormStateErrors | ||
}) => { | ||
const ref = useRef<HTMLDivElement>(null) | ||
useEffect(() => { | ||
if (ref.current) { | ||
ref.current.scrollIntoView({ behavior: 'smooth' }) | ||
} | ||
}, []) | ||
return ( | ||
<div ref={ref}> | ||
<NoticeBox | ||
className={css.noticeBox} | ||
warning | ||
title={i18n.t('Validation errors')} | ||
> | ||
<p> | ||
{i18n.t( | ||
'Some fields have validation errors. Please fix them before saving.' | ||
)} | ||
</p> | ||
{formStateErrors.errors && ( | ||
<ErrorList errors={formStateErrors.errors} /> | ||
)} | ||
</NoticeBox> | ||
</div> | ||
) | ||
} | ||
|
||
const ErrorList = ({ errors }: { errors: Record<string, string> }) => { | ||
const labels = getFieldLabelsBestEffort() | ||
|
||
return ( | ||
<ul style={{ padding: '0 16px' }}> | ||
{Object.entries(errors).map(([key, value]) => { | ||
return ( | ||
<li key={key} style={{ display: 'flex', gap: '8px' }}> | ||
<span | ||
style={{ | ||
fontWeight: '600', | ||
}} | ||
> | ||
{labels.get(key) || key}: | ||
</span> | ||
<span>{value}</span> | ||
</li> | ||
) | ||
})} | ||
</ul> | ||
) | ||
} | ||
|
||
const ServerSubmitError = ({ | ||
formStateErrors, | ||
}: { | ||
formStateErrors: FormStateErrors | ||
}) => { | ||
const ref = useRef<HTMLDivElement>(null) | ||
useEffect(() => { | ||
if (ref.current) { | ||
ref.current.scrollIntoView({ behavior: 'smooth' }) | ||
} | ||
}, []) | ||
return ( | ||
<div ref={ref}> | ||
<NoticeBox | ||
error | ||
title={i18n.t('Something went wrong when submitting the form')} | ||
> | ||
<p>{formStateErrors.submitError}</p> | ||
</NoticeBox> | ||
</div> | ||
) | ||
} | ||
|
||
/** | ||
* We don't have a good way to get the translated labels, so for now | ||
* we get these from the DOM. This is a best-effort approach. | ||
* TODO: Find a better way to get the labels, eg. by wrapping Field components | ||
* in a generic component that can register fields with metadata such as labels. | ||
*/ | ||
const getFieldLabelsBestEffort = () => { | ||
const labels = Array.from(document.getElementsByTagName('label')) | ||
.filter((elem) => elem.htmlFor) | ||
.map((elem) => { | ||
const fieldName = elem.htmlFor | ||
const label = elem.firstChild?.textContent | ||
?.replace('(required)', '') | ||
.trim() | ||
return [fieldName, label] as const | ||
}) | ||
return new Map(labels) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters