2017-06-28 03:59:53 +00:00
|
|
|
// @flow
|
2017-07-16 18:47:48 +00:00
|
|
|
import {
|
|
|
|
observable,
|
|
|
|
action,
|
|
|
|
computed,
|
|
|
|
ObservableMap,
|
|
|
|
runInAction,
|
|
|
|
autorunAsync,
|
|
|
|
} from 'mobx';
|
2017-06-28 03:59:53 +00:00
|
|
|
import { client } from 'utils/ApiClient';
|
|
|
|
import _ from 'lodash';
|
|
|
|
import invariant from 'invariant';
|
|
|
|
|
2017-08-03 13:48:07 +00:00
|
|
|
import BaseStore from 'stores/BaseStore';
|
2017-06-28 03:59:53 +00:00
|
|
|
import stores from 'stores';
|
|
|
|
import Document from 'models/Document';
|
|
|
|
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
|
|
|
|
|
|
|
const DOCUMENTS_CACHE_KEY = 'DOCUMENTS_CACHE_KEY';
|
2017-06-28 03:59:53 +00:00
|
|
|
|
2017-07-16 18:54:23 +00:00
|
|
|
type Options = {
|
|
|
|
cache: CacheStore,
|
2017-08-29 15:37:17 +00:00
|
|
|
ui: UiStore,
|
2017-07-16 18:54:23 +00:00
|
|
|
};
|
|
|
|
|
2017-08-03 13:48:07 +00:00
|
|
|
class DocumentsStore extends BaseStore {
|
2017-06-28 04:52:47 +00:00
|
|
|
@observable recentlyViewedIds: Array<string> = [];
|
2017-06-28 05:15:29 +00:00
|
|
|
@observable data: Map<string, Document> = new ObservableMap([]);
|
2017-06-28 03:59:53 +00:00
|
|
|
@observable isLoaded: boolean = false;
|
2017-07-18 04:46:32 +00:00
|
|
|
@observable isFetching: boolean = false;
|
2017-07-18 05:24:19 +00:00
|
|
|
|
2017-06-28 03:59:53 +00:00
|
|
|
errors: ErrorsStore;
|
2017-07-16 18:47:48 +00:00
|
|
|
cache: CacheStore;
|
2017-08-29 15:37:17 +00:00
|
|
|
ui: UiStore;
|
2017-06-28 03:59:53 +00:00
|
|
|
|
2017-07-09 18:26:17 +00:00
|
|
|
/* Computed */
|
|
|
|
|
|
|
|
@computed get recentlyViewed(): Array<Document> {
|
2017-09-03 21:12:37 +00:00
|
|
|
return _.take(
|
|
|
|
_.filter(this.data.values(), ({ id }) =>
|
|
|
|
this.recentlyViewedIds.includes(id)
|
|
|
|
),
|
|
|
|
5
|
2017-07-09 18:26:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@computed get recentlyEdited(): Array<Document> {
|
|
|
|
// $FlowIssue
|
2017-09-03 21:12:37 +00:00
|
|
|
return _.take(this.data.values(), 5);
|
2017-07-09 18:26:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@computed get starred(): Array<Document> {
|
|
|
|
return _.filter(this.data.values(), 'starred');
|
|
|
|
}
|
|
|
|
|
2017-08-29 15:37:17 +00:00
|
|
|
@computed get active(): ?Document {
|
|
|
|
return this.ui.activeDocumentId
|
|
|
|
? this.getById(this.ui.activeDocumentId)
|
|
|
|
: undefined;
|
|
|
|
}
|
|
|
|
|
2017-06-28 03:59:53 +00:00
|
|
|
/* Actions */
|
|
|
|
|
2017-09-03 21:12:37 +00:00
|
|
|
@action fetchAll = async (
|
|
|
|
request: string = 'list',
|
|
|
|
options: ?Object
|
|
|
|
): Promise<*> => {
|
2017-07-18 04:46:32 +00:00
|
|
|
this.isFetching = true;
|
|
|
|
|
2017-06-28 03:59:53 +00:00
|
|
|
try {
|
2017-09-03 21:12:37 +00:00
|
|
|
const res = await client.post(`/documents.${request}`, options);
|
2017-06-28 03:59:53 +00:00
|
|
|
invariant(res && res.data, 'Document list not available');
|
|
|
|
const { data } = res;
|
|
|
|
runInAction('DocumentsStore#fetchAll', () => {
|
2017-06-28 04:52:47 +00:00
|
|
|
data.forEach(document => {
|
|
|
|
this.data.set(document.id, new Document(document));
|
|
|
|
});
|
2017-06-28 03:59:53 +00:00
|
|
|
this.isLoaded = true;
|
|
|
|
});
|
2017-06-28 04:20:09 +00:00
|
|
|
return data;
|
2017-06-28 03:59:53 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.errors.add('Failed to load documents');
|
2017-07-18 04:46:32 +00:00
|
|
|
} finally {
|
|
|
|
this.isFetching = false;
|
2017-06-28 03:59:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-03 21:12:37 +00:00
|
|
|
@action fetchRecentlyModified = async (options: ?Object): Promise<*> => {
|
2017-09-11 05:59:14 +00:00
|
|
|
return await this.fetchAll('list', options);
|
2017-09-03 21:12:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
@action fetchRecentlyViewed = async (options: ?Object): Promise<*> => {
|
|
|
|
const data = await this.fetchAll('viewed', options);
|
2017-06-28 04:52:47 +00:00
|
|
|
|
|
|
|
runInAction('DocumentsStore#fetchRecentlyViewed', () => {
|
2017-06-28 05:15:29 +00:00
|
|
|
this.recentlyViewedIds = _.map(data, 'id');
|
2017-06-28 04:52:47 +00:00
|
|
|
});
|
2017-09-03 21:12:37 +00:00
|
|
|
return data;
|
2017-06-28 04:20:09 +00:00
|
|
|
};
|
|
|
|
|
2017-06-28 05:15:29 +00:00
|
|
|
@action fetchStarred = async (): Promise<*> => {
|
|
|
|
await this.fetchAll('starred');
|
|
|
|
};
|
|
|
|
|
2017-06-28 03:59:53 +00:00
|
|
|
@action fetch = async (id: string): Promise<*> => {
|
2017-07-18 04:46:32 +00:00
|
|
|
this.isFetching = true;
|
|
|
|
|
2017-06-28 03:59:53 +00:00
|
|
|
try {
|
|
|
|
const res = await client.post('/documents.info', { id });
|
|
|
|
invariant(res && res.data, 'Document not available');
|
|
|
|
const { data } = res;
|
2017-07-09 05:12:14 +00:00
|
|
|
const document = new Document(data);
|
|
|
|
|
2017-06-28 03:59:53 +00:00
|
|
|
runInAction('DocumentsStore#fetch', () => {
|
2017-07-09 05:12:14 +00:00
|
|
|
this.data.set(data.id, document);
|
2017-06-28 03:59:53 +00:00
|
|
|
this.isLoaded = true;
|
|
|
|
});
|
2017-07-09 05:12:14 +00:00
|
|
|
|
|
|
|
return document;
|
2017-06-28 03:59:53 +00:00
|
|
|
} catch (e) {
|
|
|
|
this.errors.add('Failed to load documents');
|
2017-07-18 04:46:32 +00:00
|
|
|
} finally {
|
|
|
|
this.isFetching = false;
|
2017-06-28 03:59:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@action add = (document: Document): void => {
|
2017-06-28 04:52:47 +00:00
|
|
|
this.data.set(document.id, document);
|
2017-06-28 03:59:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
@action remove = (id: string): void => {
|
2017-06-28 04:52:47 +00:00
|
|
|
this.data.delete(id);
|
2017-06-28 03:59:53 +00:00
|
|
|
};
|
|
|
|
|
2017-06-28 04:52:47 +00:00
|
|
|
getById = (id: string): ?Document => {
|
|
|
|
return this.data.get(id);
|
2017-06-28 03:59:53 +00:00
|
|
|
};
|
|
|
|
|
2017-09-13 02:55:01 +00:00
|
|
|
/**
|
|
|
|
* Match documents by the url ID as the title slug can change
|
|
|
|
*/
|
2017-06-29 04:46:00 +00:00
|
|
|
getByUrl = (url: string): ?Document => {
|
2017-09-13 02:55:01 +00:00
|
|
|
return _.find(this.data.values(), doc => url.endsWith(doc.urlId));
|
2017-06-29 04:46:00 +00:00
|
|
|
};
|
|
|
|
|
2017-07-16 18:47:48 +00:00
|
|
|
constructor(options: Options) {
|
2017-08-03 13:48:07 +00:00
|
|
|
super();
|
|
|
|
|
2017-06-28 03:59:53 +00:00
|
|
|
this.errors = stores.errors;
|
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-07-16 18:47:48 +00:00
|
|
|
|
|
|
|
this.cache.getItem(DOCUMENTS_CACHE_KEY).then(data => {
|
|
|
|
if (data) {
|
|
|
|
data.forEach(document => this.add(new Document(document)));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-29 06:50:45 +00:00
|
|
|
this.on('documents.delete', (data: { id: string }) => {
|
2017-08-03 13:48:07 +00:00
|
|
|
this.remove(data.id);
|
|
|
|
});
|
|
|
|
|
2017-07-16 18:47:48 +00:00
|
|
|
autorunAsync('DocumentsStore.persists', () => {
|
|
|
|
if (this.data.size) {
|
|
|
|
this.cache.setItem(
|
|
|
|
DOCUMENTS_CACHE_KEY,
|
|
|
|
Array.from(this.data.values()).map(collection => collection.data)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
2017-06-28 03:59:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DocumentsStore;
|