grafana/public/app/features/explore/state/history.ts
Piotr Jamróz e7605ad974
Query History: Split data and view models (#44922)
* Remove unused properties

* Fix unit tests

* Fix unit tests

* Split data models

* Simplify updating items in rich history

* Update tests

* Fix starring an item and add a unit test

* Move the converter to a separate file and add unit tests

* Convert a private function to an inline function

* Add more docs and clean up the code

* Update public/app/core/history/localStorageConverter.ts

Co-authored-by: Giordano Ricci <me@giordanoricci.com>

* Update public/app/core/utils/richHistory.test.ts

Co-authored-by: Giordano Ricci <me@giordanoricci.com>

* Use template literals over explicit casting

* Split updateRichHistory to three separate functions

Co-authored-by: Giordano Ricci <me@giordanoricci.com>
2022-02-10 13:59:43 +01:00

63 lines
2.0 KiB
TypeScript

import {
deleteAllFromRichHistory,
deleteQueryInRichHistory,
updateCommentInRichHistory,
updateStarredInRichHistory,
} from 'app/core/utils/richHistory';
import { ExploreId, ExploreItemState, ThunkResult } from 'app/types';
import { richHistoryUpdatedAction } from './main';
import { HistoryItem } from '@grafana/data';
import { AnyAction, createAction } from '@reduxjs/toolkit';
//
// Actions and Payloads
//
export interface HistoryUpdatedPayload {
exploreId: ExploreId;
history: HistoryItem[];
}
export const historyUpdatedAction = createAction<HistoryUpdatedPayload>('explore/historyUpdated');
//
// Action creators
//
export const starHistoryItem = (id: string, starred: boolean): ThunkResult<void> => {
return async (dispatch, getState) => {
const nextRichHistory = await updateStarredInRichHistory(getState().explore.richHistory, id, starred);
dispatch(richHistoryUpdatedAction({ richHistory: nextRichHistory }));
};
};
export const commentHistoryItem = (id: string, comment?: string): ThunkResult<void> => {
return async (dispatch, getState) => {
const nextRichHistory = await updateCommentInRichHistory(getState().explore.richHistory, id, comment);
dispatch(richHistoryUpdatedAction({ richHistory: nextRichHistory }));
};
};
export const deleteHistoryItem = (id: string): ThunkResult<void> => {
return async (dispatch, getState) => {
const nextRichHistory = await deleteQueryInRichHistory(getState().explore.richHistory, id);
dispatch(richHistoryUpdatedAction({ richHistory: nextRichHistory }));
};
};
export const deleteRichHistory = (): ThunkResult<void> => {
return async (dispatch) => {
await deleteAllFromRichHistory();
dispatch(richHistoryUpdatedAction({ richHistory: [] }));
};
};
export const historyReducer = (state: ExploreItemState, action: AnyAction): ExploreItemState => {
if (historyUpdatedAction.match(action)) {
return {
...state,
history: action.payload.history,
};
}
return state;
};