actions.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { getBackendSrv } from '@grafana/runtime';
  2. import { updateNavIndex } from 'app/core/actions';
  3. import { contextSrv } from 'app/core/core';
  4. import { accessControlQueryParam } from 'app/core/utils/accessControl';
  5. import { AccessControlAction, TeamMember, ThunkResult } from 'app/types';
  6. import { buildNavModel } from './navModel';
  7. import { teamGroupsLoaded, teamLoaded, teamMembersLoaded, teamsLoaded } from './reducers';
  8. export function loadTeams(): ThunkResult<void> {
  9. return async (dispatch) => {
  10. // Early return if the user cannot list teams
  11. if (!contextSrv.hasPermission(AccessControlAction.ActionTeamsRead)) {
  12. dispatch(teamsLoaded([]));
  13. return;
  14. }
  15. const response = await getBackendSrv().get(
  16. '/api/teams/search',
  17. accessControlQueryParam({ perpage: 1000, page: 1 })
  18. );
  19. dispatch(teamsLoaded(response.teams));
  20. };
  21. }
  22. export function loadTeam(id: number): ThunkResult<void> {
  23. return async (dispatch) => {
  24. const response = await getBackendSrv().get(`/api/teams/${id}`, accessControlQueryParam());
  25. dispatch(teamLoaded(response));
  26. dispatch(updateNavIndex(buildNavModel(response)));
  27. };
  28. }
  29. export function loadTeamMembers(): ThunkResult<void> {
  30. return async (dispatch, getStore) => {
  31. const team = getStore().team.team;
  32. const response = await getBackendSrv().get(`/api/teams/${team.id}/members`);
  33. dispatch(teamMembersLoaded(response));
  34. };
  35. }
  36. export function addTeamMember(id: number): ThunkResult<void> {
  37. return async (dispatch, getStore) => {
  38. const team = getStore().team.team;
  39. await getBackendSrv().post(`/api/teams/${team.id}/members`, { userId: id });
  40. dispatch(loadTeamMembers());
  41. };
  42. }
  43. export function removeTeamMember(id: number): ThunkResult<void> {
  44. return async (dispatch, getStore) => {
  45. const team = getStore().team.team;
  46. await getBackendSrv().delete(`/api/teams/${team.id}/members/${id}`);
  47. dispatch(loadTeamMembers());
  48. };
  49. }
  50. export function updateTeam(name: string, email: string): ThunkResult<void> {
  51. return async (dispatch, getStore) => {
  52. const team = getStore().team.team;
  53. await getBackendSrv().put(`/api/teams/${team.id}`, { name, email });
  54. dispatch(loadTeam(team.id));
  55. };
  56. }
  57. export function loadTeamGroups(): ThunkResult<void> {
  58. return async (dispatch, getStore) => {
  59. const team = getStore().team.team;
  60. const response = await getBackendSrv().get(`/api/teams/${team.id}/groups`);
  61. dispatch(teamGroupsLoaded(response));
  62. };
  63. }
  64. export function addTeamGroup(groupId: string): ThunkResult<void> {
  65. return async (dispatch, getStore) => {
  66. const team = getStore().team.team;
  67. await getBackendSrv().post(`/api/teams/${team.id}/groups`, { groupId: groupId });
  68. dispatch(loadTeamGroups());
  69. };
  70. }
  71. export function removeTeamGroup(groupId: string): ThunkResult<void> {
  72. return async (dispatch, getStore) => {
  73. const team = getStore().team.team;
  74. await getBackendSrv().delete(`/api/teams/${team.id}/groups/${encodeURIComponent(groupId)}`);
  75. dispatch(loadTeamGroups());
  76. };
  77. }
  78. export function deleteTeam(id: number): ThunkResult<void> {
  79. return async (dispatch) => {
  80. await getBackendSrv().delete(`/api/teams/${id}`);
  81. // Update users permissions in case they lost teams.read with the deletion
  82. await contextSrv.fetchUserPermissions();
  83. dispatch(loadTeams());
  84. };
  85. }
  86. export function updateTeamMember(member: TeamMember): ThunkResult<void> {
  87. return async (dispatch) => {
  88. await getBackendSrv().put(`/api/teams/${member.teamId}/members/${member.userId}`, {
  89. permission: member.permission,
  90. });
  91. dispatch(loadTeamMembers());
  92. };
  93. }