reducers.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { createSlice, PayloadAction } from '@reduxjs/toolkit';
  2. import { processAclItems } from 'app/core/utils/acl';
  3. import { DashboardAclDTO, FolderDTO, FolderState } from 'app/types';
  4. export const initialState: FolderState = {
  5. id: 0,
  6. uid: 'loading',
  7. title: 'loading',
  8. url: '',
  9. canSave: false,
  10. canDelete: false,
  11. hasChanged: false,
  12. version: 1,
  13. permissions: [],
  14. canViewFolderPermissions: false,
  15. };
  16. const folderSlice = createSlice({
  17. name: 'folder',
  18. initialState,
  19. reducers: {
  20. loadFolder: (state, action: PayloadAction<FolderDTO>): FolderState => {
  21. return {
  22. ...state,
  23. ...action.payload,
  24. hasChanged: false,
  25. };
  26. },
  27. setFolderTitle: (state, action: PayloadAction<string>): FolderState => {
  28. return {
  29. ...state,
  30. title: action.payload,
  31. hasChanged: action.payload.trim().length > 0,
  32. };
  33. },
  34. loadFolderPermissions: (state, action: PayloadAction<DashboardAclDTO[]>): FolderState => {
  35. return {
  36. ...state,
  37. permissions: processAclItems(action.payload),
  38. };
  39. },
  40. setCanViewFolderPermissions: (state, action: PayloadAction<boolean>): FolderState => {
  41. state.canViewFolderPermissions = action.payload;
  42. return state;
  43. },
  44. },
  45. });
  46. export const { loadFolderPermissions, loadFolder, setFolderTitle, setCanViewFolderPermissions } = folderSlice.actions;
  47. export const folderReducer = folderSlice.reducer;
  48. export default {
  49. folder: folderReducer,
  50. };