This commit is contained in:
Tom Moor
2017-06-27 21:20:09 -07:00
parent 72fd39b494
commit 4c3f4fea16
2 changed files with 21 additions and 18 deletions

View File

@ -8,10 +8,6 @@ import stores from 'stores';
import Document from 'models/Document';
import ErrorsStore from 'stores/ErrorsStore';
type Options = {
teamId: string,
};
class DocumentsStore {
@observable data: Object = {};
@observable isLoaded: boolean = false;
@ -19,9 +15,9 @@ class DocumentsStore {
/* Actions */
@action fetchAll = async (): Promise<*> => {
@action fetchAll = async (request?: string = 'list'): Promise<*> => {
try {
const res = await client.post('/documents.list');
const res = await client.post(`/documents.${request}`);
invariant(res && res.data, 'Document list not available');
const { data } = res;
runInAction('DocumentsStore#fetchAll', () => {
@ -35,18 +31,29 @@ class DocumentsStore {
};
this.isLoaded = true;
});
return data;
} catch (e) {
this.errors.add('Failed to load documents');
}
};
@action fetchRecent = async (): Promise<*> => {
const data = await this.fetchAll('recent');
console.log(data);
};
@action fetchViewed = async (): Promise<*> => {
const data = await this.fetchAll('viewed');
console.log(data);
};
@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;
runInAction('DocumentsStore#fetch', () => {
this.update(new Document(data));
this.add(new Document(data));
this.isLoaded = true;
});
} catch (e) {
@ -62,20 +69,11 @@ class DocumentsStore {
delete this.data[id];
};
@action update = (document: Document): void => {
const existing = this.data[document.id];
this.data[document.id] = {
...existing,
document,
};
};
getById = (id: string): Document => {
return _.find(this.data, { id });
};
constructor(options: Options) {
constructor() {
this.errors = stores.errors;
}
}