Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Remove `metabase/redux/question-reducer` (#76134)

Romeo Van Snickcd59e7adb550
diff --git a/frontend/src/metabase/api/card.ts b/frontend/src/metabase/api/card.ts
index a8806c58072..a668bfb5856 100644
--- a/frontend/src/metabase/api/card.ts
+++ b/frontend/src/metabase/api/card.ts
@@ -171,6 +171,7 @@ export const cardApi = Api.injectEndpoints({
body,
}),
invalidatesTags: (_, error) => invalidateTags(error, [listTag("card")]),
+ onQueryStarted: hydrateMetadataStore(QuestionSchema),
}),
createCardFromCsv: builder.mutation<Card, CreateCardFromCsvRequest>({
query: ({ file, collection_id }) => {
@@ -221,6 +222,7 @@ export const cardApi = Api.injectEndpoints({
return invalidateTags(error, tags);
},
+ onQueryStarted: hydrateMetadataStore(QuestionSchema),
}),
deleteCard: builder.mutation<void, CardId>({
query: (id) => ({
diff --git a/frontend/src/metabase/questions/components/MoveCardModal/MoveCardModal.tsx b/frontend/src/metabase/questions/components/MoveCardModal/MoveCardModal.tsx
index 797515a50c2..a41bfb57df9 100644
--- a/frontend/src/metabase/questions/components/MoveCardModal/MoveCardModal.tsx
+++ b/frontend/src/metabase/questions/components/MoveCardModal/MoveCardModal.tsx
@@ -16,7 +16,6 @@ import { DashboardName } from "metabase/common/components/DashboardName";
import type { OmniPickerCollectionItem } from "metabase/common/components/Pickers";
import { MoveModal } from "metabase/common/components/Pickers";
import { useDispatch } from "metabase/redux";
-import { INJECT_RTK_QUERY_QUESTION_VALUE } from "metabase/redux/entities/questions-reducer";
import { API_UPDATE_QUESTION } from "metabase/redux/query-builder";
import { addUndo } from "metabase/redux/undo";
import { Box, Icon, Radio, Title } from "metabase/ui";
@@ -74,10 +73,6 @@ export const MoveCardModal = ({ card, onClose }: MoveCardModalProps) => {
// HACK: entity framework would previously keep the qb in sync
// with changing where the question lived
dispatch({ type: API_UPDATE_QUESTION, payload: updatedCard });
- dispatch({
- type: INJECT_RTK_QUERY_QUESTION_VALUE,
- payload: updatedCard,
- });
dispatch(
addUndo({
diff --git a/frontend/src/metabase/redux/cards.ts b/frontend/src/metabase/redux/cards.ts
index cf7b88a0091..59be7f35536 100644
--- a/frontend/src/metabase/redux/cards.ts
+++ b/frontend/src/metabase/redux/cards.ts
@@ -2,7 +2,6 @@ import _ from "underscore";
import { cardApi } from "metabase/api";
import { runRtkEndpoint } from "metabase/api/utils/run-rtk-endpoint";
-import { INJECT_RTK_QUERY_QUESTION_VALUE } from "metabase/redux/entities/questions-reducer";
import type { Dispatch } from "metabase/redux/store";
import type { Card } from "metabase-types/api";
@@ -85,10 +84,6 @@ export const createQuestionCard =
cardApi.endpoints.createCard,
);
dispatch(cardCreated(card));
- // Hydrate the normalized `questions` slice so `getMetadata(state).question(id)`
- // reflects the write immediately, instead of waiting for a later GET — the
- // entity framework used to do this when normalizing the create response.
- dispatch({ type: INJECT_RTK_QUERY_QUESTION_VALUE, payload: card });
return card;
};
@@ -105,9 +100,5 @@ export const updateQuestionCard =
cardApi.endpoints.updateCard,
);
dispatch(cardUpdated(card));
- // Hydrate the normalized `questions` slice so `getMetadata(state).question(id)`
- // reflects the write immediately, instead of waiting for a later GET — the
- // entity framework used to do this when normalizing the update response.
- dispatch({ type: INJECT_RTK_QUERY_QUESTION_VALUE, payload: card });
return card;
};
diff --git a/frontend/src/metabase/redux/entities/index.ts b/frontend/src/metabase/redux/entities/index.ts
index 4b1b2237929..108b0ba4af6 100644
--- a/frontend/src/metabase/redux/entities/index.ts
+++ b/frontend/src/metabase/redux/entities/index.ts
@@ -1,7 +1,6 @@
import { type Reducer, combineReducers } from "@reduxjs/toolkit";
import { getIn } from "icepick";
-import { questionsReducer } from "./questions-reducer";
import { tablesReducer } from "./tables-reducer";
type SliceState = Record<string, unknown>;
@@ -77,7 +76,6 @@ function makeSliceReducer(
}
const customReducers: Partial<Record<string, SliceReducer>> = {
- questions: questionsReducer as SliceReducer,
tables: tablesReducer as SliceReducer,
};
diff --git a/frontend/src/metabase/redux/entities/index.unit.spec.ts b/frontend/src/metabase/redux/entities/index.unit.spec.ts
index 6756b39ef08..27a6c3339ed 100644
--- a/frontend/src/metabase/redux/entities/index.unit.spec.ts
+++ b/frontend/src/metabase/redux/entities/index.unit.spec.ts
@@ -1,5 +1,3 @@
-import { SOFT_RELOAD_CARD } from "metabase/redux/query-builder";
-
import { reducer } from "./index";
const UPDATE = "metabase/entities/UPDATE";
@@ -124,27 +122,29 @@ describe("entities reducer", () => {
});
describe("custom slice reducers", () => {
- it("runs the questions custom reducer after merging entities", () => {
- const merged = reducer(undefined, {
+ it("runs the tables custom reducer after merging entities", () => {
+ const seeded = reducer(undefined, {
type: UPDATE,
payload: {
entities: {
- questions: { 1: { id: 1, moderated_status: null } },
+ tables: {
+ 5: { id: 5, original_fields: [{ id: 100, name: "Old" }] },
+ },
},
},
});
- const next = reducer(merged, {
- type: SOFT_RELOAD_CARD,
+ const next = reducer(seeded, {
+ type: UPDATE,
payload: {
- id: 1,
- moderation_reviews: [{ status: "verified", most_recent: true }],
+ entities: {
+ fields: { 100: { id: 100, table_id: 5, name: "New" } },
+ },
},
});
- expect(next.questions[1]).toMatchObject({
- id: 1,
- moderated_status: "verified",
+ expect(next.tables[5]).toMatchObject({
+ original_fields: [{ id: 100, name: "New" }],
});
});
});
diff --git a/frontend/src/metabase/redux/entities/questions-reducer.ts b/frontend/src/metabase/redux/entities/questions-reducer.ts
deleted file mode 100644
index 323c67e4759..00000000000
--- a/frontend/src/metabase/redux/entities/questions-reducer.ts
+++ /dev/null
@@ -1,54 +0,0 @@
-import { updateIn } from "icepick";
-
-import { SOFT_RELOAD_CARD } from "metabase/redux/query-builder";
-
-/**
- * Dispatched (with a card payload) to merge freshly-fetched RTK Query card data
- * into the normalized `state.entities.questions` slice, so `getMetadata` sees it.
- */
-export const INJECT_RTK_QUERY_QUESTION_VALUE =
- "metabase/entities/questions/FETCH_ADHOC_METADATA";
-
-type QuestionState = Record<string, any>;
-type ReducerAction = { type: string; payload?: any };
-
-/**
- * Reducer for the `state.entities.questions` slice.
- *
- * Questions no longer go through the entity framework — CRUD lives in
- * `metabase/api/card`. The slice itself is still consumed by `getMetadata` in
- * `metabase/selectors/metadata.ts`, so we keep it in sync when:
- *
- * - a card is soft-reloaded (to reflect the latest moderation status), and
- * - an RTK Query consumer injects ad-hoc card values via
- * `INJECT_RTK_QUERY_QUESTION_VALUE`.
- *
- * It runs after the generic slice reducer in `./index` has merged any
- * `payload.entities.questions` from `metabase/entities/*` actions.
- */
-export function questionsReducer(
- state: QuestionState = {},
- { type, payload }: ReducerAction,
-): QuestionState {
- if (type === SOFT_RELOAD_CARD) {
- const { id } = payload;
- const latestReview = payload.moderation_reviews?.find(
- (review: { most_recent?: boolean }) => review.most_recent,
- );
-
- if (latestReview) {
- return updateIn(state, [id], (question) => ({
- ...question,
- moderated_status: latestReview.status,
- }));
- }
- }
-
- if (type === INJECT_RTK_QUERY_QUESTION_VALUE) {
- const { id } = payload;
-
- return updateIn(state, [id], (question) => ({ ...question, ...payload }));
- }
-
- return state;
-}
diff --git a/frontend/src/metabase/redux/entities/questions-reducer.unit.spec.ts b/frontend/src/metabase/redux/entities/questions-reducer.unit.spec.ts
deleted file mode 100644
index eb89a121b4a..00000000000
--- a/frontend/src/metabase/redux/entities/questions-reducer.unit.spec.ts
+++ /dev/null
@@ -1,57 +0,0 @@
-import { SOFT_RELOAD_CARD } from "metabase/redux/query-builder";
-
-import {
- INJECT_RTK_QUERY_QUESTION_VALUE,
- questionsReducer,
-} from "./questions-reducer";
-
-describe("questionsReducer", () => {
- describe(SOFT_RELOAD_CARD, () => {
- it("updates moderated_status from the most recent review", () => {
- const state = { 1: { id: 1, moderated_status: null } };
-
- const nextState = questionsReducer(state, {
- type: SOFT_RELOAD_CARD,
- payload: {
- id: 1,
- moderation_reviews: [
- { status: "verified", most_recent: true },
- { status: null, most_recent: false },
- ],
- },
- });
-
- expect(nextState[1].moderated_status).toBe("verified");
- });
-
- it("leaves state untouched when there is no most-recent review", () => {
- const state = { 1: { id: 1, moderated_status: "verified" } };
-
- const nextState = questionsReducer(state, {
- type: SOFT_RELOAD_CARD,
- payload: { id: 1, moderation_reviews: [] },
- });
-
- expect(nextState).toBe(state);
- });
- });
-
- describe(INJECT_RTK_QUERY_QUESTION_VALUE, () => {
- it("merges the payload into the existing question", () => {
- const state = { 1: { id: 1, name: "Old", display: "table" } };
-
- const nextState = questionsReducer(state, {
- type: INJECT_RTK_QUERY_QUESTION_VALUE,
- payload: { id: 1, name: "New" },
- });
-
- expect(nextState[1]).toEqual({ id: 1, name: "New", display: "table" });
- });
- });
-
- it("ignores unrelated actions", () => {
- const state = { 1: { id: 1 } };
-
- expect(questionsReducer(state, { type: "metabase/unrelated" })).toBe(state);
- });
-});