From b437b9d62427c6f7d7f6a600b9c297835b94c9c0 Mon Sep 17 00:00:00 2001 From: David Wheatley Date: Wed, 27 Oct 2021 17:17:01 +0200 Subject: [PATCH 1/2] chore: better typings for DiscussionListState --- js/src/forum/states/DiscussionListState.ts | 29 ++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/js/src/forum/states/DiscussionListState.ts b/js/src/forum/states/DiscussionListState.ts index 6f687c83aa..b84b09eecb 100644 --- a/js/src/forum/states/DiscussionListState.ts +++ b/js/src/forum/states/DiscussionListState.ts @@ -2,6 +2,12 @@ import app from '../../forum/app'; import PaginatedListState, { Page } from '../../common/states/PaginatedListState'; import Discussion from '../../common/models/Discussion'; +export interface IRequestParams { + include: string[]; + filter: Record; + sort?: string; +} + export default class DiscussionListState extends PaginatedListState { protected extraDiscussions: Discussion[] = []; @@ -13,21 +19,24 @@ export default class DiscussionListState extends PaginatedListState return 'discussions'; } - requestParams() { - const params: any = { include: ['user', 'lastPostedUser'], filter: this.params.filter || {} }; + requestParams(): IRequestParams { + const params: IRequestParams = { + include: ['user', 'lastPostedUser'], + filter: this.params.filter || {}, + }; - params.sort = this.sortMap()[this.params.sort]; + params.sort = this.sortMap().get(this.params.sort); if (this.params.q) { params.filter.q = this.params.q; - params.include.push('mostRelevantPost', 'mostRelevantPost.user'); } + return params; } - protected loadPage(page: number = 1): any { - const preloadedDiscussions = app.preloadedApiDocument(); + protected loadPage(page: number = 1): Promise { + const preloadedDiscussions = app.preloadedApiDocument() as Discussion[] | null; if (preloadedDiscussions) { this.initialLoading = false; @@ -38,7 +47,7 @@ export default class DiscussionListState extends PaginatedListState return super.loadPage(page); } - clear() { + clear(): void { super.clear(); this.extraDiscussions = []; @@ -65,11 +74,11 @@ export default class DiscussionListState extends PaginatedListState /** * In the last request, has the user searched for a discussion? */ - isSearchResults() { + isSearchResults(): boolean { return !!this.params.q; } - removeDiscussion(discussion: Discussion) { + removeDiscussion(discussion: Discussion): void { for (const page of this.pages) { const index = page.items.indexOf(discussion); @@ -91,7 +100,7 @@ export default class DiscussionListState extends PaginatedListState /** * Add a discussion to the top of the list. */ - addDiscussion(discussion: Discussion) { + addDiscussion(discussion: Discussion): void { this.removeDiscussion(discussion); this.extraDiscussions.unshift(discussion); From 1ca04b279466950bb5dabab6fa8b2cc271607dde Mon Sep 17 00:00:00 2001 From: David Wheatley Date: Wed, 27 Oct 2021 18:38:55 +0200 Subject: [PATCH 2/2] Update js/src/forum/states/DiscussionListState.ts --- js/src/forum/states/DiscussionListState.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/src/forum/states/DiscussionListState.ts b/js/src/forum/states/DiscussionListState.ts index b84b09eecb..74300fdfe5 100644 --- a/js/src/forum/states/DiscussionListState.ts +++ b/js/src/forum/states/DiscussionListState.ts @@ -25,7 +25,7 @@ export default class DiscussionListState extends PaginatedListState filter: this.params.filter || {}, }; - params.sort = this.sortMap().get(this.params.sort); + params.sort = this.sortMap()[this.params.sort]; if (this.params.q) { params.filter.q = this.params.q;