2023-01-09 02:54:33 -06:00
|
|
|
import { debounce } from 'lodash';
|
|
|
|
|
2019-06-03 10:55:59 -05:00
|
|
|
import { getBackendSrv } from '@grafana/runtime';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { accessControlQueryParam } from 'app/core/utils/accessControl';
|
2020-01-13 01:03:22 -06:00
|
|
|
import { OrgUser } from 'app/types';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
|
|
|
import { ThunkResult } from '../../../types';
|
|
|
|
|
2023-01-09 02:54:33 -06:00
|
|
|
import { usersLoaded, pageChanged, usersFetchBegin, usersFetchEnd, searchQueryChanged } from './reducers';
|
2018-10-03 02:43:10 -05:00
|
|
|
|
|
|
|
export function loadUsers(): ThunkResult<void> {
|
2023-01-09 02:54:33 -06:00
|
|
|
return async (dispatch, getState) => {
|
|
|
|
try {
|
|
|
|
const { perPage, page, searchQuery } = getState().users;
|
|
|
|
const users = await getBackendSrv().get(
|
|
|
|
`/api/org/users/search`,
|
|
|
|
accessControlQueryParam({ perpage: perPage, page, query: searchQuery })
|
|
|
|
);
|
|
|
|
dispatch(usersLoaded(users));
|
|
|
|
} catch (error) {
|
|
|
|
usersFetchEnd();
|
|
|
|
}
|
2018-10-03 02:43:10 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-09 02:54:33 -06:00
|
|
|
const fetchUsersWithDebounce = debounce((dispatch) => dispatch(loadUsers()), 300);
|
|
|
|
|
2018-10-03 02:43:10 -05:00
|
|
|
export function updateUser(user: OrgUser): ThunkResult<void> {
|
2021-01-20 00:59:48 -06:00
|
|
|
return async (dispatch) => {
|
2018-10-03 03:54:15 -05:00
|
|
|
await getBackendSrv().patch(`/api/org/users/${user.userId}`, { role: user.role });
|
2018-10-03 02:43:10 -05:00
|
|
|
dispatch(loadUsers());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function removeUser(userId: number): ThunkResult<void> {
|
2021-01-20 00:59:48 -06:00
|
|
|
return async (dispatch) => {
|
2018-10-03 02:43:10 -05:00
|
|
|
await getBackendSrv().delete(`/api/org/users/${userId}`);
|
|
|
|
dispatch(loadUsers());
|
|
|
|
};
|
|
|
|
}
|
2023-01-09 02:54:33 -06:00
|
|
|
|
|
|
|
export function changePage(page: number): ThunkResult<void> {
|
|
|
|
return async (dispatch) => {
|
|
|
|
dispatch(usersFetchBegin());
|
|
|
|
dispatch(pageChanged(page));
|
|
|
|
dispatch(loadUsers());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function changeSearchQuery(query: string): ThunkResult<void> {
|
|
|
|
return async (dispatch) => {
|
|
|
|
dispatch(usersFetchBegin());
|
|
|
|
dispatch(searchQueryChanged(query));
|
|
|
|
fetchUsersWithDebounce(dispatch);
|
|
|
|
};
|
|
|
|
}
|