Skip to content

Commit

Permalink
feat: Update IntegrationWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
art7cf committed Feb 25, 2024
1 parent 0f8b862 commit b2cd13c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const widget = new NepflowIntegrationWidget(
);
```

If your app is integrated with [Zapier](https://zapier.com/), your users can see [integration guides](https://nepflow.dev/#guides):
If your app is integrated with [Zapier](https://zapier.com/), your users can see [integration guides](https://nepflow.dev/#guides) and Zapier templates:

```javascript
const widget = new NepflowIntegrationWidget(
Expand All @@ -86,7 +86,7 @@ const widget = new NepflowIntegrationWidget(
);
```

You can use `customCards` parameter to show your native integrations and replacing existing Zapier apps:
You can use `customCards` parameter to show your native integrations or replacing existing Zapier apps:

```javascript
const widget = new NepflowIntegrationWidget(
Expand Down
1 change: 1 addition & 0 deletions package/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = {
"plugins": [
"react"
],
"ignorePatterns": ["dist/"],
"rules": {
"@typescript-eslint/prefer-nullish-coalescing": "off",
"@typescript-eslint/strict-boolean-expressions": "off"
Expand Down
4 changes: 2 additions & 2 deletions package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const widget = new NepflowIntegrationWidget(
);
```

If your app is integrated with [Zapier](https://zapier.com/), your users can see [integration guides](https://nepflow.dev/#guides):
If your app is integrated with [Zapier](https://zapier.com/), your users can see [integration guides](https://nepflow.dev/#guides) and Zapier templates:

```javascript
const widget = new NepflowIntegrationWidget(
Expand All @@ -86,7 +86,7 @@ const widget = new NepflowIntegrationWidget(
);
```

You can use `customCards` parameter to show your native integrations and replacing existing Zapier apps:
You can use `customCards` parameter to show your native integrations or replacing existing Zapier apps:

```javascript
const widget = new NepflowIntegrationWidget(
Expand Down
2 changes: 1 addition & 1 deletion package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nepflow/integration-widget",
"version": "1.4.0",
"version": "1.5.0",
"description": "",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down
2 changes: 1 addition & 1 deletion package/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IntegrationWidget as NepflowIntegrationWidget } from './models/Integrat

declare global {
interface Window {
NepflowIntegrationWidget: Function
NepflowIntegrationWidget: typeof NepflowIntegrationWidget
}
}

Expand Down
49 changes: 36 additions & 13 deletions package/src/models/IntegrationWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,36 @@ interface IframeConfig {
export class IntegrationWidget {
private readonly entityId: string
private readonly baseUrl: string
private readonly params: IntegrationWidgetConfig
private readonly iframeParams: IframeConfig

constructor (elementId: string, params: IntegrationWidgetConfig, iframeParams?: IframeConfig) {
this.entityId = this.generateEntityId()
this.baseUrl = iframeParams?.baseUrl ?? 'https://widget.nepflow.dev'
this.params = params
this.iframeParams = iframeParams ?? {}

// Get the widget element
const widgetElement = document.getElementById(elementId)
if (!widgetElement) {
console.error('Integration widget element not found.')
return
}

// Create the iframe element
const iframe = this.createIFrame()

// Append the iframe to the element
widgetElement.innerHTML = ''
widgetElement.appendChild(iframe)

this.setupMessageHandler(iframe)
}

private getSearchParams (): URLSearchParams {
// Create URLSearchParams from the params object
const searchParams = new URLSearchParams()
for (const [key, value] of Object.entries(params)) {
for (const [key, value] of Object.entries(this.params)) {
if (typeof value === 'boolean') {
searchParams.append(key, value ? 'true' : '')
} else if (typeof value === 'object') {
Expand All @@ -41,22 +57,28 @@ export class IntegrationWidget {
// Add parent window host
searchParams.append('appHost', window.location.host)

// Create the iframe element
return searchParams
}

// Create the iframe element
private createIFrame (): HTMLIFrameElement {
const searchParams = this.getSearchParams()

const iframe = document.createElement('iframe')
iframe.src = `${this.baseUrl}/${params.zapierAppId || ''}?${searchParams.toString()}`
iframe.style.width = iframeParams?.width ?? '100%'
iframe.style.height = iframeParams?.height ?? '100%'
iframe.style.minHeight = iframeParams?.minHeight ?? '500px'
iframe.src = `${this.baseUrl}/${this.params.zapierAppId || ''}?${searchParams.toString()}`
iframe.style.width = this.iframeParams?.width ?? '100%'
iframe.style.height = this.iframeParams?.height ?? '100%'
iframe.style.minHeight = this.iframeParams?.minHeight ?? '500px'
iframe.frameBorder = '0'

// Append the iframe to the integration-widget element
widgetElement.innerHTML = ''
widgetElement.appendChild(iframe)
return iframe
}

// Handler for messages from the iframe
// Setup handler for messages from the iframe
private setupMessageHandler (iframe: HTMLIFrameElement): void {
window.addEventListener('message', (event) => {
if (!event.origin.startsWith(this.baseUrl)) {
console.debug('[Nepflow] Received message from unknown origin:', event.origin)
console.debug('[Nepflow] Received message from unknown origin', event.origin)
return
}

Expand All @@ -73,14 +95,15 @@ export class IntegrationWidget {
iframe.style.height = `${data.widgetHeight}px`
break
case 'handleCardClick':
if (typeof params.onCardClick === 'function') {
params.onCardClick(data.id as string)
if (typeof this.params.onCardClick === 'function') {
this.params.onCardClick(data.id as string)
};
break
}
})
}

// Generate unique entity id
private generateEntityId (): string {
const prefix = 'integration-widget-'
const randomSuffix = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)
Expand Down

0 comments on commit b2cd13c

Please sign in to comment.