Skip to content
Snippets Groups Projects
Unverified Commit 875137b6 authored by Alexander Polyankin's avatar Alexander Polyankin Committed by GitHub
Browse files

Remove metabase-lib dependency on redux (#25893)

parent cf364378
No related branches found
No related tags found
No related merge requests found
......@@ -45,7 +45,6 @@ import {
maybeUsePivotEndpoint,
MetabaseApi,
} from "metabase/services";
import Questions from "metabase/entities/questions";
import {
Parameter as ParameterObject,
ParameterValues,
......@@ -1167,39 +1166,6 @@ class QuestionInner {
}
}
// NOTE: prefer `reduxCreate` so the store is automatically updated
async apiCreate() {
const createdCard = await Questions.api.create(this.card());
return this.setCard(createdCard);
}
// NOTE: prefer `reduxUpdate` so the store is automatically updated
async apiUpdate() {
const updatedCard = await Questions.api.update(this.card());
return this.setCard(updatedCard);
}
async reduxCreate(dispatch) {
const action = await dispatch(Questions.actions.create(this.card()));
return this.setCard(Questions.HACK_getObjectFromAction(action));
}
async reduxUpdate(dispatch, { excludeDatasetQuery = false } = {}) {
const fullCard = this.card();
const card = excludeDatasetQuery
? _.omit(fullCard, "dataset_query")
: fullCard;
const action = await dispatch(
Questions.actions.update(
{
id: this.id(),
},
card,
),
);
return this.setCard(Questions.HACK_getObjectFromAction(action));
}
setParameters(parameters) {
return this.setCard(assoc(this.card(), "parameters", parameters));
}
......
import { createAction } from "redux-actions";
import _ from "underscore";
import * as MetabaseAnalytics from "metabase/lib/analytics";
import { loadCard } from "metabase/lib/card";
import { shouldOpenInBlankWindow } from "metabase/lib/dom";
......@@ -181,10 +182,13 @@ export const apiCreateQuestion = question => {
: question;
const resultsMetadata = getResultsMetadata(getState());
const createdQuestion = await questionWithVizSettings
const questionToCreate = questionWithVizSettings
.setQuery(question.query().clean())
.setResultsMetadata(resultsMetadata)
.reduxCreate(dispatch);
.setResultsMetadata(resultsMetadata);
const createdQuestion = await reduxCreateQuestion(
questionToCreate,
dispatch,
);
const databases = Databases.selectors.getList(getState());
if (databases && !databases.some(d => d.is_saved_questions)) {
......@@ -226,15 +230,20 @@ export const apiUpdateQuestion = (question, { rerunQuery } = {}) => {
: question;
const resultsMetadata = getResultsMetadata(getState());
const updatedQuestion = await questionWithVizSettings
const questionToUpdate = questionWithVizSettings
.setQuery(question.query().clean())
.setResultsMetadata(resultsMetadata)
// When viewing a dataset, its dataset_query is swapped with a clean query using the dataset as a source table
// (it's necessary for datasets to behave like tables opened in simple mode)
// When doing updates like changing name, description, etc., we need to omit the dataset_query in the request body
.reduxUpdate(dispatch, {
.setResultsMetadata(resultsMetadata);
// When viewing a dataset, its dataset_query is swapped with a clean query using the dataset as a source table
// (it's necessary for datasets to behave like tables opened in simple mode)
// When doing updates like changing name, description, etc., we need to omit the dataset_query in the request body
const updatedQuestion = await reduxUpdateQuestion(
questionToUpdate,
dispatch,
{
excludeDatasetQuery: isAdHocModelQuestion(question, originalQuestion),
});
},
);
// reload the question alerts for the current question
// (some of the old alerts might be removed during update)
......@@ -274,3 +283,23 @@ export const revertToRevision = createThunkAction(
};
},
);
async function reduxCreateQuestion(question, dispatch) {
const action = await dispatch(Questions.actions.create(question.card()));
return question.setCard(Questions.HACK_getObjectFromAction(action));
}
async function reduxUpdateQuestion(
question,
dispatch,
{ excludeDatasetQuery = false },
) {
const fullCard = question.card();
const card = excludeDatasetQuery
? _.omit(fullCard, "dataset_query")
: fullCard;
const action = await dispatch(
Questions.actions.update({ id: question.id() }, card),
);
return question.setCard(Questions.HACK_getObjectFromAction(action));
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment