mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
* 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>
63 lines
2.0 KiB
TypeScript
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;
|
|
};
|