Files
outline/app/stores/CollectionsStore.js
Tom Moor 8cbcb77486 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

* 👕
2018-12-04 22:24:30 -08:00

93 lines
2.4 KiB
JavaScript

// @flow
import { computed, runInAction } from 'mobx';
import { concat, last } from 'lodash';
import { client } from 'utils/ApiClient';
import BaseStore from './BaseStore';
import RootStore from './RootStore';
import Collection from '../models/Collection';
import naturalSort from 'shared/utils/naturalSort';
export type DocumentPathItem = {
id: string,
title: string,
url: string,
type: 'document' | 'collection',
};
export type DocumentPath = DocumentPathItem & {
path: DocumentPathItem[],
};
export default class CollectionsStore extends BaseStore<Collection> {
constructor(rootStore: RootStore) {
super(rootStore, Collection);
}
@computed
get active(): ?Collection {
return this.rootStore.ui.activeCollectionId
? this.data.get(this.rootStore.ui.activeCollectionId)
: undefined;
}
@computed
get orderedData(): Collection[] {
return naturalSort(Array.from(this.data.values()), 'name');
}
/**
* List of paths to each of the documents, where paths are composed of id and title/name pairs
*/
@computed
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]));
});
if (this.isLoaded) {
this.data.forEach(collection => {
const { id, name, url } = collection;
const node = { id, title: name, url, type: 'collection' };
results.push([node]);
travelDocuments(collection.documents, [node]);
});
}
return results.map(result => {
const tail = last(result);
return {
...tail,
path: result,
};
});
}
getPathForDocument(documentId: string): ?DocumentPath {
return this.pathsToDocuments.find(path => path.id === documentId);
}
titleForDocument(documentUrl: string): ?string {
const path = this.pathsToDocuments.find(path => path.url === documentUrl);
if (path) return path.title;
}
delete(collection: Collection) {
super.delete(collection);
runInAction(() => {
this.rootStore.documents.fetchRecentlyUpdated();
this.rootStore.documents.fetchRecentlyViewed();
});
}
export = () => {
return client.post('/collections.exportAll');
};
}