This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/app/stores/CollectionsStore.js

175 lines
4.2 KiB
JavaScript
Raw Normal View History

// @flow
2017-11-20 04:16:49 +00:00
import { observable, computed, action, runInAction, ObservableMap } from 'mobx';
import { client } from 'utils/ApiClient';
import _ from 'lodash';
2017-05-30 02:08:03 +00:00
import invariant from 'invariant';
import BaseStore from './BaseStore';
import UiStore from './UiStore';
import Collection from 'models/Collection';
import naturalSort from 'shared/utils/naturalSort';
import type { PaginationParams } from 'types';
2017-07-16 18:47:48 +00:00
type Options = {
ui: UiStore,
};
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
};
class CollectionsStore extends BaseStore {
2017-11-20 04:16:49 +00:00
@observable data: Map<string, Collection> = new ObservableMap([]);
@observable isLoaded: boolean = false;
2017-11-20 04:16:49 +00:00
@observable isFetching: boolean = false;
ui: UiStore;
2017-11-10 22:14:30 +00:00
@computed
get active(): ?Collection {
return this.ui.activeCollectionId
? this.getById(this.ui.activeCollectionId)
: undefined;
}
2017-11-10 22:14:30 +00:00
@computed
get orderedData(): Collection[] {
return naturalSort(Array.from(this.data.values()), 'name');
}
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;
}
/* Actions */
2017-11-10 22:14:30 +00:00
@action
fetchPage = async (options: ?PaginationParams): Promise<*> => {
2017-11-20 04:16:49 +00:00
this.isFetching = true;
try {
const res = await client.post('/collections.list', options);
2017-05-30 02:08:03 +00:00
invariant(res && res.data, 'Collection list not available');
const { data } = res;
runInAction('CollectionsStore#fetchPage', () => {
2017-11-20 04:16:49 +00:00
data.forEach(collection => {
this.data.set(collection.id, new Collection(collection));
});
this.isLoaded = true;
});
return res;
} 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-11-10 22:14:30 +00:00
@action
2017-11-20 04:16:49 +00:00
fetch = async (id: string): Promise<?Collection> => {
let collection = this.getById(id);
2017-11-20 04:16:49 +00:00
if (collection) return collection;
this.isFetching = true;
try {
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-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;
}
};
@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
};
getById = (id: string): ?Collection => {
2017-11-20 04:16:49 +00:00
return this.data.get(id);
};
constructor(options: Options) {
super();
this.ui = options.ui;
this.on('collections.delete', (data: { id: string }) => {
this.remove(data.id);
});
}
}
export default CollectionsStore;