2017-05-26 19:56:10 +00:00
|
|
|
// @flow
|
2017-07-16 18:47:48 +00:00
|
|
|
import {
|
|
|
|
observable,
|
2017-08-29 15:37:17 +00:00
|
|
|
computed,
|
2017-07-16 18:47:48 +00:00
|
|
|
action,
|
|
|
|
runInAction,
|
|
|
|
ObservableArray,
|
|
|
|
} from 'mobx';
|
2017-05-26 19:56:10 +00:00
|
|
|
import ApiClient, { client } from 'utils/ApiClient';
|
|
|
|
import _ from 'lodash';
|
2017-05-30 02:08:03 +00:00
|
|
|
import invariant from 'invariant';
|
2017-05-26 19:56:10 +00:00
|
|
|
|
|
|
|
import stores from 'stores';
|
|
|
|
import Collection from 'models/Collection';
|
|
|
|
import ErrorsStore from 'stores/ErrorsStore';
|
2017-07-16 18:47:48 +00:00
|
|
|
import CacheStore from 'stores/CacheStore';
|
2017-08-29 15:37:17 +00:00
|
|
|
import UiStore from 'stores/UiStore';
|
2017-07-16 18:47:48 +00:00
|
|
|
|
2017-05-26 19:56:10 +00:00
|
|
|
type Options = {
|
|
|
|
teamId: string,
|
2017-07-16 18:47:48 +00:00
|
|
|
cache: CacheStore,
|
2017-08-29 15:37:17 +00:00
|
|
|
ui: UiStore,
|
2017-05-26 19:56:10 +00:00
|
|
|
};
|
|
|
|
|
2017-10-02 06:42:17 +00:00
|
|
|
type DocumentPathItem = {
|
|
|
|
id: string,
|
|
|
|
title: string,
|
|
|
|
type: 'document' | 'collection',
|
|
|
|
};
|
|
|
|
|
|
|
|
export type DocumentPath = DocumentPathItem & {
|
|
|
|
path: Array<DocumentPathItem>,
|
|
|
|
};
|
|
|
|
|
2017-05-26 19:56:10 +00:00
|
|
|
class CollectionsStore {
|
|
|
|
@observable data: ObservableArray<Collection> = observable.array([]);
|
|
|
|
@observable isLoaded: boolean = false;
|
|
|
|
|
|
|
|
client: ApiClient;
|
|
|
|
teamId: string;
|
|
|
|
errors: ErrorsStore;
|
2017-07-16 18:47:48 +00:00
|
|
|
cache: CacheStore;
|
2017-08-29 15:37:17 +00:00
|
|
|
ui: UiStore;
|
|
|
|
|
|
|
|
@computed get active(): ?Collection {
|
|
|
|
return this.ui.activeCollectionId
|
|
|
|
? this.getById(this.ui.activeCollectionId)
|
|
|
|
: undefined;
|
|
|
|
}
|
2017-05-26 19:56:10 +00:00
|
|
|
|
2017-09-27 07:11:26 +00:00
|
|
|
/**
|
|
|
|
* List of paths to each of the documents, where paths are composed of id and title/name pairs
|
|
|
|
*/
|
2017-10-02 06:42:17 +00:00
|
|
|
@computed get pathsToDocuments(): Array<DocumentPath> {
|
2017-09-27 07:11:26 +00:00
|
|
|
let results = [];
|
|
|
|
const travelDocuments = (documentList, path) =>
|
|
|
|
documentList.forEach(document => {
|
|
|
|
const { id, title } = document;
|
2017-10-02 01:36:44 +00:00
|
|
|
const node = { id, title, type: 'document' };
|
2017-09-27 07:11:26 +00:00
|
|
|
results.push(_.concat(path, node));
|
|
|
|
travelDocuments(document.children, _.concat(path, [node]));
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.isLoaded) {
|
|
|
|
this.data.forEach(collection => {
|
|
|
|
const { id, name } = collection;
|
2017-10-02 01:36:44 +00:00
|
|
|
const node = { id, title: name, type: 'collection' };
|
|
|
|
results.push([node]);
|
2017-09-27 07:11:26 +00:00
|
|
|
travelDocuments(collection.documents, [node]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-02 06:42:17 +00:00
|
|
|
return results.map(result => {
|
|
|
|
const tail = _.last(result);
|
|
|
|
return {
|
|
|
|
...tail,
|
|
|
|
path: result,
|
|
|
|
};
|
|
|
|
});
|
2017-09-27 07:11:26 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 06:42:17 +00:00
|
|
|
getPathForDocument(documentId: string): ?DocumentPath {
|
|
|
|
return this.pathsToDocuments.find(path => path.id === documentId);
|
2017-10-02 01:36:44 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 19:56:10 +00:00
|
|
|
/* Actions */
|
|
|
|
|
2017-06-28 03:59:53 +00:00
|
|
|
@action fetchAll = async (): Promise<*> => {
|
2017-05-26 19:56:10 +00:00
|
|
|
try {
|
|
|
|
const res = await this.client.post('/collections.list', {
|
|
|
|
id: this.teamId,
|
|
|
|
});
|
2017-05-30 02:08:03 +00:00
|
|
|
invariant(res && res.data, 'Collection list not available');
|
2017-05-26 19:56:10 +00:00
|
|
|
const { data } = res;
|
|
|
|
runInAction('CollectionsStore#fetch', () => {
|
2017-05-30 03:00:08 +00:00
|
|
|
this.data.replace(data.map(collection => new Collection(collection)));
|
2017-05-26 19:56:10 +00:00
|
|
|
this.isLoaded = true;
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
this.errors.add('Failed to load collections');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-29 15:37:17 +00:00
|
|
|
@action fetchById = async (id: string): Promise<?Collection> => {
|
|
|
|
let collection = this.getById(id);
|
2017-07-18 06:30:16 +00:00
|
|
|
if (!collection) {
|
|
|
|
try {
|
|
|
|
const res = await this.client.post('/collections.info', {
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
invariant(res && res.data, 'Collection not available');
|
|
|
|
const { data } = res;
|
|
|
|
runInAction('CollectionsStore#getById', () => {
|
|
|
|
collection = new Collection(data);
|
|
|
|
this.add(collection);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2017-07-19 06:36:49 +00:00
|
|
|
Bugsnag.notify(e);
|
|
|
|
this.errors.add('Something went wrong');
|
2017-07-18 06:30:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return collection;
|
2017-05-26 19:56:10 +00:00
|
|
|
};
|
|
|
|
|
2017-07-10 03:02:10 +00:00
|
|
|
@action add = (collection: Collection): void => {
|
|
|
|
this.data.push(collection);
|
|
|
|
};
|
|
|
|
|
|
|
|
@action remove = (id: string): void => {
|
|
|
|
this.data.splice(this.data.indexOf(id), 1);
|
|
|
|
};
|
|
|
|
|
2017-08-29 15:37:17 +00:00
|
|
|
getById = (id: string): ?Collection => {
|
|
|
|
return _.find(this.data, { id });
|
|
|
|
};
|
|
|
|
|
2017-05-26 19:56:10 +00:00
|
|
|
constructor(options: Options) {
|
|
|
|
this.client = client;
|
|
|
|
this.errors = stores.errors;
|
|
|
|
this.teamId = options.teamId;
|
2017-07-16 18:47:48 +00:00
|
|
|
this.cache = options.cache;
|
2017-08-29 15:37:17 +00:00
|
|
|
this.ui = options.ui;
|
2017-05-26 19:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default CollectionsStore;
|