2017-06-28 03:59:53 +00:00
|
|
|
// @flow
|
2018-05-13 04:01:17 +00:00
|
|
|
import { observable, action, computed, ObservableMap, runInAction } 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 Document from 'models/Document';
|
|
|
|
import ErrorsStore from 'stores/ErrorsStore';
|
2017-08-29 15:37:17 +00:00
|
|
|
import UiStore from 'stores/UiStore';
|
2017-12-04 00:50:50 +00:00
|
|
|
import type { PaginationParams } from 'types';
|
2017-07-16 18:47:48 +00:00
|
|
|
|
2017-12-04 00:50:50 +00:00
|
|
|
export const DEFAULT_PAGINATION_LIMIT = 25;
|
2017-06-28 03:59:53 +00:00
|
|
|
|
2017-07-16 18:54:23 +00:00
|
|
|
type Options = {
|
2017-08-29 15:37:17 +00:00
|
|
|
ui: UiStore,
|
2018-05-13 20:26:06 +00:00
|
|
|
errors: ErrorsStore,
|
|
|
|
};
|
|
|
|
|
|
|
|
type FetchOptions = {
|
|
|
|
prefetch?: boolean,
|
|
|
|
shareId?: string,
|
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> = [];
|
2018-01-29 01:04:53 +00:00
|
|
|
@observable recentlyEditedIds: 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-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 */
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
|
|
|
get recentlyViewed(): Array<Document> {
|
2018-01-29 01:04:53 +00:00
|
|
|
const docs = [];
|
|
|
|
this.recentlyViewedIds.forEach(id => {
|
|
|
|
const doc = this.getById(id);
|
|
|
|
if (doc) docs.push(doc);
|
|
|
|
});
|
|
|
|
return docs;
|
2017-07-09 18:26:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
2017-11-28 16:55:15 +00:00
|
|
|
get recentlyEdited(): Document[] {
|
2018-01-29 01:04:53 +00:00
|
|
|
const docs = [];
|
|
|
|
this.recentlyEditedIds.forEach(id => {
|
|
|
|
const doc = this.getById(id);
|
|
|
|
if (doc) docs.push(doc);
|
|
|
|
});
|
|
|
|
return docs;
|
2017-07-09 18:26:17 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
pinnedInCollection(collectionId: string): Document[] {
|
|
|
|
return _.filter(
|
|
|
|
this.recentlyEditedInCollection(collectionId),
|
|
|
|
document => document.pinned
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
recentlyEditedInCollection(collectionId: string): Document[] {
|
2017-11-20 05:32:18 +00:00
|
|
|
return _.orderBy(
|
2018-03-01 07:28:36 +00:00
|
|
|
_.filter(
|
|
|
|
this.data.values(),
|
|
|
|
document =>
|
|
|
|
document.collectionId === collectionId && !!document.publishedAt
|
2017-11-20 05:32:18 +00:00
|
|
|
),
|
|
|
|
'updatedAt',
|
|
|
|
'desc'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
2018-02-28 06:41:12 +00:00
|
|
|
get starred(): Document[] {
|
2017-07-09 18:26:17 +00:00
|
|
|
return _.filter(this.data.values(), 'starred');
|
|
|
|
}
|
|
|
|
|
2018-02-28 06:41:12 +00:00
|
|
|
@computed
|
|
|
|
get drafts(): Document[] {
|
|
|
|
return _.filter(
|
|
|
|
_.orderBy(this.data.values(), 'updatedAt', 'desc'),
|
|
|
|
doc => !doc.publishedAt
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@computed
|
|
|
|
get active(): ?Document {
|
2017-08-29 15:37:17 +00:00
|
|
|
return this.ui.activeDocumentId
|
|
|
|
? this.getById(this.ui.activeDocumentId)
|
|
|
|
: undefined;
|
|
|
|
}
|
|
|
|
|
2017-06-28 03:59:53 +00:00
|
|
|
/* Actions */
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2018-02-04 20:30:35 +00:00
|
|
|
fetchPage = async (
|
2017-12-04 00:50:50 +00:00
|
|
|
request: string = 'list',
|
|
|
|
options: ?PaginationParams
|
|
|
|
): 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;
|
2018-02-04 20:30:35 +00:00
|
|
|
runInAction('DocumentsStore#fetchPage', () => {
|
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-11-10 22:14:30 +00:00
|
|
|
@action
|
2017-12-04 00:50:50 +00:00
|
|
|
fetchRecentlyModified = async (options: ?PaginationParams): Promise<*> => {
|
2018-02-04 20:30:35 +00:00
|
|
|
const data = await this.fetchPage('list', options);
|
2018-01-29 01:04:53 +00:00
|
|
|
|
|
|
|
runInAction('DocumentsStore#fetchRecentlyModified', () => {
|
|
|
|
this.recentlyEditedIds = _.map(data, 'id');
|
|
|
|
});
|
|
|
|
return data;
|
2017-09-03 21:12:37 +00:00
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2017-12-04 00:50:50 +00:00
|
|
|
fetchRecentlyViewed = async (options: ?PaginationParams): Promise<*> => {
|
2018-02-04 20:30:35 +00:00
|
|
|
const data = await this.fetchPage('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-11-10 22:14:30 +00:00
|
|
|
@action
|
2018-02-28 06:41:12 +00:00
|
|
|
fetchStarred = async (options: ?PaginationParams): Promise<*> => {
|
|
|
|
await this.fetchPage('starred', options);
|
|
|
|
};
|
|
|
|
|
|
|
|
@action
|
|
|
|
fetchDrafts = async (options: ?PaginationParams): Promise<*> => {
|
|
|
|
await this.fetchPage('drafts', options);
|
2017-06-28 05:15:29 +00:00
|
|
|
};
|
|
|
|
|
2018-03-01 07:28:36 +00:00
|
|
|
@action
|
|
|
|
fetchPinned = async (options: ?PaginationParams): Promise<*> => {
|
|
|
|
await this.fetchPage('pinned', options);
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2017-12-04 00:50:50 +00:00
|
|
|
search = async (
|
|
|
|
query: string,
|
2018-02-28 06:41:12 +00:00
|
|
|
options: ?PaginationParams
|
2017-12-04 00:50:50 +00:00
|
|
|
): Promise<string[]> => {
|
|
|
|
const res = await client.get('/documents.search', {
|
|
|
|
...options,
|
|
|
|
query,
|
|
|
|
});
|
2017-09-13 06:30:18 +00:00
|
|
|
invariant(res && res.data, 'res or res.data missing');
|
|
|
|
const { data } = res;
|
|
|
|
data.forEach(documentData => this.add(new Document(documentData)));
|
|
|
|
return data.map(documentData => documentData.id);
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
|
|
|
prefetchDocument = async (id: string) => {
|
2018-05-13 20:26:06 +00:00
|
|
|
if (!this.getById(id)) {
|
|
|
|
this.fetch(id, { prefetch: true });
|
|
|
|
}
|
2017-10-20 07:27:01 +00:00
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@action
|
2018-05-13 20:26:06 +00:00
|
|
|
fetch = async (id: string, options?: FetchOptions = {}): Promise<*> => {
|
|
|
|
if (!options.prefetch) this.isFetching = true;
|
2017-07-18 04:46:32 +00:00
|
|
|
|
2017-06-28 03:59:53 +00:00
|
|
|
try {
|
2018-05-13 20:26:06 +00:00
|
|
|
const res = await client.post('/documents.info', {
|
|
|
|
id,
|
|
|
|
shareId: options.shareId,
|
|
|
|
});
|
2017-06-28 03:59:53 +00:00
|
|
|
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) {
|
2018-05-13 20:26:06 +00:00
|
|
|
this.errors.add('Failed to load document');
|
2017-07-18 04:46:32 +00:00
|
|
|
} finally {
|
|
|
|
this.isFetching = false;
|
2017-06-28 03:59:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +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
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +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();
|
|
|
|
|
2018-05-13 04:01:17 +00:00
|
|
|
this.errors = options.errors;
|
2017-08-29 15:37:17 +00:00
|
|
|
this.ui = options.ui;
|
2017-07-16 18:47:48 +00:00
|
|
|
|
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-12-28 12:35:59 +00:00
|
|
|
this.on('documents.create', (data: Document) => {
|
|
|
|
this.add(new Document(data));
|
|
|
|
});
|
2017-08-03 13:48:07 +00:00
|
|
|
|
2018-01-29 01:04:53 +00:00
|
|
|
// Re-fetch dashboard content so that we don't show deleted documents
|
|
|
|
this.on('collections.delete', () => {
|
|
|
|
this.fetchRecentlyModified();
|
|
|
|
this.fetchRecentlyViewed();
|
|
|
|
});
|
|
|
|
this.on('documents.delete', () => {
|
|
|
|
this.fetchRecentlyModified();
|
|
|
|
this.fetchRecentlyViewed();
|
|
|
|
});
|
2017-06-28 03:59:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DocumentsStore;
|