Skip to content

Commit

Permalink
Testing event bus tracker service
Browse files Browse the repository at this point in the history
  • Loading branch information
Ifycode committed Dec 11, 2024
1 parent 8d77d79 commit 03e4e07
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = any> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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 {
Expand Down

0 comments on commit 03e4e07

Please sign in to comment.