From 62c4a067a6b6a2360538377b31079febd72608aa Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Sun, 16 Apr 2023 16:07:30 +0100 Subject: [PATCH] fix: deleting a discussion from the profile does not visually remove it Signed-off-by: Sami Mazouz --- framework/core/js/src/common/compat.ts | 2 ++ .../core/js/src/common/utils/EventEmitter.ts | 23 +++++++++++++++++++ .../src/forum/states/DiscussionListState.ts | 10 ++++++++ 3 files changed, 35 insertions(+) create mode 100644 framework/core/js/src/common/utils/EventEmitter.ts diff --git a/framework/core/js/src/common/compat.ts b/framework/core/js/src/common/compat.ts index ff9224a7e1..df41ce85dc 100644 --- a/framework/core/js/src/common/compat.ts +++ b/framework/core/js/src/common/compat.ts @@ -4,6 +4,7 @@ import Session from './Session'; import Store from './Store'; import BasicEditorDriver from './utils/BasicEditorDriver'; import evented from './utils/evented'; +import EventEmitter from './utils/EventEmitter'; import KeyboardNavigatable from './utils/KeyboardNavigatable'; import liveHumanTimes from './utils/liveHumanTimes'; import ItemList from './utils/ItemList'; @@ -97,6 +98,7 @@ export default { Store: Store, 'utils/BasicEditorDriver': BasicEditorDriver, 'utils/evented': evented, + 'utils/EventEmitter': EventEmitter, 'utils/KeyboardNavigatable': KeyboardNavigatable, 'utils/liveHumanTimes': liveHumanTimes, 'utils/ItemList': ItemList, diff --git a/framework/core/js/src/common/utils/EventEmitter.ts b/framework/core/js/src/common/utils/EventEmitter.ts new file mode 100644 index 0000000000..597d54d830 --- /dev/null +++ b/framework/core/js/src/common/utils/EventEmitter.ts @@ -0,0 +1,23 @@ +export default class EventEmitter { + protected events: any = {}; + + public constructor() { + this.events = {}; + } + + public on(event: string, listener: Function): EventEmitter { + if (!this.events[event]) { + this.events[event] = []; + } + + this.events[event].push(listener); + + return this; + } + + public emit(event: string, ...args: any[]): void { + if (this.events[event]) { + this.events[event].forEach((listener: Function) => listener(...args)); + } + } +} diff --git a/framework/core/js/src/forum/states/DiscussionListState.ts b/framework/core/js/src/forum/states/DiscussionListState.ts index d81267042e..76d890785c 100644 --- a/framework/core/js/src/forum/states/DiscussionListState.ts +++ b/framework/core/js/src/forum/states/DiscussionListState.ts @@ -2,16 +2,22 @@ import app from '../../forum/app'; import PaginatedListState, { Page, PaginatedListParams, PaginatedListRequestParams } from '../../common/states/PaginatedListState'; import Discussion from '../../common/models/Discussion'; import { ApiResponsePlural } from '../../common/Store'; +import EventEmitter from '../../common/utils/EventEmitter'; export interface DiscussionListParams extends PaginatedListParams { sort?: string; } +const globalEventEmitter = new EventEmitter(); + export default class DiscussionListState

extends PaginatedListState { protected extraDiscussions: Discussion[] = []; + protected eventEmitter: EventEmitter; constructor(params: P, page: number = 1) { super(params, page, 20); + + this.eventEmitter = globalEventEmitter.on('discussion.deleted', this.deleteDiscussion.bind(this)); } get type(): string { @@ -77,6 +83,10 @@ export default class DiscussionListState