actions.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { ThunkAction } from 'redux-thunk';
  2. import { StoreState } from '../../../types';
  3. import { getBackendSrv } from '../../../core/services/backend_srv';
  4. import { Invitee, OrgUser } from 'app/types';
  5. export enum ActionTypes {
  6. LoadUsers = 'LOAD_USERS',
  7. LoadInvitees = 'LOAD_INVITEES',
  8. SetUsersSearchQuery = 'SET_USERS_SEARCH_QUERY',
  9. }
  10. export interface LoadUsersAction {
  11. type: ActionTypes.LoadUsers;
  12. payload: OrgUser[];
  13. }
  14. export interface LoadInviteesAction {
  15. type: ActionTypes.LoadInvitees;
  16. payload: Invitee[];
  17. }
  18. export interface SetUsersSearchQueryAction {
  19. type: ActionTypes.SetUsersSearchQuery;
  20. payload: string;
  21. }
  22. const usersLoaded = (users: OrgUser[]): LoadUsersAction => ({
  23. type: ActionTypes.LoadUsers,
  24. payload: users,
  25. });
  26. const inviteesLoaded = (invitees: Invitee[]): LoadInviteesAction => ({
  27. type: ActionTypes.LoadInvitees,
  28. payload: invitees,
  29. });
  30. export const setUsersSearchQuery = (query: string): SetUsersSearchQueryAction => ({
  31. type: ActionTypes.SetUsersSearchQuery,
  32. payload: query,
  33. });
  34. export type Action = LoadUsersAction | SetUsersSearchQueryAction | LoadInviteesAction;
  35. type ThunkResult<R> = ThunkAction<R, StoreState, undefined, Action>;
  36. export function loadUsers(): ThunkResult<void> {
  37. return async dispatch => {
  38. const users = await getBackendSrv().get('/api/org/users');
  39. dispatch(usersLoaded(users));
  40. };
  41. }
  42. export function loadInvitees(): ThunkResult<void> {
  43. return async dispatch => {
  44. const invitees = await getBackendSrv().get('/api/org/invites');
  45. dispatch(inviteesLoaded(invitees));
  46. };
  47. }
  48. export function updateUser(user: OrgUser): ThunkResult<void> {
  49. return async dispatch => {
  50. await getBackendSrv().patch(`/api/org/users/${user.userId}`, { role: user.role });
  51. dispatch(loadUsers());
  52. };
  53. }
  54. export function removeUser(userId: number): ThunkResult<void> {
  55. return async dispatch => {
  56. await getBackendSrv().delete(`/api/org/users/${userId}`);
  57. dispatch(loadUsers());
  58. };
  59. }
  60. export function revokeInvite(code: string): ThunkResult<void> {
  61. return async dispatch => {
  62. await getBackendSrv().patch(`/api/org/invites/${code}/revoke`, {});
  63. dispatch(loadInvitees());
  64. };
  65. }