From 6e395c8275f09e561c4bf5abfedbcd7b7b121851 Mon Sep 17 00:00:00 2001 From: Levente Balogh Date: Thu, 14 Oct 2021 08:35:04 +0200 Subject: [PATCH] Plugins Catalog: use createAction() instead of string action type (#40393) * refactor(Plugins/Admin): use createAction() instead of string action type * refactor(Panel/Actions): use the new action name in the tests --- public/app/features/panel/state/actions.test.ts | 3 ++- public/app/features/plugins/admin/state/actions.ts | 9 ++++----- public/app/features/plugins/admin/state/reducer.ts | 7 ++++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/public/app/features/panel/state/actions.test.ts b/public/app/features/panel/state/actions.test.ts index fcd5fba6daf..e2606f53054 100644 --- a/public/app/features/panel/state/actions.test.ts +++ b/public/app/features/panel/state/actions.test.ts @@ -3,6 +3,7 @@ import { thunkTester } from '../../../../test/core/thunk/thunkTester'; import { changePanelPlugin } from './actions'; import { panelModelAndPluginReady } from './reducers'; import { getPanelPlugin } from 'app/features/plugins/__mocks__/pluginMocks'; +import { panelPluginLoaded } from 'app/features/plugins/admin/state/actions'; jest.mock('app/features/plugins/importPanelPlugin', () => { return { @@ -31,7 +32,7 @@ describe('panel state actions', () => { .whenThunkIsDispatched(sourcePanel, 'table'); expect(dispatchedActions.length).toBe(2); - expect(dispatchedActions[0].type).toBe('plugins/loadPanelPlugin/fulfilled'); + expect(dispatchedActions[0].type).toBe(panelPluginLoaded.type); expect(dispatchedActions[1].type).toBe(panelModelAndPluginReady.type); expect(sourcePanel.type).toBe('table'); }); diff --git a/public/app/features/plugins/admin/state/actions.ts b/public/app/features/plugins/admin/state/actions.ts index 0cd50413617..af485d8f198 100644 --- a/public/app/features/plugins/admin/state/actions.ts +++ b/public/app/features/plugins/admin/state/actions.ts @@ -1,4 +1,4 @@ -import { createAsyncThunk, Update } from '@reduxjs/toolkit'; +import { createAction, createAsyncThunk, Update } from '@reduxjs/toolkit'; import { getBackendSrv } from '@grafana/runtime'; import { PanelPlugin } from '@grafana/data'; import { StoreState, ThunkResult } from 'app/types'; @@ -96,6 +96,8 @@ export const loadPluginDashboards = createAsyncThunk(`${STATE_PREFIX}/loadPlugin return getBackendSrv().get(url); }); +export const panelPluginLoaded = createAction(`${STATE_PREFIX}/panelPluginLoaded`); + // We need this to be backwards-compatible with other parts of Grafana. // (Originally in "public/app/features/plugins/state/actions.ts") // It cannot be constructed with `createAsyncThunk()` as we need the return value on the call-site, @@ -110,10 +112,7 @@ export const loadPanelPlugin = (id: string): ThunkResult> = // second check to protect against raise condition if (!getStore().plugins.panels[id]) { - dispatch({ - type: `${STATE_PREFIX}/loadPanelPlugin/fulfilled`, - payload: plugin, - }); + dispatch(panelPluginLoaded(plugin)); } } diff --git a/public/app/features/plugins/admin/state/reducer.ts b/public/app/features/plugins/admin/state/reducer.ts index a0307e1b549..74b6a03c054 100644 --- a/public/app/features/plugins/admin/state/reducer.ts +++ b/public/app/features/plugins/admin/state/reducer.ts @@ -1,7 +1,8 @@ import { createSlice, createEntityAdapter, AnyAction, PayloadAction } from '@reduxjs/toolkit'; -import { fetchAll, fetchDetails, install, uninstall, loadPluginDashboards } from './actions'; +import { fetchAll, fetchDetails, install, uninstall, loadPluginDashboards, panelPluginLoaded } from './actions'; import { CatalogPlugin, PluginListDisplayMode, ReducerState, RequestStatus } from '../types'; import { STATE_PREFIX } from '../constants'; +import { PanelPlugin } from '@grafana/data'; export const pluginsAdapter = createEntityAdapter(); @@ -62,8 +63,8 @@ const slice = createSlice({ }) // Load a panel plugin (backward-compatibility) // TODO - .addCase(`${STATE_PREFIX}/loadPanelPlugin/fulfilled`, (state, action: AnyAction) => { - state.panels[action.payload.meta!.id] = action.payload; + .addCase(panelPluginLoaded, (state, action: PayloadAction) => { + state.panels[action.payload.meta.id] = action.payload; }) // Start loading panel dashboards (backward-compatibility) // TODO