Skip to content

Commit

Permalink
feat: flow config locales usage (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
dorsha authored Aug 6, 2023
1 parent 9d11e8c commit 963c95f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 10 deletions.
10 changes: 8 additions & 2 deletions packages/web-component/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ DESCOPE_PROJECT_ID=<project-id>
DESCOPE_FLOW_ID=<flow-id>
# Optional - Descope base URL
DESCOPE_BASE_URL
# Optional - Descope locale (according to the target locales configured in the flow)
DESCOPE_LOCALE=<locale>
```

1. Run the sample `pnpm run start`
Expand Down Expand Up @@ -107,7 +109,9 @@ Usage example:

```javascript
const descopeWcEle = document.getElementsByTagName('descope-wc')[0];
descopeWcEle.addEventListener('error', (e) => alert(`Error! - ${e.detail.errorMessage}`));
descopeWcEle.addEventListener('error', (e) =>
alert(`Error! - ${e.detail.errorMessage}`)
);
```

### `success` - Fired when the flow is completed successfully. The event detail contains the flow result.
Expand All @@ -116,5 +120,7 @@ Usage example:

```javascript
const descopeWcEle = document.getElementsByTagName('descope-wc')[0];
descopeWcEle.addEventListener('success', (e) => alert(`Success! - ${JSON.stringify(e.detail)}`));
descopeWcEle.addEventListener('success', (e) =>
alert(`Success! - ${JSON.stringify(e.detail)}`)
);
```
3 changes: 2 additions & 1 deletion packages/web-component/rollup.config.app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export default {
contents
.replace('<project-id>', process.env.DESCOPE_PROJECT_ID || '')
.replace('<flow-id>', process.env.DESCOPE_FLOW_ID || 'sign-up-or-in')
.replace('<base-url>', process.env.DESCOPE_BASE_URL || ''),
.replace('<base-url>', process.env.DESCOPE_BASE_URL || '')
.replace('<locale>', process.env.DESCOPE_LOCALE || ''),
}),
],
};
1 change: 1 addition & 0 deletions packages/web-component/src/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
project-id="<project-id>"
flow-id="<flow-id>"
base-url="<base-url>"
locale="<locale>"
debug="true"
></descope-wc>
</div>
Expand Down
4 changes: 2 additions & 2 deletions packages/web-component/src/lib/descope-wc/BaseDescopeWc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ class BaseDescopeWc extends HTMLElement {
}

async getTargetLocales() {
const { projectConfig } = await this.#getConfig();
return (projectConfig?.targetLocales || []).map((locale: string) =>
const flowConfig = await this.getFlowConfig();
return (flowConfig?.targetLocales || []).map((locale: string) =>
locale.toLowerCase()
);
}
Expand Down
30 changes: 25 additions & 5 deletions packages/web-component/test/descope-wc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2433,7 +2433,11 @@ describe('web-component', () => {
startMock.mockReturnValue(generateSdkResponse());

configContent = {
targetLocales: ['en-US'],
flows: {
otpSignInEmail: {
targetLocales: ['en-US'],
},
},
};

pageContent = '<input id="email"></input><span>It works!</span>';
Expand Down Expand Up @@ -2472,7 +2476,11 @@ describe('web-component', () => {
startMock.mockReturnValue(generateSdkResponse());

configContent = {
targetLocales: ['de'],
flows: {
otpSignInEmail: {
targetLocales: ['de'],
},
},
};

pageContent = '<input id="email"></input><span>It works!</span>';
Expand Down Expand Up @@ -2511,7 +2519,11 @@ describe('web-component', () => {
startMock.mockReturnValue(generateSdkResponse());

configContent = {
targetLocales: ['en-US'],
flows: {
otpSignInEmail: {
targetLocales: ['en-US'],
},
},
};

Object.defineProperty(navigator, 'language', {
Expand Down Expand Up @@ -2560,7 +2572,11 @@ describe('web-component', () => {
startMock.mockReturnValue(generateSdkResponse());

configContent = {
targetLocales: ['de'],
flows: {
otpSignInEmail: {
targetLocales: ['de'],
},
},
};

Object.defineProperty(navigator, 'language', {
Expand Down Expand Up @@ -2609,7 +2625,11 @@ describe('web-component', () => {
startMock.mockReturnValue(generateSdkResponse());

configContent = {
targetLocales: ['en-US'],
flows: {
otpSignInEmail: {
targetLocales: ['en-US'],
},
},
};

const fn = fetchMock.getMockImplementation();
Expand Down

0 comments on commit 963c95f

Please sign in to comment.