manageDashboards.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { DashboardSection, DashboardSectionItem, SearchAction } from '../types';
  2. import { mergeReducers } from '../utils';
  3. import { TOGGLE_ALL_CHECKED, TOGGLE_CHECKED, MOVE_ITEMS, DELETE_ITEMS } from './actionTypes';
  4. import { dashboardsSearchState, DashboardsSearchState, searchReducer } from './dashboardSearch';
  5. export interface ManageDashboardsState extends DashboardsSearchState {
  6. allChecked: boolean;
  7. }
  8. export const manageDashboardsState: ManageDashboardsState = {
  9. ...dashboardsSearchState,
  10. allChecked: false,
  11. };
  12. const reducer = (state: ManageDashboardsState, action: SearchAction) => {
  13. switch (action.type) {
  14. case TOGGLE_ALL_CHECKED:
  15. const newAllChecked = !state.allChecked;
  16. return {
  17. ...state,
  18. results: state.results.map((result) => {
  19. return {
  20. ...result,
  21. checked: newAllChecked,
  22. items: result.items.map((item) => ({ ...item, checked: newAllChecked })),
  23. };
  24. }),
  25. allChecked: newAllChecked,
  26. };
  27. case TOGGLE_CHECKED:
  28. const { id } = action.payload;
  29. return {
  30. ...state,
  31. results: state.results.map((result) => {
  32. if (result.id === id) {
  33. return {
  34. ...result,
  35. checked: !result.checked,
  36. items: result.items.map((item) => ({ ...item, checked: !result.checked })),
  37. };
  38. }
  39. return {
  40. ...result,
  41. items: result.items.map((item) => (item.id === id ? { ...item, checked: !item.checked } : item)),
  42. };
  43. }),
  44. };
  45. case MOVE_ITEMS: {
  46. const dashboards: DashboardSectionItem[] = action.payload.dashboards;
  47. const folder: DashboardSection = action.payload.folder;
  48. const uids = dashboards.map((db) => db.uid);
  49. return {
  50. ...state,
  51. results: state.results.map((result) => {
  52. if (folder.id === result.id) {
  53. return result.expanded
  54. ? {
  55. ...result,
  56. items: [...result.items, ...dashboards.map((db) => ({ ...db, checked: false }))],
  57. checked: false,
  58. }
  59. : result;
  60. } else {
  61. return { ...result, items: result.items.filter((item) => !uids.includes(item.uid)) };
  62. }
  63. }),
  64. };
  65. }
  66. case DELETE_ITEMS: {
  67. const { folders, dashboards } = action.payload;
  68. if (!folders.length && !dashboards.length) {
  69. return state;
  70. }
  71. return {
  72. ...state,
  73. results: state.results.reduce((filtered, result) => {
  74. if (!folders.includes(result.uid)) {
  75. return [...filtered, { ...result, items: result.items.filter((item) => !dashboards.includes(item.uid)) }];
  76. }
  77. return filtered;
  78. }, [] as DashboardSection[]),
  79. };
  80. }
  81. default:
  82. return state;
  83. }
  84. };
  85. export const manageDashboardsReducer = mergeReducers([searchReducer, reducer]);