actions.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { ThunkAction } from 'redux-thunk';
  2. import { getBackendSrv } from 'app/core/services/backend_srv';
  3. import { StoreState } from 'app/types';
  4. import { DataSourcePermissionDTO } from '../../types';
  5. export enum ActionTypes {
  6. LoadDataSourcePermissions = 'LOAD_DATA_SOURCE_PERMISSIONS',
  7. }
  8. export interface LoadDataSourcePermissionsAction {
  9. type: ActionTypes.LoadDataSourcePermissions;
  10. payload: DataSourcePermissionDTO;
  11. }
  12. const dataSourcePermissionsLoaded = (
  13. dataSourcePermission: DataSourcePermissionDTO
  14. ): LoadDataSourcePermissionsAction => ({
  15. type: ActionTypes.LoadDataSourcePermissions,
  16. payload: dataSourcePermission,
  17. });
  18. export type Action = LoadDataSourcePermissionsAction;
  19. type ThunkResult<R> = ThunkAction<R, StoreState, undefined, Action>;
  20. export function loadDataSourcePermissions(id: number): ThunkResult<void> {
  21. return async (dispatch) => {
  22. const response = await getBackendSrv().get(`/api/datasources/${id}/permissions`);
  23. dispatch(dataSourcePermissionsLoaded(response));
  24. };
  25. }
  26. export function enableDataSourcePermissions(id: number): ThunkResult<void> {
  27. return async (dispatch) => {
  28. await getBackendSrv().post(`/api/datasources/${id}/enable-permissions`, {});
  29. dispatch(loadDataSourcePermissions(id));
  30. };
  31. }
  32. export function disableDataSourcePermissions(id: number): ThunkResult<void> {
  33. return async (dispatch) => {
  34. await getBackendSrv().post(`/api/datasources/${id}/disable-permissions`, {});
  35. dispatch(loadDataSourcePermissions(id));
  36. };
  37. }
  38. export function addDataSourcePermission(id: number, data: object): ThunkResult<void> {
  39. return async (dispatch) => {
  40. await getBackendSrv().post(`/api/datasources/${id}/permissions`, data);
  41. dispatch(loadDataSourcePermissions(id));
  42. };
  43. }
  44. export function removeDataSourcePermission(id: number, permissionId: number): ThunkResult<void> {
  45. return async (dispatch) => {
  46. await getBackendSrv().delete(`/api/datasources/${id}/permissions/${permissionId}`);
  47. dispatch(loadDataSourcePermissions(id));
  48. };
  49. }