chore: better typings for DiscussionListState

This commit is contained in:
David Wheatley 2021-10-27 17:17:01 +02:00
parent 83014a5a9f
commit 98d0fbd4ae

View File

@ -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<string, string>;
sort?: string;
}
export default class DiscussionListState extends PaginatedListState<Discussion> {
protected extraDiscussions: Discussion[] = [];
@ -13,21 +19,24 @@ export default class DiscussionListState extends PaginatedListState<Discussion>
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<Discussion[]> {
const preloadedDiscussions = app.preloadedApiDocument() as Discussion[] | null;
if (preloadedDiscussions) {
this.initialLoading = false;
@ -38,7 +47,7 @@ export default class DiscussionListState extends PaginatedListState<Discussion>
return super.loadPage(page);
}
clear() {
clear(): void {
super.clear();
this.extraDiscussions = [];
@ -65,11 +74,11 @@ export default class DiscussionListState extends PaginatedListState<Discussion>
/**
* 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<Discussion>
/**
* Add a discussion to the top of the list.
*/
addDiscussion(discussion: Discussion) {
addDiscussion(discussion: Discussion): void {
this.removeDiscussion(discussion);
this.extraDiscussions.unshift(discussion);