This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/frontend/scenes/Search/SearchStore.js
2017-07-09 11:26:17 -07:00

39 lines
968 B
JavaScript

// @flow
import { observable, action, runInAction } from 'mobx';
import invariant from 'invariant';
import { client } from 'utils/ApiClient';
import Document from 'models/Document';
class SearchStore {
@observable documents: Array<Document> = [];
@observable searchTerm: ?string = null;
@observable isFetching = false;
/* Actions */
@action search = async (query: string) => {
this.searchTerm = query;
this.isFetching = true;
if (query) {
try {
const res = await client.get('/documents.search', { query });
invariant(res && res.data, 'res or res.data missing');
const { data } = res;
runInAction('search document', () => {
this.documents = data.map(documentData => new Document(documentData));
});
} catch (e) {
console.error('Something went wrong');
}
} else {
this.documents = [];
}
this.isFetching = false;
};
}
export default SearchStore;