reducer.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { createAction } from '@reduxjs/toolkit';
  2. import { AnyAction } from 'redux';
  3. import { PanelPluginMeta, SelectableValue } from '@grafana/data';
  4. import { FolderInfo } from '../../../../types';
  5. export interface LibraryPanelsSearchState {
  6. searchQuery: string;
  7. sortDirection?: string;
  8. panelFilter: string[];
  9. folderFilter: string[];
  10. }
  11. export const initialLibraryPanelsSearchState: LibraryPanelsSearchState = {
  12. searchQuery: '',
  13. panelFilter: [],
  14. folderFilter: [],
  15. sortDirection: undefined,
  16. };
  17. export const searchChanged = createAction<string>('libraryPanels/search/searchChanged');
  18. export const sortChanged = createAction<SelectableValue<string>>('libraryPanels/search/sortChanged');
  19. export const panelFilterChanged = createAction<PanelPluginMeta[]>('libraryPanels/search/panelFilterChanged');
  20. export const folderFilterChanged = createAction<FolderInfo[]>('libraryPanels/search/folderFilterChanged');
  21. export const libraryPanelsSearchReducer = (state: LibraryPanelsSearchState, action: AnyAction) => {
  22. if (searchChanged.match(action)) {
  23. return { ...state, searchQuery: action.payload };
  24. }
  25. if (sortChanged.match(action)) {
  26. return { ...state, sortDirection: action.payload.value };
  27. }
  28. if (panelFilterChanged.match(action)) {
  29. return { ...state, panelFilter: action.payload.map((p) => p.id) };
  30. }
  31. if (folderFilterChanged.match(action)) {
  32. return { ...state, folderFilter: action.payload.map((f) => String(f.id!)) };
  33. }
  34. return state;
  35. };