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.
Files
outline/frontend/stores/CollectionsStore.js
2017-05-29 18:04:26 -07:00

60 lines
1.4 KiB
JavaScript

// @flow
import { observable, action, runInAction, ObservableArray } from 'mobx';
import ApiClient, { client } from 'utils/ApiClient';
import _ from 'lodash';
import stores from 'stores';
import Collection from 'models/Collection';
import ErrorsStore from 'stores/ErrorsStore';
type Options = {
teamId: string,
};
class CollectionsStore {
@observable data: ObservableArray<Collection> = observable.array([]);
@observable isLoaded: boolean = false;
client: ApiClient;
teamId: string;
errors: ErrorsStore;
/* Actions */
@action fetch = async (): Promise<*> => {
try {
const res = await this.client.post('/collections.list', {
id: this.teamId,
});
const { data } = res;
runInAction('CollectionsStore#fetch', () => {
this.data.replace(
data.map(
collection =>
new Collection({
...collection,
errors: this.errors,
})
)
);
this.isLoaded = true;
});
} catch (e) {
this.errors.add('Failed to load collections');
}
};
@action getById = async (id: string): Promise<Collection> => {
if (!this.isLoaded) await this.fetch();
return _.find(this.data, { id });
};
constructor(options: Options) {
this.client = client;
this.errors = stores.errors;
this.teamId = options.teamId;
}
}
export default CollectionsStore;