grafana/public/app/features/library-panels/components/LibraryPanelsView/reducer.ts
Josh Hunt 3c6e0e8ef8
Chore: ESlint import order (#44959)
* Add and configure eslint-plugin-import

* Fix the lint:ts npm command

* Autofix + prettier all the files

* Manually fix remaining files

* Move jquery code in jest-setup to external file to safely reorder imports

* Resolve issue caused by circular dependencies within Prometheus

* Update .betterer.results

* Fix missing // @ts-ignore

* ignore iconBundle.ts

* Fix missing // @ts-ignore
2022-04-22 14:33:13 +01:00

60 lines
1.6 KiB
TypeScript

import { createAction } from '@reduxjs/toolkit';
import { AnyAction } from 'redux';
import { LoadingState } from '@grafana/data';
import { LibraryElementDTO } from '../../types';
export interface LibraryPanelsViewState {
loadingState: LoadingState;
libraryPanels: LibraryElementDTO[];
totalCount: number;
perPage: number;
page: number;
numberOfPages: number;
currentPanelId?: string;
}
export const initialLibraryPanelsViewState: LibraryPanelsViewState = {
loadingState: LoadingState.Loading,
libraryPanels: [],
totalCount: 0,
perPage: 40,
page: 1,
numberOfPages: 0,
currentPanelId: undefined,
};
export const initSearch = createAction('libraryPanels/view/initSearch');
export const searchCompleted = createAction<
Omit<LibraryPanelsViewState, 'currentPanelId' | 'searchString' | 'loadingState' | 'numberOfPages'>
>('libraryPanels/view/searchCompleted');
export const changePage = createAction<Pick<LibraryPanelsViewState, 'page'>>('libraryPanels/view/changePage');
export const libraryPanelsViewReducer = (state: LibraryPanelsViewState, action: AnyAction) => {
if (initSearch.match(action)) {
return { ...state, loadingState: LoadingState.Loading };
}
if (searchCompleted.match(action)) {
const { libraryPanels, page, perPage, totalCount } = action.payload;
const numberOfPages = Math.ceil(totalCount / perPage);
return {
...state,
libraryPanels,
perPage,
totalCount,
loadingState: LoadingState.Done,
numberOfPages,
page: page > numberOfPages ? page - 1 : page,
};
}
if (changePage.match(action)) {
return { ...state, page: action.payload.page };
}
return state;
};