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/UsersStore.js
2019-06-18 23:39:29 -07:00

62 lines
1.4 KiB
JavaScript

// @flow
import { filter, orderBy } from 'lodash';
import { computed, action, runInAction } from 'mobx';
import invariant from 'invariant';
import { client } from 'utils/ApiClient';
import BaseStore from './BaseStore';
import RootStore from './RootStore';
import User from 'models/User';
export default class UsersStore extends BaseStore<User> {
constructor(rootStore: RootStore) {
super(rootStore, User);
}
@computed
get active(): User[] {
return filter(this.orderedData, user => !user.isSuspended);
}
@computed
get admins(): User[] {
return filter(this.orderedData, user => user.isAdmin);
}
@computed
get orderedData(): T[] {
return orderBy(Array.from(this.data.values()), 'name', 'asc');
}
@action
promote = (user: User) => {
return this.actionOnUser('promote', user);
};
@action
demote = (user: User) => {
return this.actionOnUser('demote', user);
};
@action
suspend = (user: User) => {
return this.actionOnUser('suspend', user);
};
@action
activate = (user: User) => {
return this.actionOnUser('activate', user);
};
actionOnUser = async (action: string, user: User) => {
const res = await client.post(`/users.${action}`, {
id: user.id,
});
invariant(res && res.data, 'Data should be available');
const { data } = res;
runInAction(`UsersStore#${action}`, () => {
this.add(data);
});
};
}