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/DocumentsStore.js

242 lines
5.9 KiB
JavaScript
Raw Normal View History

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';
import BaseStore from 'stores/BaseStore';
2017-06-28 03:59:53 +00:00
import Document from 'models/Document';
import ErrorsStore from 'stores/ErrorsStore';
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 = {
ui: UiStore,
2017-07-16 18:54:23 +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;
@observable isFetching: boolean = false;
2017-07-18 05:24:19 +00:00
2017-06-28 03:59:53 +00:00
errors: ErrorsStore;
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
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
}
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(
_.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
get starred(): Document[] {
2017-07-09 18:26:17 +00:00
return _.filter(this.data.values(), 'starred');
}
@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 {
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
fetchPage = async (
2017-12-04 00:50:50 +00:00
request: string = 'list',
options: ?PaginationParams
): Promise<*> => {
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#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');
} 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<*> => {
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<*> => {
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
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
};
@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,
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) => {
2017-10-20 07:27:01 +00:00
if (!this.getById(id)) this.fetch(id, true);
};
2017-11-10 22:14:30 +00:00
@action
fetch = async (id: string, prefetch?: boolean): Promise<*> => {
2017-10-20 07:27:01 +00:00
if (!prefetch) 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;
const document = new Document(data);
2017-06-28 03:59:53 +00:00
runInAction('DocumentsStore#fetch', () => {
this.data.set(data.id, document);
2017-06-28 03:59:53 +00:00
this.isLoaded = true;
});
return document;
2017-06-28 03:59:53 +00:00
} catch (e) {
this.errors.add('Failed to load documents');
} 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
};
/**
* Match documents by the url ID as the title slug can change
*/
getByUrl = (url: string): ?Document => {
return _.find(this.data.values(), doc => url.endsWith(doc.urlId));
};
2017-07-16 18:47:48 +00:00
constructor(options: Options) {
super();
2018-05-13 04:01:17 +00:00
this.errors = options.errors;
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 }) => {
this.remove(data.id);
});
2017-12-28 12:35:59 +00:00
this.on('documents.create', (data: Document) => {
this.add(new Document(data));
});
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;