Skip to content
Snippets Groups Projects
Commit 72b9489a authored by Tom Robinson's avatar Tom Robinson
Browse files

Use redux action to save xray dashboard, make sure it invalidates dashboard listings

parent ca3c9b15
No related branches found
No related tags found
No related merge requests found
......@@ -22,7 +22,7 @@ import Parameters from "metabase/parameters/components/Parameters";
import { getMetadata } from "metabase/selectors/metadata";
import { getUserIsAdmin } from "metabase/selectors/user";
import { DashboardApi } from "metabase/services";
import Dashboards from "metabase/entities/dashboards";
import * as Urls from "metabase/lib/urls";
import MetabaseAnalytics from "metabase/lib/analytics";
......@@ -41,7 +41,11 @@ const mapStateToProps = (state, props) => ({
dashboardId: getDashboardId(state, props),
});
@connect(mapStateToProps)
const mapDispatchToProps = {
saveDashboard: Dashboards.actions.save,
};
@connect(mapStateToProps, mapDispatchToProps)
@DashboardData
@withToast
@title(({ dashboard }) => dashboard && dashboard.name)
......@@ -58,9 +62,11 @@ class AutomaticDashboardApp extends React.Component {
}
save = async () => {
const { dashboard, triggerToast } = this.props;
const { dashboard, triggerToast, saveDashboard } = this.props;
// remove the transient id before trying to save
const newDashboard = await DashboardApi.save(dissoc(dashboard, "id"));
const { payload: newDashboard } = await saveDashboard(
dissoc(dashboard, "id"),
);
triggerToast(
<div className="flex align-center">
<Icon
......
......@@ -18,6 +18,7 @@ const Dashboards = createEntity({
api: {
favorite: POST("/api/dashboard/:id/favorite"),
unfavorite: DELETE("/api/dashboard/:id/favorite"),
save: POST("/api/dashboard/save"),
},
objectActions: {
......@@ -56,6 +57,17 @@ const Dashboards = createEntity({
},
},
actions: {
save: dashboard => async dispatch => {
const savedDashboard = await Dashboards.api.save(dashboard);
dispatch({ type: Dashboards.actionTypes.INVALIDATE_LISTS_ACTION });
return {
type: "metabase/entities/dashboards/SAVE_DASHBOARD",
payload: savedDashboard,
};
},
},
reducer: (state = {}, { type, payload, error }) => {
if (type === FAVORITE_ACTION && !error) {
return assocIn(state, [payload, "favorite"], true);
......
......@@ -175,6 +175,9 @@ export function createEntity(def: EntityDefinition): Entity {
const UPDATE_ACTION = `metabase/entities/${entity.name}/UPDATE`;
const DELETE_ACTION = `metabase/entities/${entity.name}/DELETE`;
const FETCH_LIST_ACTION = `metabase/entities/${entity.name}/FETCH_LIST`;
const INVALIDATE_LISTS_ACTION = `metabase/entities/${
entity.name
}/INVALIDATE_LISTS_ACTION`;
entity.actionTypes = {
CREATE: CREATE_ACTION,
......@@ -182,6 +185,7 @@ export function createEntity(def: EntityDefinition): Entity {
UPDATE: UPDATE_ACTION,
DELETE: DELETE_ACTION,
FETCH_LIST: FETCH_LIST_ACTION,
INVALIDATE_LISTS_ACTION: INVALIDATE_LISTS_ACTION,
...(entity.actionTypes || {}),
};
......@@ -470,7 +474,8 @@ export function createEntity(def: EntityDefinition): Entity {
entity.actionShouldInvalidateLists = action =>
action.type === CREATE_ACTION ||
action.type === DELETE_ACTION ||
action.type === UPDATE_ACTION;
action.type === UPDATE_ACTION ||
action.type === INVALIDATE_LISTS_ACTION;
}
entity.requestsReducer = (state, 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