actions.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { TimeZone } from '@grafana/data';
  2. import { getBackendSrv } from '@grafana/runtime';
  3. import { notifyApp } from 'app/core/actions';
  4. import { createSuccessNotification } from 'app/core/copy/appNotification';
  5. import { updateTimeZoneForSession, updateWeekStartForSession } from 'app/features/profile/state/reducers';
  6. import { DashboardAcl, DashboardAclUpdateDTO, NewDashboardAclItem, PermissionLevel, ThunkResult } from 'app/types';
  7. import { loadPluginDashboards } from '../../plugins/admin/state/actions';
  8. import { cancelVariables } from '../../variables/state/actions';
  9. import { getTimeSrv } from '../services/TimeSrv';
  10. import { cleanUpDashboard, loadDashboardPermissions } from './reducers';
  11. export function getDashboardPermissions(id: number): ThunkResult<void> {
  12. return async (dispatch) => {
  13. const permissions = await getBackendSrv().get(`/api/dashboards/id/${id}/permissions`);
  14. dispatch(loadDashboardPermissions(permissions));
  15. };
  16. }
  17. function toUpdateItem(item: DashboardAcl): DashboardAclUpdateDTO {
  18. return {
  19. userId: item.userId,
  20. teamId: item.teamId,
  21. role: item.role,
  22. permission: item.permission,
  23. };
  24. }
  25. export function updateDashboardPermission(
  26. dashboardId: number,
  27. itemToUpdate: DashboardAcl,
  28. level: PermissionLevel
  29. ): ThunkResult<void> {
  30. return async (dispatch, getStore) => {
  31. const { dashboard } = getStore();
  32. const itemsToUpdate = [];
  33. for (const item of dashboard.permissions) {
  34. if (item.inherited) {
  35. continue;
  36. }
  37. const updated = toUpdateItem(item);
  38. // if this is the item we want to update, update it's permission
  39. if (itemToUpdate === item) {
  40. updated.permission = level;
  41. }
  42. itemsToUpdate.push(updated);
  43. }
  44. await getBackendSrv().post(`/api/dashboards/id/${dashboardId}/permissions`, { items: itemsToUpdate });
  45. await dispatch(getDashboardPermissions(dashboardId));
  46. };
  47. }
  48. export function removeDashboardPermission(dashboardId: number, itemToDelete: DashboardAcl): ThunkResult<void> {
  49. return async (dispatch, getStore) => {
  50. const dashboard = getStore().dashboard;
  51. const itemsToUpdate = [];
  52. for (const item of dashboard.permissions) {
  53. if (item.inherited || item === itemToDelete) {
  54. continue;
  55. }
  56. itemsToUpdate.push(toUpdateItem(item));
  57. }
  58. await getBackendSrv().post(`/api/dashboards/id/${dashboardId}/permissions`, { items: itemsToUpdate });
  59. await dispatch(getDashboardPermissions(dashboardId));
  60. };
  61. }
  62. export function addDashboardPermission(dashboardId: number, newItem: NewDashboardAclItem): ThunkResult<void> {
  63. return async (dispatch, getStore) => {
  64. const { dashboard } = getStore();
  65. const itemsToUpdate = [];
  66. for (const item of dashboard.permissions) {
  67. if (item.inherited) {
  68. continue;
  69. }
  70. itemsToUpdate.push(toUpdateItem(item));
  71. }
  72. itemsToUpdate.push({
  73. userId: newItem.userId,
  74. teamId: newItem.teamId,
  75. role: newItem.role,
  76. permission: newItem.permission,
  77. });
  78. await getBackendSrv().post(`/api/dashboards/id/${dashboardId}/permissions`, { items: itemsToUpdate });
  79. await dispatch(getDashboardPermissions(dashboardId));
  80. };
  81. }
  82. export function importDashboard(data: any, dashboardTitle: string): ThunkResult<void> {
  83. return async (dispatch) => {
  84. await getBackendSrv().post('/api/dashboards/import', data);
  85. dispatch(notifyApp(createSuccessNotification('Dashboard Imported', dashboardTitle)));
  86. dispatch(loadPluginDashboards());
  87. };
  88. }
  89. export function removeDashboard(uid: string): ThunkResult<void> {
  90. return async (dispatch) => {
  91. await getBackendSrv().delete(`/api/dashboards/uid/${uid}`);
  92. dispatch(loadPluginDashboards());
  93. };
  94. }
  95. export const cleanUpDashboardAndVariables = (): ThunkResult<void> => (dispatch, getStore) => {
  96. const store = getStore();
  97. const dashboard = store.dashboard.getModel();
  98. if (dashboard) {
  99. dashboard.destroy();
  100. dispatch(cancelVariables(dashboard.uid));
  101. }
  102. getTimeSrv().stopAutoRefresh();
  103. dispatch(cleanUpDashboard());
  104. };
  105. export const updateTimeZoneDashboard =
  106. (timeZone: TimeZone): ThunkResult<void> =>
  107. (dispatch) => {
  108. dispatch(updateTimeZoneForSession(timeZone));
  109. getTimeSrv().refreshTimeModel();
  110. };
  111. export const updateWeekStartDashboard =
  112. (weekStart: string): ThunkResult<void> =>
  113. (dispatch) => {
  114. dispatch(updateWeekStartForSession(weekStart));
  115. getTimeSrv().refreshTimeModel();
  116. };