Skip to content

Commit

Permalink
fix: deleting a discussion from the profile does not visually remove …
Browse files Browse the repository at this point in the history
…it (#3799)

Signed-off-by: Sami Mazouz <[email protected]>
  • Loading branch information
SychO9 authored Apr 19, 2023
1 parent c0af41c commit 297a2d8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions framework/core/js/src/common/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions framework/core/js/src/common/utils/EventEmitter.ts
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
10 changes: 10 additions & 0 deletions framework/core/js/src/forum/states/DiscussionListState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<P extends DiscussionListParams = DiscussionListParams> extends PaginatedListState<Discussion, P> {
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 {
Expand Down Expand Up @@ -77,6 +83,10 @@ export default class DiscussionListState<P extends DiscussionListParams = Discus
}

removeDiscussion(discussion: Discussion): void {
this.eventEmitter.emit('discussion.deleted', discussion);
}

deleteDiscussion(discussion: Discussion): void {
for (const page of this.pages) {
const index = page.items.indexOf(discussion);

Expand Down

0 comments on commit 297a2d8

Please sign in to comment.