Members -> Users
This commit is contained in:
65
app/stores/UsersStore.js
Normal file
65
app/stores/UsersStore.js
Normal file
@@ -0,0 +1,65 @@
|
||||
// @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 isLoaded: boolean = false;
|
||||
@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');
|
||||
}
|
||||
this.isLoaded = false;
|
||||
};
|
||||
|
||||
@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;
|
||||
Reference in New Issue
Block a user