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

chore: better typings for DiscussionListState #3132

Merged
merged 2 commits into from
Nov 9, 2021
Merged
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
27 changes: 18 additions & 9 deletions js/src/forum/states/DiscussionListState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we enforce that if q is present in filters, it should be the only key?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, that's a good question. Let me have a play.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a way.

One of the promising things was:

filter: { q: string } | { [key: string]: string } & { q?: never }

but this seemed to not work correctly, for some reason.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find a way.

One of the promising things was:

filter: { q: string } | { [key: string]: string } & { q?: never }

but this seemed to not work correctly, for some reason.

Fair enough, let's keep things as is then

include: string[];
filter: Record<string, string>;
sort?: string;
}

export default class DiscussionListState extends PaginatedListState<Discussion> {
protected extraDiscussions: Discussion[] = [];

Expand All @@ -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];

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;
Expand All @@ -38,7 +47,7 @@ export default class DiscussionListState extends PaginatedListState<Discussion>
return super.loadPage(page);
}

clear() {
clear(): void {
super.clear();

this.extraDiscussions = [];
Expand All @@ -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);

Expand All @@ -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);

Expand Down