Skip to content

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

If a dashboard is back to just one tab, auto-assign all cards to that tab (#68201)

Nathan Voxland056d61238eb0
diff --git a/src/metabase/dashboards_rest/api.clj b/src/metabase/dashboards_rest/api.clj
index caaf183e728..fd4edb511ff 100644
--- a/src/metabase/dashboards_rest/api.clj
+++ b/src/metabase/dashboards_rest/api.clj
@@ -1012,11 +1012,18 @@
(let [{current-dashcards :dashcards
current-tabs :tabs
:as hydrated-current-dash} (t2/hydrate current-dash [:dashcards :series :card] :tabs)
- _ (when (and (seq current-tabs)
+ new-tabs (map-indexed (fn [idx tab] (assoc tab :position idx)) tabs)
+ dashcards (if (= 1 (count new-tabs))
+ (let [single-tab-id (-> new-tabs first :id)]
+ (mapv #(if (nil? (:dashboard_tab_id %))
+ (assoc % :dashboard_tab_id single-tab-id)
+ %)
+ dashcards))
+ dashcards)
+ _ (when (and (seq new-tabs)
(not (every? #(some? (:dashboard_tab_id %)) dashcards)))
(throw (ex-info (tru "This dashboard has tab, makes sure every card has a tab")
{:status-code 400})))
- new-tabs (map-indexed (fn [idx tab] (assoc tab :position idx)) tabs)
{:keys [old->new-tab-id
deleted-tab-ids]
:as tabs-changes-stats} (dashboard-tab/do-update-tabs! (:id current-dash) current-tabs new-tabs)
diff --git a/test/metabase/dashboards_rest/api_test.clj b/test/metabase/dashboards_rest/api_test.clj
index 64896cc6398..1e7bcc44b3e 100644
--- a/test/metabase/dashboards_rest/api_test.clj
+++ b/test/metabase/dashboards_rest/api_test.clj
@@ -2088,7 +2088,7 @@
(deftest update-cards-error-handling-test
(testing "PUT /api/dashboard/:id"
(with-simple-dashboard-with-tabs [{:keys [dashboard-id]}]
- (testing "if a dashboard has tabs, check if all cards from the request has a tab_id"
+ (testing "if a dashboard has multiple tabs, check if all cards from the request has a tab_id"
(is (= "This dashboard has tab, makes sure every card has a tab"
(mt/user-http-request :crowberto :put 400 (format "dashboard/%d" dashboard-id)
{:dashcards (conj
@@ -2100,6 +2100,30 @@
:row 1})
:tabs (tabs dashboard-id)})))))))
+(deftest update-cards-auto-assign-single-tab-test
+ (testing "PUT /api/dashboard/:id - auto-assign null dashboard_tab_id to the single tab (metabase#67971)"
+ (mt/with-temp
+ [:model/Dashboard {dashboard-id :id} {}
+ :model/Card {card-id :id} {}
+ :model/DashboardTab {tab-id :id} {:name "Tab 1" :dashboard_id dashboard-id :position 0}
+ :model/DashboardCard _ {:dashboard_id dashboard-id
+ :card_id card-id
+ :dashboard_tab_id tab-id}]
+ (testing "when dashboard has exactly one tab, cards with null dashboard_tab_id are auto-assigned to that tab"
+ (let [resp (mt/user-http-request :crowberto :put 200 (format "dashboard/%d" dashboard-id)
+ {:dashcards (conj
+ (current-cards dashboard-id)
+ {:id -1
+ :size_x 4
+ :size_y 4
+ :col 1
+ :row 1
+ :card_id card-id})
+ :tabs [{:id tab-id :name "Tab 1"}]})
+ new-card (last (sort-by :id (:dashcards resp)))]
+ ;; The new card should have been auto-assigned to the single tab
+ (is (= tab-id (:dashboard_tab_id new-card))))))))
+
(deftest update-tabs-track-snowplow-test
(mt/with-temp
[:model/Dashboard {dashboard-id :id} {}