Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deleting a discussion from the profile does not visually remove it #3799

Merged
merged 1 commit into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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