Commit 890c94d9 authored by Tom Käsler's avatar Tom Käsler

Add event emitter

Push RoomDeleted event when creator delets it.
React to RoomDeleted event in RoomList.
parent 5e8c798f
Pipeline #27068 passed with stages
in 6 minutes and 47 seconds
......@@ -12,6 +12,7 @@ import { AuthenticationGuard } from './guards/authentication.guard';
import { RoomService } from './services/http/room.service';
import { CommentService } from './services/http/comment.service';
import { DataStoreService } from './services/util/data-store.service';
import { EventService } from './services/util/event.service';
import { ContentService } from './services/http/content.service';
import { ContentAnswerService } from './services/http/content-answer.service';
import { VoteService } from './services/http/vote.service';
......@@ -77,6 +78,7 @@ export function initializeApp(appConfig: AppConfig) {
AuthenticationService,
AuthenticationGuard,
DataStoreService,
EventService,
RoomService,
CommentService,
ContentService,
......
......@@ -9,8 +9,10 @@ import { Router } from '@angular/router';
import { RoomCreatorPageComponent } from '../../room-creator-page/room-creator-page.component';
import { DeleteCommentComponent } from '../delete-comment/delete-comment.component';
import { CommentService } from '../../../../services/http/comment.service';
import { EventService } from '../../../../services/util/event.service';
import { CommentExportComponent } from '../comment-export/comment-export.component';
import { Comment } from '../../../../models/comment';
import { RoomDeleted } from '../../../../models/messages/room-deleted';
@Component({
selector: 'app-room-edit',
......@@ -29,6 +31,7 @@ export class RoomEditComponent implements OnInit {
protected roomService: RoomService,
public router: Router,
public commentService: CommentService,
public eventService: EventService,
@Inject(MAT_DIALOG_DATA) public data: any) {
}
......@@ -69,7 +72,10 @@ export class RoomEditComponent implements OnInit {
this.translationService.get('room-page.deleted').subscribe(msg => {
this.notificationService.show(room.name + msg);
});
this.roomService.deleteRoom(room.id).subscribe();
this.roomService.deleteRoom(room.id).subscribe(result => {
const event = new RoomDeleted(room.id);
this.eventService.broadcast(event.type, event.payload);
});
this.dialogRef.close('delete');
this.router.navigate([`/creator`]);
}
......
import { Component, OnInit } from '@angular/core';
import { Room } from '../../../models/room';
import { RoomService } from '../../../services/http/room.service';
import { EventService } from '../../../services/util/event.service';
import { AuthenticationService } from '../../../services/http/authentication.service';
import { UserRole } from '../../../models/user-roles.enum';
......@@ -17,12 +18,16 @@ export class RoomListComponent implements OnInit {
constructor(
private roomService: RoomService,
public eventService: EventService,
protected authenticationService: AuthenticationService) {
}
ngOnInit() {
this.getRooms();
this.getPath();
this.eventService.on<any>('RoomDeleted').subscribe(payload => {
this.rooms = this.rooms.filter(r => r.id !== payload.id);
});
}
getPath() {
......
export class RoomDeleted {
type: string;
payload: {
id: string;
};
constructor(id: string) {
this.type = 'RoomDeleted';
this.payload = {
id: id
};
}
}
import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';
import { filter, map } from 'rxjs/operators';
interface BroadcastEvent {
key: any;
data?: any;
}
export class EventService {
private _eventBus: Subject<BroadcastEvent>;
constructor() {
this._eventBus = new Subject<BroadcastEvent>();
}
broadcast(key: any, data?: any) {
this._eventBus.next({ key, data });
}
on<T>(key: any): Observable<T> {
return this._eventBus.asObservable().pipe(
filter(event => event.key === key),
map(event => <T>event.data)
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment