fix: Search results not updated when changing filters

This commit is contained in:
Tom Moor 2021-01-11 00:50:44 -08:00
parent ac082e4a5f
commit c0bbae50c4

View File

@ -1,6 +1,6 @@
// @flow // @flow
import ArrowKeyNavigation from "boundless-arrow-key-navigation"; import ArrowKeyNavigation from "boundless-arrow-key-navigation";
import { debounce } from "lodash"; import { debounce, isEqual } from "lodash";
import { observable, action } from "mobx"; import { observable, action } from "mobx";
import { observer, inject } from "mobx-react"; import { observer, inject } from "mobx-react";
import { PlusIcon } from "outline-icons"; import { PlusIcon } from "outline-icons";
@ -52,6 +52,7 @@ type Props = {
class Search extends React.Component<Props> { class Search extends React.Component<Props> {
firstDocument: ?React.Component<any>; firstDocument: ?React.Component<any>;
lastQuery: string = ""; lastQuery: string = "";
lastParams: Object;
@observable @observable
query: string = decodeURIComponent(this.props.match.params.term || ""); query: string = decodeURIComponent(this.props.match.params.term || "");
@ -194,17 +195,7 @@ class Search extends React.Component<Props> {
@action @action
fetchResults = async () => { fetchResults = async () => {
if (this.query) { if (this.query) {
// we just requested this thing no need to try again const params = {
if (this.lastQuery === this.query) {
this.isLoading = false;
return;
}
this.isLoading = true;
this.lastQuery = this.query;
try {
const results = await this.props.documents.search(this.query, {
offset: this.offset, offset: this.offset,
limit: DEFAULT_PAGINATION_LIMIT, limit: DEFAULT_PAGINATION_LIMIT,
dateFilter: this.dateFilter, dateFilter: this.dateFilter,
@ -212,7 +203,20 @@ class Search extends React.Component<Props> {
includeDrafts: true, includeDrafts: true,
collectionId: this.collectionId, collectionId: this.collectionId,
userId: this.userId, userId: this.userId,
}); };
// we just requested this thing no need to try again
if (this.lastQuery === this.query && isEqual(params, this.lastParams)) {
this.isLoading = false;
return;
}
this.isLoading = true;
this.lastQuery = this.query;
this.lastParams = params;
try {
const results = await this.props.documents.search(this.query, params);
this.pinToTop = true; this.pinToTop = true;