-
Notifications
You must be signed in to change notification settings - Fork 32
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
1 parent
c78b161
commit af26e94
Showing
9 changed files
with
174 additions
and
259 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
packages/dnb-eufemia/src/extensions/forms/Connectors/Bring/index.ts
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 @@ | ||
export * as postalCode from './postalCode' |
67 changes: 67 additions & 0 deletions
67
packages/dnb-eufemia/src/extensions/forms/Connectors/Bring/postalCode.ts
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,67 @@ | ||
import setData from '../../Form/data-context/setData' | ||
import { GeneralConfig, fetchDataFromAPI } from '../' | ||
import { UseFieldProps } from '../../types' | ||
|
||
export type HandlerConfig = { | ||
cityPath: string | ||
} | ||
|
||
async function fetchData(value: string, generalConfig: GeneralConfig) { | ||
// Mock API response | ||
// await new Promise((resolve) => setTimeout(resolve, 800)) | ||
// const mockData = { | ||
// city: 'Vollen', | ||
// county: 'Akershus', | ||
// latitude: '59.78899739297151', | ||
// longitude: '10.482494731266165', | ||
// municipality: 'Asker', | ||
// municipalityId: '3203', | ||
// po_box: false, | ||
// postal_code: '1391', | ||
// } | ||
// return { postal_codes: [mockData] } | ||
|
||
// Visit: https://cors-anywhere.herokuapp.com/corsdemo to enable this service | ||
generalConfig.fetchConfig.url = `https://cors-anywhere.herokuapp.com/https://api.bring.com/address/api/no/postal-codes/${value}` | ||
|
||
return await fetchDataFromAPI(generalConfig) | ||
} | ||
|
||
export function onChange( | ||
generalConfig: GeneralConfig, | ||
handlerConfig?: HandlerConfig | ||
): UseFieldProps<string>['onChange'] { | ||
return async function onChange(value) { | ||
if (typeof value === 'string' && value.length >= 4) { | ||
try { | ||
const data = await fetchData(value, generalConfig) | ||
// console.log('onChange', generalConfig, handlerConfig, value, data) | ||
|
||
const { city } = data.postal_codes[0] || {} | ||
if (handlerConfig?.cityPath) { | ||
const dataContext = setData(generalConfig.handlerId) | ||
dataContext.update(handlerConfig.cityPath, city) | ||
} | ||
} catch (error) { | ||
return new Error(error) | ||
} | ||
} | ||
} | ||
} | ||
|
||
export function onBlurValidator( | ||
generalConfig: GeneralConfig | ||
): UseFieldProps<string>['onBlurValidator'] { | ||
return async function onBlurValidator(value) { | ||
try { | ||
const data = await fetchData(value, generalConfig) | ||
// console.log('onBlurValidator', generalConfig, value, data) | ||
|
||
if (data.postal_codes?.[0]?.postal_code !== value) { | ||
return new Error('💁♂️ Feil i postnummeret') | ||
} | ||
} catch (error) { | ||
return new Error(error) | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
packages/dnb-eufemia/src/extensions/forms/Connectors/createContext.ts
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,57 @@ | ||
import { SharedStateId } from '../../../shared/helpers/useSharedState' | ||
|
||
export type GeneralConfig = { | ||
handlerId?: SharedStateId | ||
fetchConfig?: { | ||
url?: string | ||
headers?: Record<string, string> | ||
} | ||
} | ||
|
||
export function createContext<GeneralConfigGeneric = GeneralConfig>( | ||
generalConfig: GeneralConfigGeneric = null | ||
) { | ||
const handlerId = {} | ||
if (!generalConfig['handlerId']) { | ||
generalConfig['handlerId'] = handlerId | ||
} | ||
|
||
return { | ||
handlerId, | ||
withConfig< | ||
HandlerMethod extends ( | ||
generalConfig: GeneralConfigGeneric, | ||
handlerConfig: unknown | ||
) => ReturnType<HandlerMethod>, | ||
>(fn: HandlerMethod, handlerConfig?: Parameters<HandlerMethod>[1]) { | ||
return fn(generalConfig, handlerConfig) | ||
}, | ||
} | ||
} | ||
|
||
export async function fetchDataFromAPI(generalConfig: GeneralConfig) { | ||
const { fetchConfig } = generalConfig | ||
|
||
const options = { | ||
method: 'GET', | ||
headers: { | ||
Accept: 'application/json', | ||
...fetchConfig.headers, | ||
}, | ||
} | ||
|
||
try { | ||
const response = await fetch(fetchConfig.url, options) | ||
|
||
// Check if the response status is in the range of 200-299 | ||
if (!response.ok) { | ||
throw new Error( | ||
`HTTP Error: ${response.status} - ${response.statusText}` | ||
) | ||
} | ||
|
||
return await response.json() | ||
} catch (error) { | ||
throw new Error(error) | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/dnb-eufemia/src/extensions/forms/Connectors/index.ts
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,2 @@ | ||
export * from './createContext' | ||
export * as Bring from './Bring' |
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
Oops, something went wrong.