Base model refactor (#810)

* Big upgrades

* WIP: Stash

* Stash, 30 flow errors left

* Downgrade mobx

* WIP

* When I understand the difference between class and instance methods

* 💚

* Fixes: File import
Model saving edge cases
pinning and starring docs
Collection editing
Upgrade mobx devtools

* Notification settings saving works

* Disabled settings

* Document mailer

* Working notifications

* Colletion created notification
Ensure not notified for own actions

* Tidy up

* Document updated event only for document creation
Add indexes
Notification setting on user creation

* Commentary

* Fixed: Notification setting on signup

* Fix document move / duplicate stale data
Add BaseModel.refresh method

* Fixes: Title in sidebar not updated after editing document

* 💚

* Improve / restore error handling
Better handle offline errors

* 👕
This commit is contained in:
Tom Moor
2018-12-04 22:24:30 -08:00
committed by GitHub
parent 67cd250316
commit 8cbcb77486
222 changed files with 2273 additions and 2361 deletions

View File

@@ -1,60 +1,12 @@
// @flow
import { observable, action, runInAction } from 'mobx';
import invariant from 'invariant';
import { client } from 'utils/ApiClient';
import type { ApiKey, PaginationParams } from 'types';
import BaseStore from './BaseStore';
import RootStore from './RootStore';
import ApiKey from 'models/ApiKey';
class ApiKeysStore {
@observable data: ApiKey[] = [];
@observable isFetching: boolean = false;
@observable isSaving: boolean = false;
export default class ApiKeysStore extends BaseStore<ApiKey> {
actions = ['list', 'create', 'delete'];
@action
fetchPage = async (options: ?PaginationParams): Promise<*> => {
this.isFetching = true;
try {
const res = await client.post('/apiKeys.list', options);
invariant(res && res.data, 'Data should be available');
const { data } = res;
runInAction('fetchApiKeys', () => {
this.data = data;
});
} catch (e) {
console.error('Something went wrong');
}
this.isFetching = false;
};
@action
createApiKey = async (name: string) => {
this.isSaving = true;
try {
const res = await client.post('/apiKeys.create', { name });
invariant(res && res.data, 'Data should be available');
const { data } = res;
runInAction('createApiKey', () => {
this.data.push(data);
});
} catch (e) {
console.error('Something went wrong');
}
this.isSaving = false;
};
@action
deleteApiKey = async (id: string) => {
try {
await client.post('/apiKeys.delete', { id });
runInAction('deleteApiKey', () => {
this.fetchPage();
});
} catch (e) {
console.error('Something went wrong');
}
};
constructor(rootStore: RootStore) {
super(rootStore, ApiKey);
}
}
export default ApiKeysStore;

View File

@@ -4,20 +4,45 @@ import invariant from 'invariant';
import Cookie from 'js-cookie';
import { client } from 'utils/ApiClient';
import { stripSubdomain } from 'shared/utils/domains';
import type { User, Team } from 'types';
import RootStore from 'stores/RootStore';
import User from 'models/User';
import Team from 'models/Team';
const AUTH_STORE = 'AUTH_STORE';
class AuthStore {
export default class AuthStore {
@observable user: ?User;
@observable team: ?Team;
@observable token: ?string;
@observable isSaving: boolean = false;
@observable isLoading: boolean = false;
@observable isSuspended: boolean = false;
@observable suspendedContactEmail: ?string;
rootStore: RootStore;
/* Computed */
constructor(rootStore: RootStore) {
// Rehydrate
let data = {};
try {
data = JSON.parse(localStorage.getItem(AUTH_STORE) || '{}');
} catch (_) {
// no-op Safari private mode
}
this.rootStore = rootStore;
this.user = data.user;
this.team = data.team;
this.token = Cookie.get('accessToken');
if (this.token) setImmediate(() => this.fetch());
autorun(() => {
try {
localStorage.setItem(AUTH_STORE, this.asJson);
} catch (_) {
// no-op Safari private mode
}
});
}
@computed
get authenticated(): boolean {
@@ -39,8 +64,18 @@ class AuthStore {
invariant(res && res.data, 'Auth not available');
runInAction('AuthStore#fetch', () => {
this.user = res.data.user;
this.team = res.data.team;
const { user, team } = res.data;
this.user = user;
this.team = team;
if (window.Bugsnag) {
Bugsnag.user = {
id: user.id,
name: user.name,
teamId: team.id,
team: team.name,
};
}
});
} catch (err) {
if (err.error.error === 'user_suspended') {
@@ -52,7 +87,7 @@ class AuthStore {
@action
deleteUser = async () => {
await client.post(`/user.delete`, { confirmation: true });
await client.post(`/users.delete`, { confirmation: true });
runInAction('AuthStore#updateUser', () => {
this.user = null;
@@ -66,7 +101,7 @@ class AuthStore {
this.isSaving = true;
try {
const res = await client.post(`/user.update`, params);
const res = await client.post(`/users.update`, params);
invariant(res && res.data, 'User response not available');
runInAction('AuthStore#updateUser', () => {
@@ -120,29 +155,4 @@ class AuthStore {
// add a timestamp to force reload from server
window.location.href = `${BASE_URL}?done=${new Date().getTime()}`;
};
constructor() {
// Rehydrate
let data = {};
try {
data = JSON.parse(localStorage.getItem(AUTH_STORE) || '{}');
} catch (_) {
// no-op Safari private mode
}
this.user = data.user;
this.team = data.team;
this.token = Cookie.get('accessToken');
if (this.token) setImmediate(() => this.fetch());
autorun(() => {
try {
localStorage.setItem(AUTH_STORE, this.asJson);
} catch (_) {
// no-op Safari private mode
}
});
}
}
export default AuthStore;

View File

@@ -1,19 +1,162 @@
// @flow
import { EventEmitter } from 'fbemitter';
import _ from 'lodash';
import invariant from 'invariant';
import { observable, set, action, computed, runInAction } from 'mobx';
import { orderBy } from 'lodash';
import { client } from 'utils/ApiClient';
import RootStore from 'stores/RootStore';
import BaseModel from '../models/BaseModel';
import type { PaginationParams } from 'types';
const emitter = new EventEmitter();
window.__emitter = emitter;
type Action = 'list' | 'info' | 'create' | 'update' | 'delete';
class BaseStore {
emitter: EventEmitter;
on: (eventName: string, callback: Function) => void;
emit: (eventName: string, data: any) => void;
constructor() {
_.extend(this, emitter);
this.on = emitter.addListener;
}
function modelNameFromClassName(string) {
return string.charAt(0).toLowerCase() + string.slice(1);
}
export default BaseStore;
export const DEFAULT_PAGINATION_LIMIT = 25;
export default class BaseStore<T: BaseModel> {
@observable data: Map<string, T> = new Map();
@observable isFetching: boolean = false;
@observable isSaving: boolean = false;
@observable isLoaded: boolean = false;
model: Class<T>;
modelName: string;
rootStore: RootStore;
actions: Action[] = ['list', 'info', 'create', 'update', 'delete'];
constructor(rootStore: RootStore, model: Class<T>) {
this.rootStore = rootStore;
this.model = model;
this.modelName = modelNameFromClassName(model.name);
}
@action
clear() {
this.data.clear();
}
@action
add = (item: Object): T => {
const Model = this.model;
if (!(item instanceof Model)) {
const existing: ?T = this.data.get(item.id);
if (existing) {
set(existing, item);
return existing;
} else {
item = new Model(item, this);
}
}
this.data.set(item.id, item);
return item;
};
@action
remove(id: string): void {
this.data.delete(id);
}
save(params: Object) {
if (params.id) return this.update(params);
return this.create(params);
}
@action
async create(params: Object) {
if (!this.actions.includes('create')) {
throw new Error(`Cannot create ${this.modelName}`);
}
this.isSaving = true;
try {
const res = await client.post(`/${this.modelName}s.create`, params);
invariant(res && res.data, 'Data should be available');
return this.add(res.data);
} finally {
this.isSaving = false;
}
}
@action
async update(params: Object): * {
if (!this.actions.includes('update')) {
throw new Error(`Cannot update ${this.modelName}`);
}
this.isSaving = true;
try {
const res = await client.post(`/${this.modelName}s.update`, params);
invariant(res && res.data, 'Data should be available');
return this.add(res.data);
} finally {
this.isSaving = false;
}
}
@action
async delete(item: T) {
if (!this.actions.includes('delete')) {
throw new Error(`Cannot delete ${this.modelName}`);
}
this.isSaving = true;
try {
await client.post(`/${this.modelName}s.delete`, { id: item.id });
return this.remove(item.id);
} finally {
this.isSaving = false;
}
}
@action
async fetch(id: string, options?: Object = {}): Promise<*> {
if (!this.actions.includes('info')) {
throw new Error(`Cannot fetch ${this.modelName}`);
}
let item = this.data.get(id);
if (item && !options.force) return item;
this.isFetching = true;
try {
const res = await client.post(`/${this.modelName}s.info`, { id });
invariant(res && res.data, 'Data should be available');
return this.add(res.data);
} finally {
this.isFetching = false;
}
}
@action
async fetchPage(params: ?PaginationParams): Promise<*> {
if (!this.actions.includes('list')) {
throw new Error(`Cannot list ${this.modelName}`);
}
this.isFetching = true;
try {
const res = await client.post(`/${this.modelName}s.list`, params);
invariant(res && res.data, 'Data not available');
runInAction(`list#${this.modelName}`, () => {
res.data.forEach(this.add);
this.isLoaded = true;
});
} finally {
this.isFetching = false;
}
}
@computed
get orderedData(): T[] {
// $FlowIssue
return orderBy(Array.from(this.data.values()), 'createdAt', 'desc');
}
}

View File

@@ -1,20 +1,14 @@
// @flow
import { observable, computed, action, runInAction, ObservableMap } from 'mobx';
import { computed, runInAction } from 'mobx';
import { concat, last } from 'lodash';
import { client } from 'utils/ApiClient';
import _ from 'lodash';
import invariant from 'invariant';
import BaseStore from './BaseStore';
import UiStore from './UiStore';
import Collection from 'models/Collection';
import RootStore from './RootStore';
import Collection from '../models/Collection';
import naturalSort from 'shared/utils/naturalSort';
import type { PaginationParams } from 'types';
type Options = {
ui: UiStore,
};
type DocumentPathItem = {
export type DocumentPathItem = {
id: string,
title: string,
url: string,
@@ -25,17 +19,15 @@ export type DocumentPath = DocumentPathItem & {
path: DocumentPathItem[],
};
class CollectionsStore extends BaseStore {
@observable data: Map<string, Collection> = new ObservableMap([]);
@observable isLoaded: boolean = false;
@observable isFetching: boolean = false;
ui: UiStore;
export default class CollectionsStore extends BaseStore<Collection> {
constructor(rootStore: RootStore) {
super(rootStore, Collection);
}
@computed
get active(): ?Collection {
return this.ui.activeCollectionId
? this.getById(this.ui.activeCollectionId)
return this.rootStore.ui.activeCollectionId
? this.data.get(this.rootStore.ui.activeCollectionId)
: undefined;
}
@@ -48,14 +40,14 @@ class CollectionsStore extends BaseStore {
* List of paths to each of the documents, where paths are composed of id and title/name pairs
*/
@computed
get pathsToDocuments(): Array<DocumentPath> {
get pathsToDocuments(): DocumentPath[] {
let results = [];
const travelDocuments = (documentList, path) =>
documentList.forEach(document => {
const { id, title, url } = document;
const node = { id, title, url, type: 'document' };
results.push(_.concat(path, node));
travelDocuments(document.children, _.concat(path, [node]));
results.push(concat(path, node));
travelDocuments(document.children, concat(path, [node]));
});
if (this.isLoaded) {
@@ -68,7 +60,7 @@ class CollectionsStore extends BaseStore {
}
return results.map(result => {
const tail = _.last(result);
const tail = last(result);
return {
...tail,
path: result,
@@ -85,90 +77,16 @@ class CollectionsStore extends BaseStore {
if (path) return path.title;
}
/* Actions */
delete(collection: Collection) {
super.delete(collection);
@action
fetchPage = async (options: ?PaginationParams): Promise<*> => {
this.isFetching = true;
try {
const res = await client.post('/collections.list', options);
invariant(res && res.data, 'Collection list not available');
const { data } = res;
runInAction('CollectionsStore#fetchPage', () => {
data.forEach(collection => {
this.data.set(collection.id, new Collection(collection));
});
this.isLoaded = true;
});
return res;
} catch (e) {
this.ui.showToast('Failed to load collections');
} finally {
this.isFetching = false;
}
};
@action
fetch = async (id: string): Promise<?Collection> => {
let collection = this.getById(id);
if (collection) return collection;
this.isFetching = true;
try {
const res = await client.post('/collections.info', {
id,
});
invariant(res && res.data, 'Collection not available');
const { data } = res;
const collection = new Collection(data);
runInAction('CollectionsStore#fetch', () => {
this.data.set(data.id, collection);
this.isLoaded = true;
});
return collection;
} catch (e) {
this.ui.showToast('Something went wrong');
} finally {
this.isFetching = false;
}
};
@action
export = async () => {
try {
await client.post('/collections.exportAll');
return true;
} catch (err) {
throw err;
}
};
@action
add = (collection: Collection): void => {
this.data.set(collection.id, collection);
};
@action
remove = (id: string): void => {
this.data.delete(id);
};
getById = (id: string): ?Collection => {
return this.data.get(id);
};
constructor(options: Options) {
super();
this.ui = options.ui;
this.on('collections.delete', (data: { id: string }) => {
this.remove(data.id);
runInAction(() => {
this.rootStore.documents.fetchRecentlyUpdated();
this.rootStore.documents.fetchRecentlyViewed();
});
}
}
export default CollectionsStore;
export = () => {
return client.post('/collections.exportAll');
};
}

View File

@@ -1,55 +1,48 @@
// @flow
import { observable, action, computed, ObservableMap, runInAction } from 'mobx';
import { observable, action, computed, runInAction } from 'mobx';
import { without, map, find, orderBy, filter, compact, uniq } from 'lodash';
import { client } from 'utils/ApiClient';
import { map, find, orderBy, filter, compact, uniq, sortBy } from 'lodash';
import naturalSort from 'shared/utils/naturalSort';
import invariant from 'invariant';
import BaseStore from 'stores/BaseStore';
import Document from 'models/Document';
import UiStore from 'stores/UiStore';
import type { PaginationParams, SearchResult } from 'types';
import RootStore from 'stores/RootStore';
import Document from '../models/Document';
import Revision from '../models/Revision';
import type { FetchOptions, PaginationParams, SearchResult } from 'types';
export const DEFAULT_PAGINATION_LIMIT = 25;
type Options = {
ui: UiStore,
};
type FetchOptions = {
prefetch?: boolean,
shareId?: string,
};
class DocumentsStore extends BaseStore {
export default class DocumentsStore extends BaseStore<Document> {
@observable recentlyViewedIds: string[] = [];
@observable recentlyUpdatedIds: string[] = [];
@observable data: Map<string, Document> = new ObservableMap([]);
@observable isLoaded: boolean = false;
@observable isFetching: boolean = false;
ui: UiStore;
constructor(rootStore: RootStore) {
super(rootStore, Document);
}
@computed
get recentlyViewed(): Document[] {
get recentlyViewed(): * {
return orderBy(
compact(this.recentlyViewedIds.map(id => this.getById(id))),
compact(this.recentlyViewedIds.map(id => this.data.get(id))),
'updatedAt',
'desc'
);
}
@computed
get recentlyUpdated(): Document[] {
get recentlyUpdated(): * {
return orderBy(
compact(this.recentlyUpdatedIds.map(id => this.getById(id))),
compact(this.recentlyUpdatedIds.map(id => this.data.get(id))),
'updatedAt',
'desc'
);
}
createdByUser(userId: string): Document[] {
createdByUser(userId: string): * {
return orderBy(
filter(this.data.values(), document => document.createdBy.id === userId),
filter(
Array.from(this.data.values()),
document => document.createdBy.id === userId
),
'updatedAt',
'desc'
);
@@ -65,7 +58,7 @@ class DocumentsStore extends BaseStore {
recentlyUpdatedInCollection(collectionId: string): Document[] {
return orderBy(
filter(
this.data.values(),
Array.from(this.data.values()),
document =>
document.collectionId === collectionId && !!document.publishedAt
),
@@ -76,33 +69,31 @@ class DocumentsStore extends BaseStore {
@computed
get starred(): Document[] {
return filter(this.data.values(), 'starred');
return filter(this.orderedData, d => d.starred);
}
@computed
get starredAlphabetical(): Document[] {
return sortBy(this.starred, doc => doc.title.toLowerCase());
return naturalSort(this.starred, 'title');
}
@computed
get drafts(): Document[] {
return filter(
orderBy(this.data.values(), 'updatedAt', 'desc'),
orderBy(Array.from(this.data.values()), 'updatedAt', 'desc'),
doc => !doc.publishedAt
);
}
@computed
get active(): ?Document {
return this.ui.activeDocumentId
? this.getById(this.ui.activeDocumentId)
return this.rootStore.ui.activeDocumentId
? this.data.get(this.rootStore.ui.activeDocumentId)
: undefined;
}
/* Actions */
@action
fetchPage = async (
fetchNamedPage = async (
request: string = 'list',
options: ?PaginationParams
): Promise<?(Document[])> => {
@@ -112,15 +103,11 @@ class DocumentsStore extends BaseStore {
const res = await client.post(`/documents.${request}`, options);
invariant(res && res.data, 'Document list not available');
const { data } = res;
runInAction('DocumentsStore#fetchPage', () => {
data.forEach(document => {
this.data.set(document.id, new Document(document));
});
runInAction('DocumentsStore#fetchNamedPage', () => {
data.forEach(this.add);
this.isLoaded = true;
});
return data;
} catch (e) {
this.ui.showToast('Failed to load documents');
} finally {
this.isFetching = false;
}
@@ -128,7 +115,7 @@ class DocumentsStore extends BaseStore {
@action
fetchRecentlyUpdated = async (options: ?PaginationParams): Promise<*> => {
const data = await this.fetchPage('list', options);
const data = await this.fetchNamedPage('list', options);
runInAction('DocumentsStore#fetchRecentlyUpdated', () => {
// $FlowFixMe
@@ -141,7 +128,7 @@ class DocumentsStore extends BaseStore {
@action
fetchRecentlyViewed = async (options: ?PaginationParams): Promise<*> => {
const data = await this.fetchPage('viewed', options);
const data = await this.fetchNamedPage('viewed', options);
runInAction('DocumentsStore#fetchRecentlyViewed', () => {
// $FlowFixMe
@@ -154,22 +141,22 @@ class DocumentsStore extends BaseStore {
@action
fetchStarred = (options: ?PaginationParams): Promise<*> => {
return this.fetchPage('starred', options);
return this.fetchNamedPage('starred', options);
};
@action
fetchDrafts = (options: ?PaginationParams): Promise<*> => {
return this.fetchPage('drafts', options);
return this.fetchNamedPage('drafts', options);
};
@action
fetchPinned = (options: ?PaginationParams): Promise<*> => {
return this.fetchPage('pinned', options);
return this.fetchNamedPage('pinned', options);
};
@action
fetchOwned = (options: ?PaginationParams): Promise<*> => {
return this.fetchPage('list', options);
return this.fetchNamedPage('list', options);
};
@action
@@ -183,23 +170,26 @@ class DocumentsStore extends BaseStore {
});
invariant(res && res.data, 'Search API response should be available');
const { data } = res;
data.forEach(result => this.add(new Document(result.document)));
data.forEach(result => this.add(result.document));
return data;
};
@action
prefetchDocument = async (id: string) => {
if (!this.getById(id)) {
this.fetch(id, { prefetch: true });
prefetchDocument = (id: string) => {
if (!this.data.get(id)) {
return this.fetch(id, { prefetch: true });
}
};
@action
fetch = async (id: string, options?: FetchOptions = {}): Promise<*> => {
fetch = async (
id: string,
options?: FetchOptions = {}
): Promise<?Document> => {
if (!options.prefetch) this.isFetching = true;
try {
const doc = this.getById(id) || this.getByUrl(id);
const doc: ?Document = this.data.get(id) || this.getByUrl(id);
if (doc) return doc;
const res = await client.post('/documents.info', {
@@ -207,24 +197,32 @@ class DocumentsStore extends BaseStore {
shareId: options.shareId,
});
invariant(res && res.data, 'Document not available');
const { data } = res;
const document = new Document(data);
this.add(res.data);
runInAction('DocumentsStore#fetch', () => {
this.data.set(data.id, document);
this.isLoaded = true;
});
return document;
} catch (_err) {
if (!options.prefetch && navigator.onLine) {
this.ui.showToast('Failed to load document');
}
return this.data.get(res.data.id);
} finally {
this.isFetching = false;
}
};
@action
move = async (document: Document, parentDocumentId: ?string) => {
const res = await client.post('/documents.move', {
id: document.id,
parentDocument: parentDocumentId,
});
invariant(res && res.data, 'Data not available');
const collection = this.getCollectionForDocument(document);
if (collection) collection.refresh();
return this.add(res.data);
};
@action
duplicate = async (document: Document): * => {
const res = await client.post('/documents.create', {
@@ -234,61 +232,69 @@ class DocumentsStore extends BaseStore {
title: `${document.title} (duplicate)`,
text: document.text,
});
invariant(res && res.data, 'Data should be available');
if (res && res.data) {
const duped = res.data;
this.emit('documents.create', new Document(duped));
this.emit('documents.publish', {
id: duped.id,
collectionId: duped.collection.id,
});
return duped;
}
const collection = this.getCollectionForDocument(document);
if (collection) collection.refresh();
return this.add(res.data);
};
async update(params: *) {
const document = await super.update(params);
// Because the collection object contains the url and title
// we need to ensure they are updated there as well.
const collection = this.getCollectionForDocument(document);
if (collection) collection.updateDocument(document);
return document;
}
async delete(document: Document) {
await super.delete(document);
runInAction(() => {
this.recentlyViewedIds = without(this.recentlyViewedIds, document.id);
this.recentlyUpdatedIds = without(this.recentlyUpdatedIds, document.id);
});
const collection = this.getCollectionForDocument(document);
if (collection) collection.refresh();
}
@action
add = (document: Document): void => {
this.data.set(document.id, document);
restore = async (document: Document, revision: Revision) => {
const res = await client.post('/documents.restore', {
id: document.id,
revisionId: revision.id,
});
runInAction('Document#restore', () => {
invariant(res && res.data, 'Data should be available');
document.updateFromJson(res.data);
});
};
@action
remove = (id: string): void => {
this.data.delete(id);
pin = (document: Document) => {
return client.post('/documents.pin', { id: document.id });
};
getById = (id: string): ?Document => {
return this.data.get(id);
unpin = (document: Document) => {
return client.post('/documents.unpin', { id: document.id });
};
star = (document: Document) => {
return client.post('/documents.star', { id: document.id });
};
unstar = (document: Document) => {
return client.post('/documents.unstar', { id: document.id });
};
/**
* 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));
return find(Array.from(this.data.values()), doc => url.endsWith(doc.urlId));
};
constructor(options: Options) {
super();
this.ui = options.ui;
this.on('documents.delete', (data: { id: string }) => {
this.remove(data.id);
});
this.on('documents.create', (data: Document) => {
this.add(data);
});
// Re-fetch dashboard content so that we don't show deleted documents
this.on('collections.delete', () => {
this.fetchRecentlyUpdated();
this.fetchRecentlyViewed();
});
this.on('documents.delete', () => {
this.fetchRecentlyUpdated();
this.fetchRecentlyViewed();
});
getCollectionForDocument(document: Document) {
return this.rootStore.collections.data.get(document.collectionId);
}
}
export default DocumentsStore;

View File

@@ -1,74 +1,25 @@
// @flow
import { observable, computed, action, runInAction, ObservableMap } from 'mobx';
import { client } from 'utils/ApiClient';
import _ from 'lodash';
import invariant from 'invariant';
import UiStore from './UiStore';
import BaseStore from './BaseStore';
import { computed } from 'mobx';
import { filter } from 'lodash';
import naturalSort from 'shared/utils/naturalSort';
import BaseStore from 'stores/BaseStore';
import RootStore from 'stores/RootStore';
import Integration from 'models/Integration';
import type { PaginationParams } from 'types';
class IntegrationsStore extends BaseStore {
@observable data: Map<string, Integration> = new ObservableMap([]);
@observable isLoaded: boolean = false;
@observable isFetching: boolean = false;
ui: UiStore;
class IntegrationsStore extends BaseStore<Integration> {
constructor(rootStore: RootStore) {
super(rootStore, Integration);
}
@computed
get orderedData(): Integration[] {
return _.sortBy(this.data.values(), 'name');
return naturalSort(Array.from(this.data.values()), 'name');
}
@computed
get slackIntegrations(): Integration[] {
return _.filter(this.orderedData, { service: 'slack' });
}
@action
fetchPage = async (options: ?PaginationParams): Promise<*> => {
this.isFetching = true;
try {
const res = await client.post('/integrations.list', options);
invariant(res && res.data, 'Integrations list not available');
const { data } = res;
runInAction('IntegrationsStore#fetchPage', () => {
data.forEach(integration => {
this.data.set(integration.id, new Integration(integration));
});
this.isLoaded = true;
});
return res;
} catch (e) {
this.ui.showToast('Failed to load integrations');
} finally {
this.isFetching = false;
}
};
@action
add = (data: Integration): void => {
this.data.set(data.id, data);
};
@action
remove = (id: string): void => {
this.data.delete(id);
};
getById = (id: string): ?Integration => {
return this.data.get(id);
};
constructor(options: { ui: UiStore }) {
super();
this.ui = options.ui;
this.on('integrations.delete', (data: { id: string }) => {
this.remove(data.id);
});
return filter(this.orderedData, { service: 'slack' });
}
}

View File

@@ -0,0 +1,19 @@
// @flow
import { find } from 'lodash';
import NotificationSetting from 'models/NotificationSetting';
import BaseStore from './BaseStore';
import RootStore from './RootStore';
export default class NotificationSettingsStore extends BaseStore<
NotificationSetting
> {
actions = ['list', 'create', 'delete'];
constructor(rootStore: RootStore) {
super(rootStore, NotificationSetting);
}
getByEvent = (event: string) => {
return find(this.orderedData, { event });
};
}

View File

@@ -1,22 +1,18 @@
// @flow
import { observable, computed, action, runInAction, ObservableMap } from 'mobx';
import { client } from 'utils/ApiClient';
import { orderBy, filter } from 'lodash';
import { action, runInAction } from 'mobx';
import { filter } from 'lodash';
import invariant from 'invariant';
import BaseStore from './BaseStore';
import UiStore from './UiStore';
import type { Revision, PaginationParams } from 'types';
import { client } from 'utils/ApiClient';
import BaseStore from 'stores/BaseStore';
import RootStore from 'stores/RootStore';
import Revision from 'models/Revision';
import type { FetchOptions, PaginationParams } from 'types';
class RevisionsStore extends BaseStore {
@observable data: Map<string, Revision> = new ObservableMap([]);
@observable isLoaded: boolean = false;
@observable isFetching: boolean = false;
export default class RevisionsStore extends BaseStore<Revision> {
actions = ['list'];
ui: UiStore;
@computed
get orderedData(): Revision[] {
return orderBy(this.data.values(), 'createdAt', 'desc');
constructor(rootStore: RootStore) {
super(rootStore, Revision);
}
getDocumentRevisions(documentId: string): Revision[] {
@@ -24,11 +20,16 @@ class RevisionsStore extends BaseStore {
}
@action
fetch = async (documentId: string, id: string): Promise<*> => {
fetch = async (
documentId: string,
options?: FetchOptions
): Promise<?Revision> => {
this.isFetching = true;
const id = options && options.revisionId;
if (!id) throw new Error('revisionId is required');
try {
const rev = this.getById(id);
const rev = this.data.get(id);
if (rev) return rev;
const res = await client.post('/documents.revision', {
@@ -44,8 +45,6 @@ class RevisionsStore extends BaseStore {
});
return data;
} catch (e) {
this.ui.showToast('Failed to load document revision');
} finally {
this.isFetching = false;
}
@@ -58,39 +57,14 @@ class RevisionsStore extends BaseStore {
try {
const res = await client.post('/documents.revisions', options);
invariant(res && res.data, 'Document revisions not available');
const { data } = res;
runInAction('RevisionsStore#fetchPage', () => {
data.forEach(revision => {
res.data.forEach(revision => {
this.data.set(revision.id, revision);
});
this.isLoaded = true;
});
return data;
} catch (e) {
this.ui.showToast('Failed to load document revisions');
} finally {
this.isFetching = false;
}
};
@action
add = (data: Revision): void => {
this.data.set(data.id, data);
};
@action
remove = (id: string): void => {
this.data.delete(id);
};
getById = (id: string): ?Revision => {
return this.data.get(id);
};
constructor(options: { ui: UiStore }) {
super();
this.ui = options.ui;
}
}
export default RevisionsStore;

48
app/stores/RootStore.js Normal file
View File

@@ -0,0 +1,48 @@
// @flow
import ApiKeysStore from './ApiKeysStore';
import AuthStore from './AuthStore';
import CollectionsStore from './CollectionsStore';
import DocumentsStore from './DocumentsStore';
import IntegrationsStore from './IntegrationsStore';
import NotificationSettingsStore from './NotificationSettingsStore';
import RevisionsStore from './RevisionsStore';
import SharesStore from './SharesStore';
import UiStore from './UiStore';
import UsersStore from './UsersStore';
export default class RootStore {
apiKeys: ApiKeysStore;
auth: AuthStore;
collections: CollectionsStore;
documents: DocumentsStore;
integrations: IntegrationsStore;
notificationSettings: NotificationSettingsStore;
revisions: RevisionsStore;
shares: SharesStore;
ui: UiStore;
users: UsersStore;
constructor() {
this.apiKeys = new ApiKeysStore(this);
this.auth = new AuthStore(this);
this.collections = new CollectionsStore(this);
this.documents = new DocumentsStore(this);
this.integrations = new IntegrationsStore(this);
this.notificationSettings = new NotificationSettingsStore(this);
this.revisions = new RevisionsStore(this);
this.shares = new SharesStore(this);
this.ui = new UiStore();
this.users = new UsersStore(this);
}
logout() {
this.apiKeys.clear();
this.collections.clear();
this.documents.clear();
this.integrations.clear();
this.notificationSettings.clear();
this.revisions.clear();
this.shares.clear();
this.users.clear();
}
}

View File

@@ -1,51 +1,26 @@
// @flow
import _ from 'lodash';
import { observable, action, runInAction, ObservableMap, computed } from 'mobx';
import invariant from 'invariant';
import { sortBy } from 'lodash';
import { action, computed } from 'mobx';
import { client } from 'utils/ApiClient';
import type { Share, PaginationParams } from 'types';
import BaseStore from './BaseStore';
import RootStore from './RootStore';
import Share from 'models/Share';
class SharesStore {
@observable data: Map<string, Share> = new ObservableMap([]);
@observable isFetching: boolean = false;
@observable isSaving: boolean = false;
export default class SharesStore extends BaseStore<Share> {
actions = ['list', 'create'];
constructor(rootStore: RootStore) {
super(rootStore, Share);
}
@computed
get orderedData(): Share[] {
return _.sortBy(this.data.values(), 'createdAt').reverse();
return sortBy(Array.from(this.data.values()), 'createdAt').reverse();
}
@action
fetchPage = async (options: ?PaginationParams): Promise<*> => {
this.isFetching = true;
try {
const res = await client.post('/shares.list', options);
invariant(res && res.data, 'Data should be available');
const { data } = res;
runInAction('fetchShares', () => {
data.forEach(share => {
this.data.set(share.id, share);
});
});
} catch (e) {
console.error('Something went wrong');
}
this.isFetching = false;
};
@action
revoke = async (share: Share) => {
try {
await client.post('/shares.revoke', { id: share.id });
runInAction('revoke', () => {
this.data.delete(share.id);
});
} catch (e) {
console.error('Something went wrong');
}
await client.post('/shares.revoke', { id: share.id });
this.remove(share.id);
};
}
export default SharesStore;

View File

@@ -12,9 +12,8 @@ class UiStore {
@observable progressBarVisible: boolean = false;
@observable editMode: boolean = false;
@observable mobileSidebarVisible: boolean = false;
@observable toasts: Toast[] = observable.array([]);
@observable toasts: Toast[] = [];
/* Actions */
@action
setActiveModal = (name: string, props: ?Object): void => {
this.activeModalName = name;
@@ -85,7 +84,7 @@ class UiStore {
@action
showToast = (
message: string,
type?: 'warning' | 'error' | 'info' | 'success' = 'warning'
type?: 'warning' | 'error' | 'info' | 'success' = 'success'
): void => {
this.toasts.push({ message, type });
};

View File

@@ -1,73 +1,56 @@
// @flow
import { observable, computed, action, runInAction } from 'mobx';
import { filter } from 'lodash';
import { computed, action, runInAction } from 'mobx';
import invariant from 'invariant';
import { client } from 'utils/ApiClient';
import type { User, PaginationParams } from 'types';
import BaseStore from './BaseStore';
import RootStore from './RootStore';
import User from 'models/User';
class UsersStore {
@observable data: User[] = [];
@observable isSaving: boolean = false;
export default class UsersStore extends BaseStore<User> {
constructor(rootStore: RootStore) {
super(rootStore, User);
}
@computed
get active(): User[] {
return this.data.filter(user => !user.isSuspended);
return filter(this.orderedData, user => !user.isSuspended);
}
@computed
get admins(): User[] {
return this.data.filter(user => user.isAdmin);
return filter(this.orderedData, user => user.isAdmin);
}
@action
fetchPage = async (options: ?PaginationParams): Promise<*> => {
try {
const res = await client.post('/team.users', options);
invariant(res && res.data, 'Data should be available');
const { data } = res;
runInAction('fetchUsers', () => {
this.data = data.reverse();
});
} catch (e) {
console.error('Something went wrong');
}
};
@action
promote = async (user: User) => {
promote = (user: User) => {
return this.actionOnUser('promote', user);
};
@action
demote = async (user: User) => {
demote = (user: User) => {
return this.actionOnUser('demote', user);
};
@action
suspend = async (user: User) => {
suspend = (user: User) => {
return this.actionOnUser('suspend', user);
};
@action
activate = async (user: User) => {
activate = (user: User) => {
return this.actionOnUser('activate', user);
};
actionOnUser = async (action: string, user: User) => {
try {
const res = await client.post(`/user.${action}`, {
id: user.id,
});
invariant(res && res.data, 'Data should be available');
const { data } = res;
const res = await client.post(`/users.${action}`, {
id: user.id,
});
invariant(res && res.data, 'Data should be available');
const { data } = res;
runInAction(`UsersStore#${action}`, () => {
this.data = this.data.map(user => (user.id === data.id ? data : user));
});
} catch (e) {
console.error('Something went wrong');
}
runInAction(`UsersStore#${action}`, () => {
this.add(data);
});
};
}
export default UsersStore;

View File

@@ -1,18 +1,6 @@
// @flow
import AuthStore from './AuthStore';
import UiStore from './UiStore';
import DocumentsStore from './DocumentsStore';
import RevisionsStore from './RevisionsStore';
import SharesStore from './SharesStore';
import RootStore from 'stores/RootStore';
const ui = new UiStore();
const stores = {
user: null, // Including for Layout
auth: new AuthStore(),
ui,
documents: new DocumentsStore({ ui }),
revisions: new RevisionsStore({ ui }),
shares: new SharesStore(),
};
const stores = new RootStore();
export default stores;