admin.ts 3.79 KB
Newer Older
Maxym Borodenko's avatar
Maxym Borodenko committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import {UserService} from 'service/UserService';
import FilteredPage, {IPageRequest} from 'model/FilteredPage';
import {User} from 'model/user/User';
import {ADMIN_APPEND_USERS, ADMIN_RECEIVE_USER, ADMIN_RECEIVE_USERS} from 'user/ui/constants';
import {showSnackbar} from 'actions/snackbar';
import navigateTo from 'actions/navigation';
import {log} from 'utilities/debug';
import * as _ from 'lodash';
import UserFilter from 'model/UserFilter';

const receiveUsers = (paged: FilteredPage<User>, error = null) => ({
  type: ADMIN_RECEIVE_USERS,
  payload: { paged, error },
});

const appendUsers = (paged: FilteredPage<User>, error = null) => ({
  type: ADMIN_APPEND_USERS,
  payload: { paged, error },
});

const receiveUser = (user: User, error = null) => ({
  type: ADMIN_RECEIVE_USER,
  payload: { user, error },
});

export const loadUser = (uuid: string) => (dispatch) => {
  return UserService.getUser(uuid)
    .then((user) => {
      dispatch(receiveUser(user));
    })
    .catch((error) => {
      dispatch(receiveUser(null, error));
    });
};

export const enableAccount = (uuid: string, enable: boolean = true) => (dispatch) => {
  const updateAccountStatus = enable ? UserService.enableAccount : UserService.disableAccount;

  return updateAccountStatus(uuid)
    .then((user) => {
      dispatch(receiveUser(user));
      dispatch(showSnackbar(`Account is ${enable ? 'enabled' : 'disabled'}`));
    })
    .catch((error) => {
      const data = _.get(error, 'response.data');
      log('Error', data.error);
      dispatch(showSnackbar(data.error || 'Error...'));
    });
};

export const lockAccount = (uuid: string, lock: boolean = true) => (dispatch) => {
  const updateAccountStatus = lock ? UserService.lockAccount : UserService.unlockAccount;

  return updateAccountStatus(uuid)
    .then((user) => {
      dispatch(receiveUser(user));
      dispatch(showSnackbar(`Account is ${lock ? 'locked' : 'unlocked'}`));
    })
    .catch((error) => {
      const data = _.get(error, 'response.data');
      log('Error', data.error);
      dispatch(showSnackbar(data.error || 'Error...'));
    });
};

export const deleteAccount = (uuid: string) => (dispatch) => {
  return UserService.archiveAccount(uuid)
    .then((user) => {
      dispatch(receiveUser(user));
      dispatch(showSnackbar(`Account is deleted`));
    })
    .catch((error) => {
      const data = _.get(error, 'response.data');
      log('Error', data.error);
      dispatch(showSnackbar(data.error || 'Error...'));
    });
};

export const sendVerificationEmail = (uuid: string) => (dispatch) => {
  return UserService.sendEmail(uuid)
    .then(() => {
      dispatch(showSnackbar(`Email is sent`));
    })
    .catch((error) => {
      const data = _.get(error, 'response.data');
      log('Error', data.error);
      dispatch(showSnackbar(data.error || 'Error...'));
    });
};

export const updateUser = (user: User) => (dispatch) => {
  return UserService.updateUser(user)
    .then((updatedUser) => {
      dispatch(receiveUser(updatedUser));
      dispatch(navigateTo(`/admin/users/${updatedUser.uuid}`));
    })
    .catch((error) => {
      dispatch(receiveUser(null, error));
    });
};

export const applyFilters = (filters: string | UserFilter, page: IPageRequest = { page: 0 }) => (dispatch) => {
  console.log('Applying new filter', filters, page);
  return UserService.listUsers(filters, page)
    .then((paged) => {
      if (paged.number === 0) {
        dispatch(receiveUsers(paged));
      } else {
        dispatch(appendUsers(paged));
      }
      dispatch(updateRoute(paged));
    }).catch((error) => {
      console.log(`API error`, error);
      dispatch(receiveUsers(null, error));
    });
};

export const updateRoute = (paged: FilteredPage<User>) => (dispatch) => {
  dispatch(navigateTo(paged.filterCode ? `/admin/users/${paged.filterCode}` : '/admin/users'));
};