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/models/Collection.js

150 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-05-23 07:45:15 +00:00
// @flow
import { extendObservable, action, computed, runInAction } from 'mobx';
2017-05-23 07:45:15 +00:00
import invariant from 'invariant';
import BaseModel from 'models/BaseModel';
2017-07-09 17:27:29 +00:00
import { client } from 'utils/ApiClient';
2017-05-23 07:45:15 +00:00
import stores from 'stores';
import ErrorsStore from 'stores/ErrorsStore';
import type { NavigationNode } from 'types';
class Collection extends BaseModel {
2017-07-09 17:27:29 +00:00
isSaving: boolean = false;
hasPendingChanges: boolean = false;
errors: ErrorsStore;
2017-07-16 18:47:48 +00:00
data: Object;
2017-07-09 17:27:29 +00:00
2017-05-23 07:45:15 +00:00
createdAt: string;
description: ?string;
id: string;
name: string;
2017-10-30 06:22:46 +00:00
color: string;
2017-05-23 07:45:15 +00:00
type: 'atlas' | 'journal';
2017-06-06 06:12:47 +00:00
documents: Array<NavigationNode>;
2017-05-23 07:45:15 +00:00
updatedAt: string;
url: string;
/* Computed */
2017-11-10 22:14:30 +00:00
@computed
get entryUrl(): string {
return this.type === 'atlas' && this.documents.length > 0
? this.documents[0].url
: this.url;
}
2017-11-20 04:16:49 +00:00
@computed
get isEmpty(): boolean {
return this.documents.length === 0;
}
@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;
}
2017-05-23 07:45:15 +00:00
/* Actions */
2017-11-10 22:14:30 +00:00
@action
fetch = async () => {
2017-05-23 07:45:15 +00:00
try {
2017-07-09 17:27:29 +00:00
const res = await client.post('/collections.info', { id: this.id });
2017-05-23 07:45:15 +00:00
invariant(res && res.data, 'API response should be available');
const { data } = res;
2017-07-09 17:27:29 +00:00
runInAction('Collection#fetch', () => {
2017-05-23 07:45:15 +00:00
this.updateData(data);
});
} catch (e) {
this.errors.add('Collection failed loading');
}
2017-07-09 17:27:29 +00:00
return this;
};
2017-11-10 22:14:30 +00:00
@action
save = async () => {
2017-07-09 17:27:29 +00:00
if (this.isSaving) return this;
this.isSaving = true;
2017-10-30 06:22:46 +00:00
const params = {
name: this.name,
color: this.color,
description: this.description,
};
2017-07-09 17:27:29 +00:00
try {
let res;
if (this.id) {
res = await client.post('/collections.update', {
id: this.id,
2017-10-30 06:22:46 +00:00
...params,
2017-07-09 17:27:29 +00:00
});
} else {
2017-10-30 06:22:46 +00:00
res = await client.post('/collections.create', params);
2017-07-09 17:27:29 +00:00
}
runInAction('Collection#save', () => {
invariant(res && res.data, 'Data should be available');
this.updateData(res.data);
this.hasPendingChanges = false;
});
2017-07-09 17:27:29 +00:00
} catch (e) {
this.errors.add('Collection failed saving');
2017-07-10 03:56:36 +00:00
return false;
2017-07-09 17:27:29 +00:00
} finally {
this.isSaving = false;
}
2017-07-10 03:56:36 +00:00
return true;
2017-05-23 07:45:15 +00:00
};
2017-11-10 22:14:30 +00:00
@action
delete = async () => {
try {
2017-09-12 06:41:12 +00:00
await client.post('/collections.delete', { id: this.id });
this.emit('collections.delete', { id: this.id });
2017-09-12 06:41:12 +00:00
return true;
} catch (e) {
this.errors.add('Collection failed to delete');
}
2017-09-12 06:41:12 +00:00
return false;
};
2017-11-10 22:14:30 +00:00
@action
updateData(data: Object = {}) {
2017-07-16 18:47:48 +00:00
this.data = data;
2017-05-23 07:45:15 +00:00
extendObservable(this, data);
}
2017-07-09 17:27:29 +00:00
constructor(collection: Object = {}) {
super();
2017-05-23 07:45:15 +00:00
this.updateData(collection);
this.errors = stores.errors;
2017-08-29 06:50:45 +00:00
this.on('documents.delete', (data: { collectionId: string }) => {
if (data.collectionId === this.id) this.fetch();
});
this.on(
'collections.update',
(data: { id: string, collection: Collection }) => {
// FIXME: calling this.updateData won't update the
// UI. Some mobx issue
if (data.id === this.id) this.fetch();
}
);
2017-09-04 21:48:56 +00:00
this.on('documents.move', (data: { collectionId: string }) => {
if (data.collectionId === this.id) this.fetch();
});
2017-05-23 07:45:15 +00:00
}
}
export default Collection;