Refactor to Map

This commit is contained in:
Tom Moor
2017-06-27 21:52:47 -07:00
parent 4c3f4fea16
commit 07c362cd54

View File

@ -1,5 +1,5 @@
// @flow // @flow
import { observable, action, runInAction } from 'mobx'; import { observable, action, map, runInAction } from 'mobx';
import { client } from 'utils/ApiClient'; import { client } from 'utils/ApiClient';
import _ from 'lodash'; import _ from 'lodash';
import invariant from 'invariant'; import invariant from 'invariant';
@ -9,26 +9,22 @@ import Document from 'models/Document';
import ErrorsStore from 'stores/ErrorsStore'; import ErrorsStore from 'stores/ErrorsStore';
class DocumentsStore { class DocumentsStore {
@observable data: Object = {}; @observable recentlyViewedIds: Array<string> = [];
@observable data: Map<string, Document> = map([]);
@observable isLoaded: boolean = false; @observable isLoaded: boolean = false;
errors: ErrorsStore; errors: ErrorsStore;
/* Actions */ /* Actions */
@action fetchAll = async (request?: string = 'list'): Promise<*> => { @action fetchAll = async (request: string = 'list'): Promise<*> => {
try { try {
const res = await client.post(`/documents.${request}`); const res = await client.post(`/documents.${request}`);
invariant(res && res.data, 'Document list not available'); invariant(res && res.data, 'Document list not available');
const { data } = res; const { data } = res;
runInAction('DocumentsStore#fetchAll', () => { runInAction('DocumentsStore#fetchAll', () => {
const loaded = _.keyBy( data.forEach(document => {
data.map(document => new Document(document)), this.data.set(document.id, new Document(document));
'id' });
);
this.data = {
...this.data,
...loaded,
};
this.isLoaded = true; this.isLoaded = true;
}); });
return data; return data;
@ -37,14 +33,16 @@ class DocumentsStore {
} }
}; };
@action fetchRecent = async (): Promise<*> => { @action fetchRecentlyEdited = async (): Promise<*> => {
const data = await this.fetchAll('recent'); await this.fetchAll('recent');
console.log(data);
}; };
@action fetchViewed = async (): Promise<*> => { @action fetchRecentlyViewed = async (): Promise<*> => {
const data = await this.fetchAll('viewed'); const data = await this.fetchAll('viewed');
console.log(data);
runInAction('DocumentsStore#fetchRecentlyViewed', () => {
this.recentlyViewedIds = _.pick(data, 'id');
});
}; };
@action fetch = async (id: string): Promise<*> => { @action fetch = async (id: string): Promise<*> => {
@ -53,7 +51,7 @@ class DocumentsStore {
invariant(res && res.data, 'Document not available'); invariant(res && res.data, 'Document not available');
const { data } = res; const { data } = res;
runInAction('DocumentsStore#fetch', () => { runInAction('DocumentsStore#fetch', () => {
this.add(new Document(data)); this.data.set(data.id, new Document(data));
this.isLoaded = true; this.isLoaded = true;
}); });
} catch (e) { } catch (e) {
@ -62,15 +60,15 @@ class DocumentsStore {
}; };
@action add = (document: Document): void => { @action add = (document: Document): void => {
this.data[document.id] = document; this.data.set(document.id, document);
}; };
@action remove = (id: string): void => { @action remove = (id: string): void => {
delete this.data[id]; this.data.delete(id);
}; };
getById = (id: string): Document => { getById = (id: string): ?Document => {
return _.find(this.data, { id }); return this.data.get(id);
}; };
constructor() { constructor() {