diff --git a/apps/web-mzima-client/src/app/core/services/event-bus.service.ts b/apps/web-mzima-client/src/app/core/services/event-bus.service.ts index 4edcfbb07c..bd908a6542 100644 --- a/apps/web-mzima-client/src/app/core/services/event-bus.service.ts +++ b/apps/web-mzima-client/src/app/core/services/event-bus.service.ts @@ -22,6 +22,7 @@ export const enum EventType { RefreshSurveysCounters = 'REFRESH_SURVEYS_COUNTERS', StopExportPolling = 'STOP_EXPORT_POLLING', ExportDone = 'EXPORT_DONE', + StatusChange = 'STATUS_CHANGE', } export interface BusEvent { diff --git a/apps/web-mzima-client/src/app/core/services/event-tracker.service.ts b/apps/web-mzima-client/src/app/core/services/event-tracker.service.ts new file mode 100644 index 0000000000..78095b2b89 --- /dev/null +++ b/apps/web-mzima-client/src/app/core/services/event-tracker.service.ts @@ -0,0 +1,57 @@ +import { Injectable, OnInit } from '@angular/core'; +import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy'; +import { EventBusService, EventType } from './event-bus.service'; + +@UntilDestroy() +@Injectable({ + providedIn: 'root', +}) +export class EventTrackerService implements OnInit { + public action: EventType; + public response: any; + public updateById: boolean | undefined; + constructor(private eventBusService: EventBusService) {} + + // eslint-disable-next-line @angular-eslint/contextual-lifecycle + ngOnInit() { + console.log('OnInit: Let my people go...'); + // this.emit({ payload }) + // this.trigger({ action: this.action, updateById: this.updateById }); + } + + // setTrigger({ action, updateById }: { action: EventType; updateById?: boolean }) { + + // } + + public trigger({ action, updateById }: { action: EventType; updateById?: boolean }) { + this.action = action; + this.updateById = updateById; + + this.eventBusService + .on(this.action) + .pipe(untilDestroyed(this)) + .subscribe({ + next: (response) => { + console.log(response); + console.log(this.action); + this.action = action; + this.response = response; + this.updateById = updateById; + const setResponse = () => (this.response = response); + if (updateById) { + if (this.response.id === response.id) setResponse(); + } else { + setResponse(); + } + }, + }); + } + + public emit({ payload }: { payload: any }) { + console.log(this.action, payload); + this.eventBusService.next({ + type: this.action, + payload, + }); + } +} diff --git a/apps/web-mzima-client/src/app/post/post-head/post-head.component.ts b/apps/web-mzima-client/src/app/post/post-head/post-head.component.ts index eeb4597b60..b0336d38a6 100644 --- a/apps/web-mzima-client/src/app/post/post-head/post-head.component.ts +++ b/apps/web-mzima-client/src/app/post/post-head/post-head.component.ts @@ -8,6 +8,7 @@ import { BaseComponent } from '../../base.component'; import { ShareModalComponent } from '../../shared/components'; import { PostResult, PostsService, PostStatus, postHelpers } from '@mzima-client/sdk'; import { ConfirmModalService } from '../../core/services/confirm-modal.service'; +import { EventTrackerService } from '../../core/services/event-tracker.service'; @Component({ selector: 'app-post-head', @@ -35,6 +36,7 @@ export class PostHeadComponent extends BaseComponent implements OnInit { private translate: TranslateService, private eventBusService: EventBusService, private snackBar: MatSnackBar, + private eventTrackerService: EventTrackerService, ) { super(sessionService, breakpointService); this.checkDesktop(); @@ -81,7 +83,7 @@ export class PostHeadComponent extends BaseComponent implements OnInit { if (postHelpers.isAllRequiredCompleted(post)) { this.postsService.updateStatus(this.post.id, PostStatus.Published).subscribe((res) => { this.post = res.result; - this.statusChanged.emit(); + this.eventTrackerService.emit({ payload: this.post }); }); } else { this.showMessage(this.translate.instant('notify.post.unfinished_post_task'), 'error', 5000); diff --git a/apps/web-mzima-client/src/app/post/post-metadata/post-metadata.component.ts b/apps/web-mzima-client/src/app/post/post-metadata/post-metadata.component.ts index 4b3f1bd7c2..955473fdf0 100644 --- a/apps/web-mzima-client/src/app/post/post-metadata/post-metadata.component.ts +++ b/apps/web-mzima-client/src/app/post/post-metadata/post-metadata.component.ts @@ -1,5 +1,7 @@ import { Component, Input, OnInit } from '@angular/core'; import { PostPropertiesInterface, PostResult } from '@mzima-client/sdk'; +import { EventTrackerService } from '../../core/services/event-tracker.service'; +import { EventType } from '@services'; @Component({ selector: 'app-post-metadata', @@ -10,8 +12,11 @@ export class PostMetadataComponent implements OnInit { @Input() post: PostResult | PostPropertiesInterface; author: string; + constructor(private eventTrackerService: EventTrackerService) {} + ngOnInit(): void { this.getUsername(); + this.eventTrackerService.trigger({ action: EventType.StatusChange, updateById: true }); } private getUsername(): void {