2020-11-09 07:48:24 -06:00
|
|
|
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
|
|
|
|
//
|
|
|
|
|
2022-02-10 06:59:43 -06:00
|
|
|
export const starHistoryItem = (id: string, starred: boolean): ThunkResult<void> => {
|
2022-02-04 05:46:27 -06:00
|
|
|
return async (dispatch, getState) => {
|
2022-02-10 06:59:43 -06:00
|
|
|
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);
|
2020-11-09 07:48:24 -06:00
|
|
|
dispatch(richHistoryUpdatedAction({ richHistory: nextRichHistory }));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteRichHistory = (): ThunkResult<void> => {
|
2022-02-04 05:46:27 -06:00
|
|
|
return async (dispatch) => {
|
|
|
|
await deleteAllFromRichHistory();
|
2020-11-09 07:48:24 -06:00
|
|
|
dispatch(richHistoryUpdatedAction({ richHistory: [] }));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const historyReducer = (state: ExploreItemState, action: AnyAction): ExploreItemState => {
|
|
|
|
if (historyUpdatedAction.match(action)) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
history: action.payload.history,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return state;
|
|
|
|
};
|