mirror of
https://github.com/grafana/grafana.git
synced 2025-02-20 11:48:34 -06:00
* Refactor: Adds Redux Toolkit package * Refactor: Uses configureStore from Redux Toolkit * Refactor: Migrates applicationReducer * Refactor: Migrates appNotificationsReducer * Refactor: Migrates locationReducer * Refactor: Migrates navModelReducer * Refactor: Migrates teamsReducer and teamReducer * Refactor: Migrates cleanUpAction * Refactor: Migrates alertRulesReducer * Refactor: Cleans up recursiveCleanState * Refactor: Switched to Angular compatible reducers * Refactor: Migrates folderReducer * Refactor: Migrates dashboardReducer * Migrates panelEditorReducer * Refactor: Migrates dataSourcesReducer * Refactor: Migrates usersReducer * Refactor: Migrates organizationReducer * Refactor: Migrates pluginsReducer * Refactor: Migrates ldapReducer and ldapUserReducer * Refactor: Migrates apiKeysReducer * Refactor: Migrates exploreReducer and itemReducer * Refactor: Removes actionCreatorFactory and reducerFactory * Refactor: Moves mocks to test section * Docs: Removes sections about home grown framework * Update contribute/style-guides/redux.md Co-Authored-By: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> * Refactor: Cleans up some code * Refactor: Adds state typings * Refactor: Cleans up typings * Refactor: Adds comment about ImmerJs autoFreeze Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>
122 lines
3.6 KiB
TypeScript
122 lines
3.6 KiB
TypeScript
import { AppEvents } from '@grafana/data';
|
|
|
|
import { getBackendSrv } from 'app/core/services/backend_srv';
|
|
import { FolderState, ThunkResult } from 'app/types';
|
|
import { DashboardAcl, DashboardAclUpdateDTO, NewDashboardAclItem, PermissionLevel } from 'app/types/acl';
|
|
|
|
import { updateLocation, updateNavIndex } from 'app/core/actions';
|
|
import { buildNavModel } from './navModel';
|
|
import appEvents from 'app/core/app_events';
|
|
import { loadFolder, loadFolderPermissions } from './reducers';
|
|
|
|
export function getFolderByUid(uid: string): ThunkResult<void> {
|
|
return async dispatch => {
|
|
const folder = await getBackendSrv().getFolderByUid(uid);
|
|
dispatch(loadFolder(folder));
|
|
dispatch(updateNavIndex(buildNavModel(folder)));
|
|
};
|
|
}
|
|
|
|
export function saveFolder(folder: FolderState): ThunkResult<void> {
|
|
return async dispatch => {
|
|
const res = await getBackendSrv().put(`/api/folders/${folder.uid}`, {
|
|
title: folder.title,
|
|
version: folder.version,
|
|
});
|
|
|
|
// this should be redux action at some point
|
|
appEvents.emit(AppEvents.alertSuccess, ['Folder saved']);
|
|
|
|
dispatch(updateLocation({ path: `${res.url}/settings` }));
|
|
};
|
|
}
|
|
|
|
export function deleteFolder(uid: string): ThunkResult<void> {
|
|
return async dispatch => {
|
|
await getBackendSrv().deleteFolder(uid, true);
|
|
dispatch(updateLocation({ path: `dashboards` }));
|
|
};
|
|
}
|
|
|
|
export function getFolderPermissions(uid: string): ThunkResult<void> {
|
|
return async dispatch => {
|
|
const permissions = await getBackendSrv().get(`/api/folders/${uid}/permissions`);
|
|
dispatch(loadFolderPermissions(permissions));
|
|
};
|
|
}
|
|
|
|
function toUpdateItem(item: DashboardAcl): DashboardAclUpdateDTO {
|
|
return {
|
|
userId: item.userId,
|
|
teamId: item.teamId,
|
|
role: item.role,
|
|
permission: item.permission,
|
|
};
|
|
}
|
|
|
|
export function updateFolderPermission(itemToUpdate: DashboardAcl, level: PermissionLevel): ThunkResult<void> {
|
|
return async (dispatch, getStore) => {
|
|
const folder = getStore().folder;
|
|
const itemsToUpdate = [];
|
|
|
|
for (const item of folder.permissions) {
|
|
if (item.inherited) {
|
|
continue;
|
|
}
|
|
|
|
const updated = toUpdateItem(item);
|
|
|
|
// if this is the item we want to update, update it's permission
|
|
if (itemToUpdate === item) {
|
|
updated.permission = level;
|
|
}
|
|
|
|
itemsToUpdate.push(updated);
|
|
}
|
|
|
|
await getBackendSrv().post(`/api/folders/${folder.uid}/permissions`, { items: itemsToUpdate });
|
|
await dispatch(getFolderPermissions(folder.uid));
|
|
};
|
|
}
|
|
|
|
export function removeFolderPermission(itemToDelete: DashboardAcl): ThunkResult<void> {
|
|
return async (dispatch, getStore) => {
|
|
const folder = getStore().folder;
|
|
const itemsToUpdate = [];
|
|
|
|
for (const item of folder.permissions) {
|
|
if (item.inherited || item === itemToDelete) {
|
|
continue;
|
|
}
|
|
itemsToUpdate.push(toUpdateItem(item));
|
|
}
|
|
|
|
await getBackendSrv().post(`/api/folders/${folder.uid}/permissions`, { items: itemsToUpdate });
|
|
await dispatch(getFolderPermissions(folder.uid));
|
|
};
|
|
}
|
|
|
|
export function addFolderPermission(newItem: NewDashboardAclItem): ThunkResult<void> {
|
|
return async (dispatch, getStore) => {
|
|
const folder = getStore().folder;
|
|
const itemsToUpdate = [];
|
|
|
|
for (const item of folder.permissions) {
|
|
if (item.inherited) {
|
|
continue;
|
|
}
|
|
itemsToUpdate.push(toUpdateItem(item));
|
|
}
|
|
|
|
itemsToUpdate.push({
|
|
userId: newItem.userId,
|
|
teamId: newItem.teamId,
|
|
role: newItem.role,
|
|
permission: newItem.permission,
|
|
});
|
|
|
|
await getBackendSrv().post(`/api/folders/${folder.uid}/permissions`, { items: itemsToUpdate });
|
|
await dispatch(getFolderPermissions(folder.uid));
|
|
};
|
|
}
|