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.
outline/frontend/stores/DocumentsStore.js

105 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-06-28 03:59:53 +00:00
// @flow
2017-07-09 18:26:17 +00:00
import { observable, action, computed, ObservableMap, runInAction } from 'mobx';
2017-06-28 03:59:53 +00:00
import { client } from 'utils/ApiClient';
import _ from 'lodash';
import invariant from 'invariant';
import stores from 'stores';
import Document from 'models/Document';
import ErrorsStore from 'stores/ErrorsStore';
class DocumentsStore {
2017-06-28 04:52:47 +00:00
@observable recentlyViewedIds: Array<string> = [];
2017-06-28 05:15:29 +00:00
@observable data: Map<string, Document> = new ObservableMap([]);
2017-06-28 03:59:53 +00:00
@observable isLoaded: boolean = false;
errors: ErrorsStore;
2017-07-09 18:26:17 +00:00
/* Computed */
@computed get recentlyViewed(): Array<Document> {
return _.filter(this.data.values(), ({ id }) =>
this.recentlyViewedIds.includes(id)
);
}
@computed get recentlyEdited(): Array<Document> {
// $FlowIssue
return this.data.values();
}
@computed get starred(): Array<Document> {
return _.filter(this.data.values(), 'starred');
}
2017-06-28 03:59:53 +00:00
/* Actions */
2017-06-28 04:52:47 +00:00
@action fetchAll = async (request: string = 'list'): Promise<*> => {
2017-06-28 03:59:53 +00:00
try {
2017-06-28 04:20:09 +00:00
const res = await client.post(`/documents.${request}`);
2017-06-28 03:59:53 +00:00
invariant(res && res.data, 'Document list not available');
const { data } = res;
runInAction('DocumentsStore#fetchAll', () => {
2017-06-28 04:52:47 +00:00
data.forEach(document => {
this.data.set(document.id, new Document(document));
});
2017-06-28 03:59:53 +00:00
this.isLoaded = true;
});
2017-06-28 04:20:09 +00:00
return data;
2017-06-28 03:59:53 +00:00
} catch (e) {
this.errors.add('Failed to load documents');
}
};
2017-06-28 04:52:47 +00:00
@action fetchRecentlyViewed = async (): Promise<*> => {
2017-06-28 04:20:09 +00:00
const data = await this.fetchAll('viewed');
2017-06-28 04:52:47 +00:00
runInAction('DocumentsStore#fetchRecentlyViewed', () => {
2017-06-28 05:15:29 +00:00
this.recentlyViewedIds = _.map(data, 'id');
2017-06-28 04:52:47 +00:00
});
2017-06-28 04:20:09 +00:00
};
2017-06-28 05:15:29 +00:00
@action fetchStarred = async (): Promise<*> => {
await this.fetchAll('starred');
};
2017-06-28 03:59:53 +00:00
@action fetch = async (id: string): Promise<*> => {
try {
const res = await client.post('/documents.info', { id });
invariant(res && res.data, 'Document not available');
const { data } = res;
const document = new Document(data);
2017-06-28 03:59:53 +00:00
runInAction('DocumentsStore#fetch', () => {
this.data.set(data.id, document);
2017-06-28 03:59:53 +00:00
this.isLoaded = true;
});
return document;
2017-06-28 03:59:53 +00:00
} catch (e) {
this.errors.add('Failed to load documents');
}
};
@action add = (document: Document): void => {
2017-06-28 04:52:47 +00:00
this.data.set(document.id, document);
2017-06-28 03:59:53 +00:00
};
@action remove = (id: string): void => {
2017-06-28 04:52:47 +00:00
this.data.delete(id);
2017-06-28 03:59:53 +00:00
};
2017-06-28 04:52:47 +00:00
getById = (id: string): ?Document => {
return this.data.get(id);
2017-06-28 03:59:53 +00:00
};
getByUrl = (url: string): ?Document => {
return _.find(this.data.values(), { url });
};
2017-06-28 04:20:09 +00:00
constructor() {
2017-06-28 03:59:53 +00:00
this.errors = stores.errors;
}
}
export default DocumentsStore;