From e79547b5d32718ac45c76093f6d426994a2ddad6 Mon Sep 17 00:00:00 2001 From: chavda-bhavik Date: Thu, 29 Aug 2024 17:46:44 +0530 Subject: [PATCH] feat: finalized events with angular sdk --- package.json | 1 + packages/angular/src/index.ts | 4 +-- .../angular/src/services/impler.service.ts | 34 ++++++++++++------- packages/angular/src/types/index.ts | 4 +++ 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 46d31e25f..baf139f99 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "build": "npx nx run-many --target=build --all", "build:api": "npx nx build @impler/api", "build:dal": "npx nx build @impler/dal", + "build:angular": "npx nx build @impler/angular", "build:react": "npx nx build @impler/react", "build:shared": "npx nx build @impler/shared", "build:widget": "npx nx build @impler/widget", diff --git a/packages/angular/src/index.ts b/packages/angular/src/index.ts index 42be75d0c..e7f198088 100644 --- a/packages/angular/src/index.ts +++ b/packages/angular/src/index.ts @@ -1,3 +1,3 @@ export * from './services'; -export type { ColumnTypesEnum } from '@impler/shared'; -export type { ISchemaItem, CustomTexts } from './types'; +export type { ColumnTypesEnum, EventTypesEnum } from '@impler/shared'; +export type { ISchemaItem, CustomTexts, EventCalls } from './types'; diff --git a/packages/angular/src/services/impler.service.ts b/packages/angular/src/services/impler.service.ts index 43a0435f3..56e1bb018 100644 --- a/packages/angular/src/services/impler.service.ts +++ b/packages/angular/src/services/impler.service.ts @@ -1,17 +1,19 @@ import { Injectable, EventEmitter } from '@angular/core'; -import { CustomTexts } from '../types'; +import { EventCalls, ShowWidgetProps } from '../types'; +import { logError } from '../utils/logger'; @Injectable({ providedIn: 'root', }) export class ImplerService { + private uuid: string; private isImplerReady = false; - public widgetEvents = new EventEmitter(); + private widgetEvents = new EventEmitter(true); - public initializeImpler(): void { - const uuid = this.generateUuid(); + initializeImpler(): void { + this.uuid = this.generateUuid(); if (window.impler) { - window.impler.init(uuid); + window.impler.init(this.uuid); const intervalId = setInterval(() => { if (window.impler.isReady()) { this.isImplerReady = true; @@ -21,27 +23,35 @@ export class ImplerService { window.impler.on( 'message', - (eventData) => { + (eventData: EventCalls) => { this.widgetEvents.emit(eventData); }, - uuid + this.uuid ); - } + } else logError('IMPLER_UNDEFINED_ERROR'); } - public show(options: { projectId: string; templateId: string; accessToken: string; texts?: CustomTexts }): void { + showWidget(options: ShowWidgetProps): void { if (this.isImplerReady) { - window.impler.show(options); + window.impler.show({ ...options, uuid: this.uuid }); } else { - console.error('Impler is not ready yet.'); + logError('IMPLER_UNDEFINED_ERROR'); } } - public getReadyState(): boolean { + getReadyState(): boolean { return this.isImplerReady; } private generateUuid(): string { return window.crypto.getRandomValues(new Uint32Array(1))[0].toString(); } + closeWidget() { + if (window.impler) { + window.impler.close(); + } else logError('IMPLER_UNDEFINED_ERROR'); + } + subscribeToWidgetEvents(callback: (data: EventCalls) => void): void { + this.widgetEvents.subscribe(callback); + } } diff --git a/packages/angular/src/types/index.ts b/packages/angular/src/types/index.ts index 420c44650..37e7f5350 100644 --- a/packages/angular/src/types/index.ts +++ b/packages/angular/src/types/index.ts @@ -51,6 +51,10 @@ export interface ShowWidgetProps { schema?: ISchemaItem[]; data?: Record[]; output?: Record; + projectId: string; + templateId: string; + accessToken: string; + texts?: CustomTexts; } export type DeepPartial = T extends object