Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Forms): add Bring API connector for postal code validation and autofill city #4554

Merged
merged 22 commits into from
Feb 16, 2025

Conversation

tujoworker
Copy link
Member

@tujoworker tujoworker commented Feb 11, 2025

  • Preview: Bring connector
  • Fully Treeshakeable: Ensures no unused code is bundled.
  • Flexible and Extendable: Designed to adapt to future needs.
  • Leverages Existing APIs: Built on current field props and APIs.

Example code:

import { Form, Connectors } from '@dnb/eufemia/extensions/forms'

// 1. Create a context with the config
const { withConfig, handlerId } = Connectors.createContext({
  fetchConfig: {
    url: '...',
    headers: {
      'X-Mybring-API-Uid': '',
    },
  },
})

// 2. Use the context to create the onChangeValidator and onChange functions
const onChangeValidator = withConfig(Connectors.Bring.postalCode.validator)

// Should we name ".postalCode.onChange" to ".postalCode.autocompleteOnChange" or something like that?
const onChange = withConfig(Connectors.Bring.postalCode.autofill, {
  cityPath: '/city',
})

render(
  <Form.Handler
    id={handlerId}
  >
    <Field.PostalCodeAndCity
      postalCode={{
        path: '/postalCode',
        onChange,
        onChangeValidator,
      }}
      city={{
        path: '/city',
      }}
    />
  </Form.Handler>
)

Copy link

vercel bot commented Feb 11, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
eufemia ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 16, 2025 7:46pm

Copy link

codesandbox-ci bot commented Feb 11, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@langz
Copy link
Contributor

langz commented Feb 12, 2025

Not super duper important, as Norwegian(4-digit postal codes) is the most important.
But when playing with the great example https://eufemia-git-feat-forms-api-connectors-bring-eufemia.vercel.app/uilib/extensions/forms/Connectors/Bring/ by selecting Sweden(5-digit postal codes) as country, the requests seems to be sent when entering a postal code with 4-digits, as well as 5-digits. I don't think we need/should send the request when entering a 4-digit postal code and country is sweden 🤔

Screen.Recording.2025-02-12.at.13.17.16.mov

@tujoworker tujoworker force-pushed the feat/forms-api-connectors-bring branch from dec2537 to 2db1ea0 Compare February 12, 2025 12:44
@tujoworker
Copy link
Member Author

I don't think we need/should send the request when entering a 4-digit postal code and country is Sweden

Yes, I also thought about it. And that's why I think, its still easier to implement with the onBlurValidator, when we always send the request on blur. I get it, that instant feedback is nice, if we know the amount of characters. But when we don't know it, then 🤷‍♂️ (I don't think we should keep a list of all possible lengths. But maybe there are good arguments for that too? Let me know.)

@tujoworker tujoworker force-pushed the feat/forms-api-connectors-bring branch from 3ef31a0 to 4ae9ae9 Compare February 12, 2025 14:25
@tujoworker tujoworker force-pushed the feat/forms-api-connectors-bring branch 2 times, most recently from 9a08611 to 04de67e Compare February 16, 2025 07:01
@@ -89,6 +90,10 @@ This is useful if you want to use a custom schema keyword and `validate` functio

You can also easily generate a Ajv schema from a set of fields (JSX), by using the `log` property on the `Tools.GenerateSchema` component. I will e.gc. console log the generated schema. More info about this feature can be found [on a separate page](/uilib/extensions/forms/Form/schema-validation/#generate-schema-from-fields)
Copy link
Contributor

@langz langz Feb 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does e.gc. mean?

@@ -143,6 +143,7 @@ function PostalCodeAndCity(props: Props) {
inputClassName="dnb-forms-field-postal-code-and-city__postal-code-input"
inputMode="numeric"
autoComplete="postal-code"
data-country-code={country || countryCode}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


Connectors are created by returning a function that takes the `generalConfig` as an argument.

Here is an example of how to create a validator:
Copy link
Contributor

@langz langz Feb 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Here is an example of how to create a validator:
Here is an example of how to create a validator connector:

validator, is that the correct word?
On the line above it says Connectors are created by returning a function that takes the generalConfig as an argument.

Shouldn't we rather provide an example of how to create a connector rather than a validator?
Or perhaps a validator is a connector? Anyhow, it's a bit confusing when reading it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will enhance this section. Also now that we have a response resolver, I think it's worth to mention it.

@langz
Copy link
Contributor

langz commented Feb 16, 2025

In the example provided, could/should we make a new API request when changing the country?
In the following video, it does not trigger an API call and/or validation when changing the country:

Screen.Recording.2025-02-16.at.14.38.47.mov

@langz
Copy link
Contributor

langz commented Feb 16, 2025

I don't think we need/should send the request when entering a 4-digit postal code and country is Sweden

Yes, I also thought about it. And that's why I think, its still easier to implement with the onBlurValidator, when we always send the request on blur. I get it, that instant feedback is nice, if we know the amount of characters. But when we don't know it, then 🤷‍♂️ (I don't think we should keep a list of all possible lengths. But maybe there are good arguments for that too? Let me know.)

Yes, that sounds smart, making it an onBlurValidator would solve this 🙏
Or else we would have to know the possible lengths of postal codes for all countries supported by Bring's API, but that might also change? What do I know

Would be safer to execute the validation on onblur, and isn't that also what we suggest/recommend on regular input fields, to use onBlurValidator?

@tujoworker
Copy link
Member Author

In the example provided, could/should we make a new API request when changing the country?
In the following video, it does not trigger an API call and/or validation when changing the country:

It does actually make a new request when testing with a real API 👍

connector.mov

@tujoworker tujoworker merged commit f2ccd5d into main Feb 16, 2025
10 checks passed
@tujoworker tujoworker deleted the feat/forms-api-connectors-bring branch February 16, 2025 19:55
tujoworker pushed a commit that referenced this pull request Feb 17, 2025
## [10.64.0](v10.63.4...v10.64.0) (2025-02-17)

### ✨ Features

* **Field.Expiry:** add month and year validation ([#4571](#4571)) ([bc4fff9](bc4fff9))
* **Forms:** add `createMinimumAgeVerifier` to Field.NationalIdentityNumber ([#4542](#4542)) ([ceb8c2d](ceb8c2d))
* **Forms:** add `insertAt` to Iterate.PushContainer ([#4555](#4555)) ([6160d78](6160d78))
* **Forms:** add `reverse` to Iterate.Array ([#4556](#4556)) ([0b9ba79](0b9ba79))
* **Forms:** add Bring API connector for postal code validation and autofill city ([#4554](#4554)) ([f2ccd5d](f2ccd5d))
* **Forms:** add feature fields Field.Address.Street & Field.Address.Postal ([#4562](#4562)) ([c546009](c546009))
* **Forms:** provide `{ props }` to the second `onChange`, `onFocus` and `onBlur` event parameter ([#4550](#4550)) ([374ff0e](374ff0e)), closes [#4344](#4344)
* **Forms:** show field indicator inline without shifting content below ([#4553](#4553)) ([45347a3](45347a3))
* **icons:** add digipass_corporate, digipass_private, calendar_add, investor, goal, layout_grid, table, show_pin ([#4522](#4522)) ([c5db964](c5db964)), closes [#4527](#4527)
* **PostalCodeAndCity:** deprecate `country` in favor of `countryCode` ([#4572](#4572)) ([8d0d980](8d0d980))

### 📝 Documentation

* **InfoOverlay:** display fallback content when `undefined` ([#4580](#4580)) ([9879149](9879149))
* link to base fields from their respective value components ([#4564](#4564)) ([3d71c19](3d71c19))
* link to feature fields from their respective value components ([#4563](#4563)) ([45d0e3d](45d0e3d))
* removes duplicated docs for help prop ([#4568](#4568)) ([ae46e71](ae46e71))

### 🐛 Bug Fixes

* **DatePicker:** compare `date` and `maxDate` at same time of day so `is_valid_end_date` is `true` when `date` and `maxDate` are the same date ([#4565](#4565)) ([c52a40b](c52a40b))
* **Drawer:** ensure content on very small screen sizes doesn't get cut off ([#4567](#4567)) ([1dd405f](1dd405f))
* **ePlatform:** adjust ul list style and wrapper style (`max-width` and `padding`) ([#4552](#4552)) ([44f551e](44f551e))
* **Forms:** ensure city in PostalCodeAndCity accepts non-Norwegian characters when using other country ([#4569](#4569)) ([af1bdc8](af1bdc8))
* **Forms:** reveal error only when onChangeValidator is actually the initiator ([#4570](#4570)) ([4d962df](4d962df))
* **Name:** support spacing props ([eb6a412](eb6a412))
* **TextArea:** ResizeObserver loop completed with undelivered notifications ([#4582](#4582)) ([f2f97e3](f2f97e3))
* **Wizard.Step:** use `title` as aria-label when wrapped in `Wizard.Container` ([#4573](#4573)) ([de095e7](de095e7))
@tujoworker
Copy link
Member Author

🎉 This PR is included in version 10.64.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants