2017-05-26 19:56:10 +00:00
|
|
|
// @flow
|
2017-11-20 04:16:49 +00:00
|
|
|
import { observable, computed, action, runInAction, ObservableMap } from 'mobx';
|
2018-02-04 20:30:35 +00:00
|
|
|
import { client } from 'utils/ApiClient';
|
2017-05-26 19:56:10 +00:00
|
|
|
import _ from 'lodash';
|
2017-05-30 02:08:03 +00:00
|
|
|
import invariant from 'invariant';
|
2017-05-26 19:56:10 +00:00
|
|
|
|
2018-04-04 03:36:25 +00:00
|
|
|
import BaseStore from './BaseStore';
|
|
|
|
import UiStore from './UiStore';
|
2017-05-26 19:56:10 +00:00
|
|
|
import Collection from 'models/Collection';
|
2018-03-01 07:00:41 +00:00
|
|
|
import naturalSort from 'shared/utils/naturalSort';
|
2018-02-04 20:30:35 +00:00
|
|
|
import type { PaginationParams } from 'types';
|
2017-07-16 18:47:48 +00:00
|
|
|
|
2017-05-26 19:56:10 +00:00
|
|
|
type Options = {
|
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,
|
2017-10-22 18:02:05 +00:00
|
|
|
url: string,
|
2017-10-02 06:42:17 +00:00
|
|
|
type: 'document' | 'collection',
|
|
|
|
};
|
|
|
|
|
|
|
|
export type DocumentPath = DocumentPathItem & {
|
2017-11-20 04:16:49 +00:00
|
|
|
path: DocumentPathItem[],
|
2017-10-02 06:42:17 +00:00
|
|
|
};
|
|
|
|
|
2018-04-04 03:36:25 +00:00
|
|
|
class CollectionsStore extends BaseStore {
|
2017-11-20 04:16:49 +00:00
|
|
|
@observable data: Map<string, Collection> = new ObservableMap([]);
|
2017-05-26 19:56:10 +00:00
|
|
|
@observable isLoaded: boolean = false;
|
2017-11-20 04:16:49 +00:00
|
|
|
@observable isFetching: boolean = false;
|
2017-05-26 19:56:10 +00:00
|
|
|
|
2017-08-29 15:37:17 +00:00
|
|
|
ui: UiStore;
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
|
|
|
get active(): ?Collection {
|
2017-08-29 15:37:17 +00:00
|
|
|
return this.ui.activeCollectionId
|
|
|
|
? this.getById(this.ui.activeCollectionId)
|
|
|
|
: undefined;
|
|
|
|
}
|
2017-05-26 19:56:10 +00:00
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
|
|
|
get orderedData(): Collection[] {
|
2018-03-01 07:00:41 +00:00
|
|
|
return naturalSort(Array.from(this.data.values()), 'name');
|
2017-10-10 03:59:01 +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-11-10 22:14:30 +00:00
|
|
|
@computed
|
|
|
|
get pathsToDocuments(): Array<DocumentPath> {
|
2017-09-27 07:11:26 +00:00
|
|
|
let results = [];
|
|
|
|
const travelDocuments = (documentList, path) =>
|
|
|
|
documentList.forEach(document => {
|
2017-10-22 18:02:05 +00:00
|
|
|
const { id, title, url } = document;
|
|
|
|
const node = { id, title, url, 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 => {
|
2017-10-22 18:02:05 +00:00
|
|
|
const { id, name, url } = collection;
|
|
|
|
const node = { id, title: name, url, type: 'collection' };
|
2017-10-02 01:36:44 +00:00
|
|
|
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-10-22 18:02:05 +00:00
|
|
|
titleForDocument(documentUrl: string): ?string {
|
|
|
|
const path = this.pathsToDocuments.find(path => path.url === documentUrl);
|
|
|
|
if (path) return path.title;
|
|
|
|
}
|
|
|
|
|
2017-05-26 19:56:10 +00:00
|
|
|
/* Actions */
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2018-02-04 20:30:35 +00:00
|
|
|
fetchPage = async (options: ?PaginationParams): Promise<*> => {
|
2017-11-20 04:16:49 +00:00
|
|
|
this.isFetching = true;
|
|
|
|
|
2017-05-26 19:56:10 +00:00
|
|
|
try {
|
2018-02-04 20:30:35 +00:00
|
|
|
const res = await client.post('/collections.list', options);
|
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;
|
2018-02-04 20:30:35 +00:00
|
|
|
runInAction('CollectionsStore#fetchPage', () => {
|
2017-11-20 04:16:49 +00:00
|
|
|
data.forEach(collection => {
|
|
|
|
this.data.set(collection.id, new Collection(collection));
|
|
|
|
});
|
2017-05-26 19:56:10 +00:00
|
|
|
this.isLoaded = true;
|
|
|
|
});
|
2018-02-04 20:30:35 +00:00
|
|
|
return res;
|
2017-05-26 19:56:10 +00:00
|
|
|
} catch (e) {
|
2018-05-31 18:42:39 +00:00
|
|
|
this.ui.showToast('Failed to load collections');
|
2017-11-20 04:16:49 +00:00
|
|
|
} finally {
|
|
|
|
this.isFetching = false;
|
2017-05-26 19:56:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2017-11-20 04:16:49 +00:00
|
|
|
fetch = async (id: string): Promise<?Collection> => {
|
2017-08-29 15:37:17 +00:00
|
|
|
let collection = this.getById(id);
|
2017-11-20 04:16:49 +00:00
|
|
|
if (collection) return collection;
|
|
|
|
|
|
|
|
this.isFetching = true;
|
|
|
|
|
|
|
|
try {
|
2018-02-04 20:30:35 +00:00
|
|
|
const res = await client.post('/collections.info', {
|
2017-11-20 04:16:49 +00:00
|
|
|
id,
|
|
|
|
});
|
|
|
|
invariant(res && res.data, 'Collection not available');
|
|
|
|
const { data } = res;
|
|
|
|
const collection = new Collection(data);
|
|
|
|
|
|
|
|
runInAction('CollectionsStore#fetch', () => {
|
|
|
|
this.data.set(data.id, collection);
|
|
|
|
this.isLoaded = true;
|
|
|
|
});
|
2017-07-18 06:30:16 +00:00
|
|
|
|
2017-11-20 04:16:49 +00:00
|
|
|
return collection;
|
|
|
|
} catch (e) {
|
2018-05-31 18:42:39 +00:00
|
|
|
this.ui.showToast('Something went wrong');
|
2017-11-20 04:16:49 +00:00
|
|
|
} finally {
|
|
|
|
this.isFetching = false;
|
|
|
|
}
|
2017-05-26 19:56:10 +00:00
|
|
|
};
|
|
|
|
|
2018-06-21 04:33:21 +00:00
|
|
|
@action
|
|
|
|
export = async () => {
|
|
|
|
try {
|
|
|
|
await client.post('/collections.exportAll');
|
|
|
|
return true;
|
|
|
|
} catch (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
|
|
|
add = (collection: Collection): void => {
|
2017-11-20 04:16:49 +00:00
|
|
|
this.data.set(collection.id, collection);
|
2017-07-10 03:02:10 +00:00
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
|
|
|
remove = (id: string): void => {
|
2017-11-20 04:16:49 +00:00
|
|
|
this.data.delete(id);
|
2017-07-10 03:02:10 +00:00
|
|
|
};
|
|
|
|
|
2017-08-29 15:37:17 +00:00
|
|
|
getById = (id: string): ?Collection => {
|
2017-11-20 04:16:49 +00:00
|
|
|
return this.data.get(id);
|
2017-08-29 15:37:17 +00:00
|
|
|
};
|
|
|
|
|
2017-05-26 19:56:10 +00:00
|
|
|
constructor(options: Options) {
|
2018-04-04 03:36:25 +00:00
|
|
|
super();
|
2017-08-29 15:37:17 +00:00
|
|
|
this.ui = options.ui;
|
2018-04-04 03:36:25 +00:00
|
|
|
|
|
|
|
this.on('collections.delete', (data: { id: string }) => {
|
|
|
|
this.remove(data.id);
|
|
|
|
});
|
2017-05-26 19:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default CollectionsStore;
|