chore: better typings for DiscussionListState (#3132)

This commit is contained in:
David Wheatley 2021-11-09 03:33:52 +00:00 committed by GitHub
parent b97c750cf3
commit 0fed7bc247

View File

@ -2,6 +2,12 @@ import app from '../../forum/app';
import PaginatedListState, { Page } from '../../common/states/PaginatedListState'; import PaginatedListState, { Page } from '../../common/states/PaginatedListState';
import Discussion from '../../common/models/Discussion'; import Discussion from '../../common/models/Discussion';
export interface IRequestParams {
include: string[];
filter: Record<string, string>;
sort?: string;
}
export default class DiscussionListState extends PaginatedListState<Discussion> { export default class DiscussionListState extends PaginatedListState<Discussion> {
protected extraDiscussions: Discussion[] = []; protected extraDiscussions: Discussion[] = [];
@ -13,21 +19,24 @@ export default class DiscussionListState extends PaginatedListState<Discussion>
return 'discussions'; return 'discussions';
} }
requestParams() { requestParams(): IRequestParams {
const params: any = { include: ['user', 'lastPostedUser'], filter: this.params.filter || {} }; const params: IRequestParams = {
include: ['user', 'lastPostedUser'],
filter: this.params.filter || {},
};
params.sort = this.sortMap()[this.params.sort]; params.sort = this.sortMap()[this.params.sort];
if (this.params.q) { if (this.params.q) {
params.filter.q = this.params.q; params.filter.q = this.params.q;
params.include.push('mostRelevantPost', 'mostRelevantPost.user'); params.include.push('mostRelevantPost', 'mostRelevantPost.user');
} }
return params; return params;
} }
protected loadPage(page: number = 1): any { protected loadPage(page: number = 1): Promise<Discussion[]> {
const preloadedDiscussions = app.preloadedApiDocument(); const preloadedDiscussions = app.preloadedApiDocument() as Discussion[] | null;
if (preloadedDiscussions) { if (preloadedDiscussions) {
this.initialLoading = false; this.initialLoading = false;
@ -38,7 +47,7 @@ export default class DiscussionListState extends PaginatedListState<Discussion>
return super.loadPage(page); return super.loadPage(page);
} }
clear() { clear(): void {
super.clear(); super.clear();
this.extraDiscussions = []; this.extraDiscussions = [];
@ -65,11 +74,11 @@ export default class DiscussionListState extends PaginatedListState<Discussion>
/** /**
* In the last request, has the user searched for a discussion? * In the last request, has the user searched for a discussion?
*/ */
isSearchResults() { isSearchResults(): boolean {
return !!this.params.q; return !!this.params.q;
} }
removeDiscussion(discussion: Discussion) { removeDiscussion(discussion: Discussion): void {
for (const page of this.pages) { for (const page of this.pages) {
const index = page.items.indexOf(discussion); const index = page.items.indexOf(discussion);
@ -91,7 +100,7 @@ export default class DiscussionListState extends PaginatedListState<Discussion>
/** /**
* Add a discussion to the top of the list. * Add a discussion to the top of the list.
*/ */
addDiscussion(discussion: Discussion) { addDiscussion(discussion: Discussion): void {
this.removeDiscussion(discussion); this.removeDiscussion(discussion);
this.extraDiscussions.unshift(discussion); this.extraDiscussions.unshift(discussion);