2022-11-16 08:55:10 -06:00
|
|
|
import { debounce } from 'lodash';
|
|
|
|
|
2019-06-03 10:55:59 -05:00
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
2020-01-13 01:03:22 -06:00
|
|
|
import { updateNavIndex } from 'app/core/actions';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { contextSrv } from 'app/core/core';
|
|
|
|
import { accessControlQueryParam } from 'app/core/utils/accessControl';
|
|
|
|
import { AccessControlAction, TeamMember, ThunkResult } from 'app/types';
|
|
|
|
|
2020-01-13 01:03:22 -06:00
|
|
|
import { buildNavModel } from './navModel';
|
2022-11-16 08:55:10 -06:00
|
|
|
import { teamGroupsLoaded, queryChanged, pageChanged, teamLoaded, teamMembersLoaded, teamsLoaded } from './reducers';
|
2018-09-11 07:14:03 -05:00
|
|
|
|
2022-11-16 08:55:10 -06:00
|
|
|
export function loadTeams(initial = false): ThunkResult<void> {
|
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const { query, page, perPage } = getState().teams;
|
2022-02-15 07:23:40 -06:00
|
|
|
// Early return if the user cannot list teams
|
|
|
|
if (!contextSrv.hasPermission(AccessControlAction.ActionTeamsRead)) {
|
2022-11-16 08:55:10 -06:00
|
|
|
dispatch(teamsLoaded({ teams: [], totalCount: 0, page: 1, perPage, noTeams: true }));
|
2022-02-15 07:23:40 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:49:39 -06:00
|
|
|
const response = await getBackendSrv().get(
|
|
|
|
'/api/teams/search',
|
2022-11-16 08:55:10 -06:00
|
|
|
accessControlQueryParam({ query, page, perpage: perPage })
|
2022-02-03 10:49:39 -06:00
|
|
|
);
|
2022-11-16 08:55:10 -06:00
|
|
|
|
|
|
|
// We only want to check if there is no teams on the initial request.
|
|
|
|
// A query that returns no teams should not render the empty list banner.
|
|
|
|
let noTeams = false;
|
|
|
|
if (initial) {
|
|
|
|
noTeams = response.teams.length === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(teamsLoaded({ noTeams, ...response }));
|
2018-09-11 07:14:03 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-16 08:55:10 -06:00
|
|
|
const loadTeamsWithDebounce = debounce((dispatch) => dispatch(loadTeams()), 500);
|
|
|
|
|
2018-09-11 07:14:03 -05:00
|
|
|
export function loadTeam(id: number): ThunkResult<void> {
|
2021-01-20 00:59:48 -06:00
|
|
|
return async (dispatch) => {
|
2022-02-03 10:49:39 -06:00
|
|
|
const response = await getBackendSrv().get(`/api/teams/${id}`, accessControlQueryParam());
|
2018-09-13 07:10:51 -05:00
|
|
|
dispatch(teamLoaded(response));
|
|
|
|
dispatch(updateNavIndex(buildNavModel(response)));
|
2018-09-11 07:14:03 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-16 08:55:10 -06:00
|
|
|
export function deleteTeam(id: number): ThunkResult<void> {
|
|
|
|
return async (dispatch) => {
|
|
|
|
await getBackendSrv().delete(`/api/teams/${id}`);
|
|
|
|
// Update users permissions in case they lost teams.read with the deletion
|
|
|
|
await contextSrv.fetchUserPermissions();
|
|
|
|
dispatch(loadTeams());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function changeQuery(query: string): ThunkResult<void> {
|
|
|
|
return async (dispatch) => {
|
|
|
|
dispatch(queryChanged(query));
|
|
|
|
loadTeamsWithDebounce(dispatch);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function changePage(page: number): ThunkResult<void> {
|
|
|
|
return async (dispatch) => {
|
|
|
|
dispatch(pageChanged(page));
|
|
|
|
dispatch(loadTeams());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-11 07:14:03 -05:00
|
|
|
export function loadTeamMembers(): ThunkResult<void> {
|
|
|
|
return async (dispatch, getStore) => {
|
|
|
|
const team = getStore().team.team;
|
2018-09-13 07:10:51 -05:00
|
|
|
const response = await getBackendSrv().get(`/api/teams/${team.id}/members`);
|
|
|
|
dispatch(teamMembersLoaded(response));
|
2018-09-11 07:14:03 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addTeamMember(id: number): ThunkResult<void> {
|
|
|
|
return async (dispatch, getStore) => {
|
|
|
|
const team = getStore().team.team;
|
2018-09-13 07:10:51 -05:00
|
|
|
await getBackendSrv().post(`/api/teams/${team.id}/members`, { userId: id });
|
|
|
|
dispatch(loadTeamMembers());
|
2018-09-11 07:14:03 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeTeamMember(id: number): ThunkResult<void> {
|
|
|
|
return async (dispatch, getStore) => {
|
|
|
|
const team = getStore().team.team;
|
2018-09-13 07:10:51 -05:00
|
|
|
await getBackendSrv().delete(`/api/teams/${team.id}/members/${id}`);
|
|
|
|
dispatch(loadTeamMembers());
|
2018-09-11 07:14:03 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updateTeam(name: string, email: string): ThunkResult<void> {
|
|
|
|
return async (dispatch, getStore) => {
|
|
|
|
const team = getStore().team.team;
|
2018-09-13 07:10:51 -05:00
|
|
|
await getBackendSrv().put(`/api/teams/${team.id}`, { name, email });
|
|
|
|
dispatch(loadTeam(team.id));
|
2018-09-11 07:14:03 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function loadTeamGroups(): ThunkResult<void> {
|
|
|
|
return async (dispatch, getStore) => {
|
|
|
|
const team = getStore().team.team;
|
2018-09-13 07:10:51 -05:00
|
|
|
const response = await getBackendSrv().get(`/api/teams/${team.id}/groups`);
|
|
|
|
dispatch(teamGroupsLoaded(response));
|
2018-09-11 07:14:03 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addTeamGroup(groupId: string): ThunkResult<void> {
|
|
|
|
return async (dispatch, getStore) => {
|
|
|
|
const team = getStore().team.team;
|
2018-09-13 07:10:51 -05:00
|
|
|
await getBackendSrv().post(`/api/teams/${team.id}/groups`, { groupId: groupId });
|
|
|
|
dispatch(loadTeamGroups());
|
2018-09-11 07:14:03 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeTeamGroup(groupId: string): ThunkResult<void> {
|
|
|
|
return async (dispatch, getStore) => {
|
|
|
|
const team = getStore().team.team;
|
2019-11-08 08:28:21 -06:00
|
|
|
await getBackendSrv().delete(`/api/teams/${team.id}/groups/${encodeURIComponent(groupId)}`);
|
2018-09-13 07:10:51 -05:00
|
|
|
dispatch(loadTeamGroups());
|
2018-09-11 07:14:03 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-08 04:48:33 -06:00
|
|
|
export function updateTeamMember(member: TeamMember): ThunkResult<void> {
|
2021-01-20 00:59:48 -06:00
|
|
|
return async (dispatch) => {
|
2019-03-08 04:48:33 -06:00
|
|
|
await getBackendSrv().put(`/api/teams/${member.teamId}/members/${member.userId}`, {
|
|
|
|
permission: member.permission,
|
|
|
|
});
|
|
|
|
dispatch(loadTeamMembers());
|
|
|
|
};
|
|
|
|
}
|