-
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 0dc92d1
Showing
8 changed files
with
180 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' |
121 changes: 121 additions & 0 deletions
121
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,121 @@ | ||
import setData from '../../Form/data-context/setData' | ||
|
||
export function onChange(apiConfig, connectionConfig) { | ||
return async function onChange(value) { | ||
const data = await verifyPostalCodeByAPI(value) | ||
|
||
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) { | ||
console.log('apiConfig', apiConfig) | ||
|
||
const data = await verifyPostalCodeByAPI(value) | ||
if (data.postal_codes[0].postal_code !== value) { | ||
return new Error('💁♂️ Feil i postnummeret') | ||
} | ||
} | ||
} | ||
|
||
// Mock API response | ||
async function verifyPostalCodeByAPI(postalCode: string) { | ||
// console.log('verifyPostalCodeByAPI', postalCode) | ||
console.log('get url', postalCode) | ||
await new Promise((resolve) => setTimeout(resolve, 600)) | ||
await new Promise((resolve) => setTimeout(resolve, 1000)) | ||
|
||
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() | ||
} | ||
|
||
// /** | ||
// * This is a part of Eufemia: | ||
// */ | ||
// const Connectors = { | ||
// BringConnector: { | ||
// postalCodeAndCity: (apiConnectionConfig = null) => { | ||
// return { | ||
// postalCode: { | ||
// // onChange: async (value) => { | ||
// // await new Promise((resolve) => setTimeout(resolve, 1000)) | ||
// // }, | ||
// onBlurValidator: async (value) => { | ||
// const data = await verifyPostalCodeByAPI(value) | ||
// if (data.postal_codes[0].postal_code !== value) { | ||
// return new Error('💁♂️ Feil i postnummeret') | ||
// } | ||
// }, | ||
// }, | ||
// city: { | ||
// // onChange: async (value) => { | ||
// // await new Promise((resolve) => setTimeout(resolve, 1000)) | ||
// // }, | ||
// }, | ||
// } | ||
// }, | ||
// }, | ||
// } | ||
|
||
// // This is a config for a Bring Plugin | ||
// const apiConnectionConfig = { | ||
// // Optional | ||
// connections: { | ||
// // Optional | ||
// postalCode: { | ||
// // Optional | ||
// url: ({ postalCode }) => | ||
// `https://api.bring.com/address/api/no/postal-codes/${postalCode}`, | ||
// // Optional | ||
// // headers: { | ||
// // 'X-Mybring-API-Uid': '', | ||
// // }, | ||
// }, | ||
// }, | ||
// // Optional | ||
// sharedBetweenAllConnections: { | ||
// headers: { | ||
// 'X-Mybring-API-Uid': '', | ||
// }, | ||
// }, | ||
// // Optional | ||
// // fetch: async ({ url }) => { | ||
// // return await fetch(url, { | ||
// // method: 'GET', | ||
// // headers: { | ||
// // Accept: 'application/json', | ||
// // 'X-Mybring-API-Uid': '', | ||
// // 'X-Mybring-API-Key': '', | ||
// // }, | ||
// // }) | ||
// // }, | ||
// } |
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.