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/app/stores/UsersStore.js
2018-05-24 23:07:44 -07:00

64 lines
1.5 KiB
JavaScript

// @flow
import { observable, action, runInAction } from 'mobx';
import invariant from 'invariant';
import { client } from 'utils/ApiClient';
import type { User, PaginationParams } from 'types';
class UsersStore {
@observable data: User[] = [];
@observable isSaving: boolean = false;
@action
fetchPage = async (options: ?PaginationParams): Promise<*> => {
try {
const res = await client.post('/team.users', options);
invariant(res && res.data, 'Data should be available');
const { data } = res;
runInAction('fetchUsers', () => {
this.data = data.reverse();
});
} catch (e) {
console.error('Something went wrong');
}
};
@action
promote = async (user: User) => {
return this.actionOnUser('promote', user);
};
@action
demote = async (user: User) => {
return this.actionOnUser('demote', user);
};
@action
suspend = async (user: User) => {
return this.actionOnUser('suspend', user);
};
@action
activate = async (user: User) => {
return this.actionOnUser('activate', user);
};
actionOnUser = async (action: string, user: User) => {
try {
const res = await client.post(`/user.${action}`, {
id: user.id,
});
invariant(res && res.data, 'Data should be available');
const { data } = res;
runInAction(`UsersStore#${action}`, () => {
this.data = this.data.map(user => (user.id === data.id ? data : user));
});
} catch (e) {
console.error('Something went wrong');
}
};
}
export default UsersStore;