-
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 2e79757
Showing
8 changed files
with
116 additions
and
260 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' |
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
import setData from '../../Form/data-context/setData' | ||
|
||
export function onChange(apiConfig, connectionConfig) { | ||
return async function onChange(value) { | ||
const data = await verifyPostalCodeByAPI(value) | ||
// console.log('onChange', apiConfig, connectionConfig, value, data) | ||
|
||
if (connectionConfig?.targetPath) { | ||
const dataContext = setData(apiConfig.handlerId) | ||
dataContext.update( | ||
connectionConfig.targetPath, | ||
data.postal_codes[0].city | ||
) | ||
} | ||
} | ||
} | ||
|
||
export function onBlurValidator(apiConfig) { | ||
return async function onBlurValidator(value) { | ||
const data = await verifyPostalCodeByAPI(value) | ||
// console.log('onBlurValidator', apiConfig, value, data) | ||
|
||
if (data.postal_codes[0].postal_code !== value) { | ||
return new Error('💁♂️ Feil i postnummeret') | ||
} | ||
} | ||
} | ||
|
||
// Mock API response | ||
async function verifyPostalCodeByAPI(postalCode: string) { | ||
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 | ||
const url = `https://cors-anywhere.herokuapp.com/https://api.bring.com/address/api/no/postal-codes/${postalCode}` | ||
const response = await fetch(url, { | ||
method: 'GET', | ||
headers: { | ||
Accept: 'application/json', | ||
'X-Mybring-API-Uid': '', | ||
'X-Mybring-API-Key': '', | ||
}, | ||
}) | ||
|
||
return await response.json() | ||
} |
15 changes: 15 additions & 0 deletions
15
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,15 @@ | ||
export * as Bring from './Bring' | ||
|
||
export function createContext<T = unknown>(config: T = null) { | ||
const handlerId = {} | ||
return { | ||
handlerId, | ||
withConfig<U>( | ||
fn: (config: T, connectionConfig: U) => U, | ||
connectionConfig?: U | ||
) { | ||
config['handlerId'] = handlerId | ||
return fn(config, connectionConfig) | ||
}, | ||
} | ||
} |
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.