2017-05-23 07:45:15 +00:00
|
|
|
// @flow
|
2019-10-06 01:42:03 +00:00
|
|
|
import { pick } from 'lodash';
|
2019-01-05 21:37:33 +00:00
|
|
|
import { action, computed, observable } from 'mobx';
|
2017-08-03 13:48:07 +00:00
|
|
|
import BaseModel from 'models/BaseModel';
|
2018-05-08 05:08:47 +00:00
|
|
|
import Document from 'models/Document';
|
2017-07-09 17:27:29 +00:00
|
|
|
import { client } from 'utils/ApiClient';
|
2017-05-23 07:45:15 +00:00
|
|
|
import type { NavigationNode } from 'types';
|
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
export default class Collection extends BaseModel {
|
2019-01-05 21:37:33 +00:00
|
|
|
@observable isSaving: boolean;
|
|
|
|
@observable isLoadingUsers: boolean;
|
2017-07-09 17:27:29 +00:00
|
|
|
|
2017-05-23 07:45:15 +00:00
|
|
|
id: string;
|
|
|
|
name: string;
|
2018-12-05 06:24:30 +00:00
|
|
|
description: string;
|
2017-10-30 06:22:46 +00:00
|
|
|
color: string;
|
2019-01-05 21:37:33 +00:00
|
|
|
private: boolean;
|
2017-05-23 07:45:15 +00:00
|
|
|
type: 'atlas' | 'journal';
|
2018-05-08 05:08:47 +00:00
|
|
|
documents: NavigationNode[];
|
2018-12-05 06:24:30 +00:00
|
|
|
createdAt: ?string;
|
|
|
|
updatedAt: ?string;
|
2019-04-18 02:11:23 +00:00
|
|
|
deletedAt: ?string;
|
2017-05-23 07:45:15 +00:00
|
|
|
url: string;
|
|
|
|
|
2019-04-09 04:25:13 +00:00
|
|
|
@computed
|
|
|
|
get isPrivate(): boolean {
|
|
|
|
return this.private;
|
|
|
|
}
|
|
|
|
|
2017-11-20 04:16:49 +00:00
|
|
|
@computed
|
|
|
|
get isEmpty(): boolean {
|
|
|
|
return this.documents.length === 0;
|
|
|
|
}
|
|
|
|
|
2017-11-28 16:55:15 +00:00
|
|
|
@computed
|
|
|
|
get documentIds(): string[] {
|
|
|
|
const results = [];
|
|
|
|
const travelDocuments = (documentList, path) =>
|
|
|
|
documentList.forEach(document => {
|
|
|
|
results.push(document.id);
|
|
|
|
travelDocuments(document.children);
|
|
|
|
});
|
|
|
|
|
|
|
|
travelDocuments(this.documents);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2018-05-08 05:08:47 +00:00
|
|
|
@action
|
|
|
|
updateDocument(document: Document) {
|
|
|
|
const travelDocuments = (documentList, path) =>
|
|
|
|
documentList.forEach(d => {
|
|
|
|
if (d.id === document.id) {
|
|
|
|
d.title = document.title;
|
|
|
|
d.url = document.url;
|
|
|
|
} else {
|
|
|
|
travelDocuments(d.children);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
travelDocuments(this.documents);
|
|
|
|
}
|
|
|
|
|
2019-04-18 02:11:23 +00:00
|
|
|
pathToDocument(document: Document) {
|
|
|
|
let path;
|
|
|
|
const traveler = (nodes, previousPath) => {
|
|
|
|
nodes.forEach(childNode => {
|
|
|
|
const newPath = [...previousPath, childNode];
|
|
|
|
if (childNode.id === document.id) {
|
|
|
|
path = newPath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return traveler(childNode.children, newPath);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.documents) {
|
|
|
|
traveler(this.documents, []);
|
|
|
|
if (path) return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
toJS = () => {
|
2019-01-05 21:37:33 +00:00
|
|
|
return pick(this, ['id', 'name', 'color', 'description', 'private']);
|
2017-05-23 07:45:15 +00:00
|
|
|
};
|
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
export = () => {
|
2019-07-30 05:35:34 +00:00
|
|
|
return client.get(
|
|
|
|
'/collections.export',
|
|
|
|
{ id: this.id },
|
|
|
|
{ download: true }
|
|
|
|
);
|
2018-06-21 04:33:21 +00:00
|
|
|
};
|
2017-05-23 07:45:15 +00:00
|
|
|
}
|