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/frontend/stores/CollectionsStore.js

121 lines
3.0 KiB
JavaScript
Raw Normal View History

// @flow
2017-07-16 18:47:48 +00:00
import {
observable,
computed,
2017-07-16 18:47:48 +00:00
action,
runInAction,
ObservableArray,
autorunAsync,
} from 'mobx';
import ApiClient, { client } from 'utils/ApiClient';
import _ from 'lodash';
2017-05-30 02:08:03 +00:00
import invariant from 'invariant';
import stores from 'stores';
import Collection from 'models/Collection';
import ErrorsStore from 'stores/ErrorsStore';
2017-07-16 18:47:48 +00:00
import CacheStore from 'stores/CacheStore';
import UiStore from 'stores/UiStore';
2017-07-16 18:47:48 +00:00
const COLLECTION_CACHE_KEY = 'COLLECTION_CACHE_KEY';
type Options = {
teamId: string,
2017-07-16 18:47:48 +00:00
cache: CacheStore,
ui: UiStore,
};
class CollectionsStore {
@observable data: ObservableArray<Collection> = observable.array([]);
@observable isLoaded: boolean = false;
client: ApiClient;
teamId: string;
errors: ErrorsStore;
2017-07-16 18:47:48 +00:00
cache: CacheStore;
ui: UiStore;
@computed get active(): ?Collection {
return this.ui.activeCollectionId
? this.getById(this.ui.activeCollectionId)
: undefined;
}
/* Actions */
2017-06-28 03:59:53 +00:00
@action fetchAll = async (): Promise<*> => {
try {
const res = await this.client.post('/collections.list', {
id: this.teamId,
});
2017-05-30 02:08:03 +00:00
invariant(res && res.data, 'Collection list not available');
const { data } = res;
runInAction('CollectionsStore#fetch', () => {
2017-05-30 03:00:08 +00:00
this.data.replace(data.map(collection => new Collection(collection)));
this.isLoaded = true;
});
} catch (e) {
this.errors.add('Failed to load collections');
}
};
@action fetchById = async (id: string): Promise<?Collection> => {
let collection = this.getById(id);
if (!collection) {
try {
const res = await this.client.post('/collections.info', {
id,
});
invariant(res && res.data, 'Collection not available');
const { data } = res;
runInAction('CollectionsStore#getById', () => {
collection = new Collection(data);
this.add(collection);
});
} catch (e) {
2017-07-19 06:36:49 +00:00
Bugsnag.notify(e);
this.errors.add('Something went wrong');
}
}
return collection;
};
2017-07-10 03:02:10 +00:00
@action add = (collection: Collection): void => {
this.data.push(collection);
};
@action remove = (id: string): void => {
this.data.splice(this.data.indexOf(id), 1);
};
getById = (id: string): ?Collection => {
return _.find(this.data, { id });
};
constructor(options: Options) {
this.client = client;
this.errors = stores.errors;
this.teamId = options.teamId;
2017-07-16 18:47:48 +00:00
this.cache = options.cache;
this.ui = options.ui;
//
// this.cache.getItem(COLLECTION_CACHE_KEY).then(data => {
// if (data) {
// this.data.replace(data.map(collection => new Collection(collection)));
// this.isLoaded = true;
// }
// });
2017-07-16 18:47:48 +00:00
autorunAsync('CollectionsStore.persists', () => {
if (this.data.length > 0)
this.cache.setItem(
COLLECTION_CACHE_KEY,
this.data.map(collection => collection.data)
);
});
}
}
export default CollectionsStore;