2016-08-01 16:03:17 +00:00
|
|
|
import { observable, action, runInAction } from 'mobx';
|
|
|
|
import { client, cacheResponse } from 'utils/ApiClient';
|
2016-06-05 01:28:14 +00:00
|
|
|
|
2017-02-10 04:21:37 +00:00
|
|
|
class DashboardStore {
|
2016-08-05 15:09:14 +00:00
|
|
|
@observable collections;
|
2016-06-05 01:28:14 +00:00
|
|
|
@observable pagination;
|
|
|
|
|
2016-06-05 01:58:42 +00:00
|
|
|
@observable isFetching = true;
|
2016-06-05 01:28:14 +00:00
|
|
|
|
|
|
|
/* Actions */
|
|
|
|
|
2017-02-10 04:21:37 +00:00
|
|
|
@action fetchCollections = async () => {
|
2016-06-05 01:28:14 +00:00
|
|
|
this.isFetching = true;
|
|
|
|
|
|
|
|
try {
|
2017-02-10 04:21:37 +00:00
|
|
|
const res = await client.post('/collections.list', { id: this.team.id });
|
2016-06-05 01:28:14 +00:00
|
|
|
const { data, pagination } = res;
|
2016-08-05 15:09:14 +00:00
|
|
|
runInAction('fetchCollections', () => {
|
|
|
|
this.collections = data;
|
2016-08-01 16:03:17 +00:00
|
|
|
this.pagination = pagination;
|
2017-04-28 04:48:13 +00:00
|
|
|
data.forEach(collection => cacheResponse(collection.recentDocuments));
|
2016-08-01 16:03:17 +00:00
|
|
|
});
|
2017-02-10 04:21:37 +00:00
|
|
|
|
|
|
|
// If only one collection, visit it automatically
|
|
|
|
if (this.collections.length === 1) {
|
|
|
|
this.router.push(this.collections[0].url);
|
|
|
|
}
|
2016-06-05 01:28:14 +00:00
|
|
|
} catch (e) {
|
2017-02-10 04:21:37 +00:00
|
|
|
console.error('Something went wrong');
|
2016-06-05 01:28:14 +00:00
|
|
|
}
|
|
|
|
this.isFetching = false;
|
2017-04-28 04:48:13 +00:00
|
|
|
};
|
2016-06-05 01:28:14 +00:00
|
|
|
|
2017-02-10 04:21:37 +00:00
|
|
|
constructor(options) {
|
|
|
|
this.team = options.team;
|
|
|
|
this.router = options.router;
|
|
|
|
this.fetchCollections();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DashboardStore;
|