This commit is contained in:
Jori Lallo
2017-09-27 00:11:26 -07:00
parent 6b10c7308c
commit 9fc1731f99
3 changed files with 51 additions and 29 deletions

View File

@ -5,7 +5,6 @@ import {
action,
runInAction,
ObservableArray,
autorunAsync,
} from 'mobx';
import ApiClient, { client } from 'utils/ApiClient';
import _ from 'lodash';
@ -17,8 +16,6 @@ import ErrorsStore from 'stores/ErrorsStore';
import CacheStore from 'stores/CacheStore';
import UiStore from 'stores/UiStore';
const COLLECTION_CACHE_KEY = 'COLLECTION_CACHE_KEY';
type Options = {
teamId: string,
cache: CacheStore,
@ -41,6 +38,30 @@ class CollectionsStore {
: undefined;
}
/**
* List of paths to each of the documents, where paths are composed of id and title/name pairs
*/
@computed get pathsToDocuments(): ?[[{ id: string, title: string }]] {
let results = [];
const travelDocuments = (documentList, path) =>
documentList.forEach(document => {
const { id, title } = document;
const node = { id, title };
results.push(_.concat(path, node));
travelDocuments(document.children, _.concat(path, [node]));
});
if (this.isLoaded) {
this.data.forEach(collection => {
const { id, name } = collection;
const node = { id, title: name };
travelDocuments(collection.documents, [node]);
});
}
return results;
}
/* Actions */
@action fetchAll = async (): Promise<*> => {
@ -99,21 +120,6 @@ class CollectionsStore {
this.teamId = options.teamId;
this.cache = options.cache;
this.ui = options.ui;
//
// this.cache.getItem(COLLECTION_CACHE_KEY).then(data => {
// if (data) {
// this.data.replace(data.map(collection => new Collection(collection)));
// this.isLoaded = true;
// }
// });
autorunAsync('CollectionsStore.persists', () => {
if (this.data.length > 0)
this.cache.setItem(
COLLECTION_CACHE_KEY,
this.data.map(collection => collection.data)
);
});
}
}