Skip to content

Commit

Permalink
[open-formulieren/open-forms#5006] Updated addressNL component to man…
Browse files Browse the repository at this point in the history
…ually fill in city and streetname

Until now the addressNL component would retrieve the city and street
name besed on the postcode nd husenumber. These fields were always
read-only but now we want to be able to fill in city and street name if
somthing goes wrong with the API call.
  • Loading branch information
vaszig committed Jan 16, 2025
1 parent 9768bdd commit 29b2e55
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/formio/components/AddressNL.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import {Formik, useFormikContext} from 'formik';
import debounce from 'lodash/debounce';
import {useContext, useEffect} from 'react';
import {useContext, useEffect, useState} from 'react';
import {createRoot} from 'react-dom/client';
import {Formio} from 'react-formio';
import {FormattedMessage, IntlProvider, defineMessages, useIntl} from 'react-intl';
Expand Down Expand Up @@ -77,6 +77,7 @@ export default class AddressNL extends Field {
city: '',
streetName: '',
secretStreetCity: '',
autoPopulated: false,
};
}

Expand Down Expand Up @@ -199,6 +200,14 @@ const FIELD_LABELS = defineMessages({
description: 'Label for addressNL houseNumber input',
defaultMessage: 'House number',
},
streetName: {
description: 'Label for addressNL streetName input',
defaultMessage: 'Street name',
},
city: {
description: 'Label for addressNL city input',
defaultMessage: 'City',
},
});

const addressNLSchema = (required, intl, {postcode = {}, city = {}}) => {
Expand All @@ -216,6 +225,7 @@ const addressNLSchema = (required, intl, {postcode = {}, city = {}}) => {
});
let postcodeSchema = z.string().regex(postcodeRegex, {message: postcodeErrorMessage});

let streetNameSchema = z.string();

Check warning on line 228 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L228

Added line #L228 was not covered by tests
const {pattern: cityPattern = '', errorMessage: cityErrorMessage = ''} = city;
let citySchema = z.string();
if (cityPattern) {
Expand All @@ -233,14 +243,15 @@ const addressNLSchema = (required, intl, {postcode = {}, city = {}}) => {
}),
});
if (!required) {
postcodeSchema = postcodeSchema.optional();
houseNumberSchema = houseNumberSchema.optional();
streetNameSchema = streetNameSchema.optional();
citySchema = citySchema.optional();

Check warning on line 247 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L246-L247

Added lines #L246 - L247 were not covered by tests
}

return z
.object({
postcode: postcodeSchema,
city: citySchema.optional(),
streetName: streetNameSchema,
city: citySchema,
houseNumber: houseNumberSchema,
houseLetter: z
.string()
Expand Down Expand Up @@ -300,13 +311,15 @@ const AddressNLForm = ({initialValues, required, deriveAddress, layout, setFormi
openForms: {components: nestedComponents},
},
} = useContext(ConfigContext);
const {postcode, city} = nestedComponents || {};
const {postcode, city, streetName} = nestedComponents || {};

const postcodePattern = postcode?.validate?.pattern;
const postcodeError = postcode?.translatedErrors[intl.locale].pattern;
const cityPattern = city?.validate?.pattern;
const cityError = city?.translatedErrors[intl.locale].pattern;
const streetNameError = streetName?.translatedErrors[intl.locale].pattern;

Check warning on line 320 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L320

Added line #L320 was not covered by tests

console.log(streetNameError, cityError);

Check warning on line 322 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L322

Added line #L322 was not covered by tests
const errorMap = (issue, ctx) => {
switch (issue.code) {
case z.ZodIssueCode.invalid_type: {
Expand Down Expand Up @@ -351,6 +364,9 @@ const AddressNLForm = ({initialValues, required, deriveAddress, layout, setFormi
pattern: cityPattern,
errorMessage: cityError,
},
streetName: {
errorMessage: streetNameError,
},
}),
{errorMap}
)}
Expand All @@ -368,6 +384,7 @@ const AddressNLForm = ({initialValues, required, deriveAddress, layout, setFormi
const FormikAddress = ({required, setFormioValues, deriveAddress, layout}) => {
const {values, isValid, setFieldValue} = useFormikContext();
const {baseUrl} = useContext(ConfigContext);
const [isAddressDisabled, setAddressDisabled] = useState(true);

Check warning on line 387 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L387

Added line #L387 was not covered by tests
const useColumns = layout === 'doubleColumn';

useEffect(() => {
Expand All @@ -394,6 +411,12 @@ const FormikAddress = ({required, setFormioValues, deriveAddress, layout}) => {
setFieldValue('city', data['city']);
setFieldValue('streetName', data['streetName']);
setFieldValue('secretStreetCity', data['secretStreetCity']);

// mark the auto-filled fields as populated and disabled when they have been both
// retrieved from the API and they do have a value
const dataRetrieved = !!(data['city'] && data['streetName']);
setAddressDisabled(dataRetrieved);
setFieldValue('autoPopulated', dataRetrieved);

Check warning on line 419 in src/formio/components/AddressNL.jsx

View check run for this annotation

Codecov / codecov/patch

src/formio/components/AddressNL.jsx#L418-L419

Added lines #L418 - L419 were not covered by tests
};

return (
Expand Down Expand Up @@ -434,7 +457,8 @@ const FormikAddress = ({required, setFormioValues, deriveAddress, layout}) => {
defaultMessage="Street name"
/>
}
disabled
disabled={isAddressDisabled}
isRequired={required}
/>
<TextField
name="city"
Expand All @@ -444,7 +468,8 @@ const FormikAddress = ({required, setFormioValues, deriveAddress, layout}) => {
defaultMessage="City"
/>
}
disabled
disabled={isAddressDisabled}
isRequired={required}
/>
</>
)}
Expand Down

0 comments on commit 29b2e55

Please sign in to comment.