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/app/stores/SharesStore.js
2018-05-23 22:09:14 -07:00

52 lines
1.3 KiB
JavaScript

// @flow
import _ from 'lodash';
import { observable, action, runInAction, ObservableMap, computed } from 'mobx';
import invariant from 'invariant';
import { client } from 'utils/ApiClient';
import type { Share, PaginationParams } from 'types';
class SharesStore {
@observable data: Map<string, Share> = new ObservableMap([]);
@observable isFetching: boolean = false;
@observable isSaving: boolean = false;
@computed
get orderedData(): Share[] {
return _.sortBy(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.delete', { id: share.id });
runInAction('revoke', () => {
this.data.delete(share.id);
});
} catch (e) {
console.error('Something went wrong');
}
};
}
export default SharesStore;