|
| 1 | +import {UnorderedList, UnorderedListItem} from '@utrecht/component-library-react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import {FormattedMessage, useIntl} from 'react-intl'; |
| 4 | + |
| 5 | +const ErrorType = PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]); |
| 6 | + |
| 7 | +const normalizeError = error => (Array.isArray(error) ? error : [error]); |
| 8 | + |
| 9 | +const StepValidationErrors = ({errors, name, stepData}) => { |
| 10 | + const intl = useIntl(); |
| 11 | + const {nonFieldErrors = [], data = {}} = errors; |
| 12 | + |
| 13 | + const allErrorMessages = [...normalizeError(nonFieldErrors)]; |
| 14 | + |
| 15 | + // related the data errors to their matching components |
| 16 | + Object.entries(data).map(([key, errorMessage]) => { |
| 17 | + const normalizedErrorMessage = normalizeError(errorMessage); |
| 18 | + const stepDataItem = stepData.find(item => item.component.key === key); |
| 19 | + |
| 20 | + for (const error of normalizedErrorMessage) { |
| 21 | + const message = intl.formatMessage( |
| 22 | + { |
| 23 | + description: 'Overview page, validation error message for step field.', |
| 24 | + defaultMessage: "Field ''{label}'': {error}", |
| 25 | + }, |
| 26 | + { |
| 27 | + label: stepDataItem.name, |
| 28 | + error: error, |
| 29 | + } |
| 30 | + ); |
| 31 | + allErrorMessages.push(message); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + return ( |
| 36 | + <> |
| 37 | + <FormattedMessage |
| 38 | + description="Overview page, title before listing the validation errors for a step" |
| 39 | + defaultMessage="Problems in form step ''{name}''" |
| 40 | + values={{name}} |
| 41 | + /> |
| 42 | + <UnorderedList className="utrecht-unordered-list--distanced"> |
| 43 | + {allErrorMessages.map(error => ( |
| 44 | + <UnorderedListItem key={error}>{error}</UnorderedListItem> |
| 45 | + ))} |
| 46 | + </UnorderedList> |
| 47 | + </> |
| 48 | + ); |
| 49 | +}; |
| 50 | + |
| 51 | +StepValidationErrors.propTypes = { |
| 52 | + errors: PropTypes.shape({ |
| 53 | + nonFieldErrors: ErrorType, |
| 54 | + // keys are the component key names |
| 55 | + data: PropTypes.objectOf(ErrorType), |
| 56 | + }).isRequired, |
| 57 | + name: PropTypes.string.isRequired, |
| 58 | + stepData: PropTypes.arrayOf( |
| 59 | + PropTypes.shape({ |
| 60 | + name: PropTypes.string.isRequired, |
| 61 | + value: PropTypes.oneOfType([ |
| 62 | + PropTypes.string, |
| 63 | + PropTypes.object, |
| 64 | + PropTypes.array, |
| 65 | + PropTypes.number, |
| 66 | + PropTypes.bool, |
| 67 | + ]), |
| 68 | + component: PropTypes.object.isRequired, |
| 69 | + }) |
| 70 | + ).isRequired, |
| 71 | +}; |
| 72 | + |
| 73 | +/** |
| 74 | + * Render the validation errors received from the backend. |
| 75 | + */ |
| 76 | +const ValidationErrors = ({errors, summaryData}) => { |
| 77 | + const {steps = []} = errors; |
| 78 | + if (steps.length === 0) return null; |
| 79 | + return ( |
| 80 | + <UnorderedList className="utrecht-unordered-list--distanced"> |
| 81 | + {steps.map((stepErrors, index) => ( |
| 82 | + <UnorderedListItem key={summaryData[index].slug}> |
| 83 | + <StepValidationErrors |
| 84 | + errors={stepErrors} |
| 85 | + name={summaryData[index].name} |
| 86 | + stepData={summaryData[index].data} |
| 87 | + /> |
| 88 | + </UnorderedListItem> |
| 89 | + ))} |
| 90 | + </UnorderedList> |
| 91 | + ); |
| 92 | +}; |
| 93 | + |
| 94 | +ValidationErrors.propTypes = { |
| 95 | + /** |
| 96 | + * Validation errors as reconstructed from the backend invalidParams |
| 97 | + */ |
| 98 | + errors: PropTypes.shape({ |
| 99 | + steps: PropTypes.arrayOf( |
| 100 | + PropTypes.shape({ |
| 101 | + nonFieldErrors: ErrorType, |
| 102 | + // keys are the component key names |
| 103 | + data: PropTypes.objectOf(ErrorType), |
| 104 | + }) |
| 105 | + ), |
| 106 | + }), |
| 107 | + /** |
| 108 | + * Array of summary data per step. |
| 109 | + */ |
| 110 | + summaryData: PropTypes.arrayOf( |
| 111 | + PropTypes.shape({ |
| 112 | + name: PropTypes.string.isRequired, |
| 113 | + slug: PropTypes.string.isRequired, |
| 114 | + data: PropTypes.arrayOf( |
| 115 | + PropTypes.shape({ |
| 116 | + name: PropTypes.string.isRequired, |
| 117 | + value: PropTypes.oneOfType([ |
| 118 | + PropTypes.string, |
| 119 | + PropTypes.object, |
| 120 | + PropTypes.array, |
| 121 | + PropTypes.number, |
| 122 | + PropTypes.bool, |
| 123 | + ]), |
| 124 | + component: PropTypes.object.isRequired, |
| 125 | + }) |
| 126 | + ).isRequired, |
| 127 | + }) |
| 128 | + ), |
| 129 | +}; |
| 130 | + |
| 131 | +export default ValidationErrors; |
0 commit comments