Skip to content

Commit

Permalink
feat: finalized events with angular sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
chavda-bhavik committed Aug 29, 2024
1 parent d3f852c commit e79547b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
34 changes: 22 additions & 12 deletions packages/angular/src/services/impler.service.ts
Original file line number Diff line number Diff line change
@@ -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<any>();
private widgetEvents = new EventEmitter<EventCalls>(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;
Expand All @@ -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);
}
}
4 changes: 4 additions & 0 deletions packages/angular/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export interface ShowWidgetProps {
schema?: ISchemaItem[];
data?: Record<string, string | any>[];
output?: Record<string, string | any>;
projectId: string;
templateId: string;
accessToken: string;
texts?: CustomTexts;
}

export type DeepPartial<T> = T extends object
Expand Down

0 comments on commit e79547b

Please sign in to comment.