Skip to content

Commit d183dac

Browse files
richardfogacakshi020302
authored andcommitted
fix: 'save and go to dashboard' option was disabled after changing the chart type (apache#36122)
1 parent 6e1be6c commit d183dac

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed

superset-frontend/src/explore/controlUtils/standardizedFormData.test.ts

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ describe('should collect control values and create SFD', () => {
217217
// advanced analytics - resample
218218
resample_rule: '1D',
219219
resample_method: 'zerofill',
220+
// dashboard context
221+
dashboardId: 123,
220222
};
221223
const sourceMockFormData: QueryFormData = {
222224
...sharedControlsFormData,
@@ -240,12 +242,15 @@ describe('should collect control values and create SFD', () => {
240242
};
241243

242244
beforeAll(() => {
245+
// dashboardId is not a control, it's just context, so exclude it from control definitions
246+
const publicControlFields = publicControls.filter(c => c !== 'dashboardId');
247+
243248
getChartControlPanelRegistry().registerValue('source_viz', {
244249
controlPanelSections: [
245250
sections.advancedAnalyticsControls,
246251
{
247252
label: 'transform controls',
248-
controlSetRows: publicControls.map(control => [control]),
253+
controlSetRows: publicControlFields.map(control => [control]),
249254
},
250255
{
251256
label: 'axis column',
@@ -258,7 +263,7 @@ describe('should collect control values and create SFD', () => {
258263
sections.advancedAnalyticsControls,
259264
{
260265
label: 'transform controls',
261-
controlSetRows: publicControls.map(control => [control]),
266+
controlSetRows: publicControlFields.map(control => [control]),
262267
},
263268
{
264269
label: 'axis column',
@@ -447,6 +452,83 @@ describe('should transform form_data between table and bigNumberTotal', () => {
447452
]);
448453
expect(tblFormData.groupby).toEqual(['name', 'gender', adhocColumn]);
449454
});
455+
456+
test('preserves dashboardId when transforming between viz types', () => {
457+
// Create form data with dashboardId (simulating opening explore from a dashboard)
458+
const formDataWithDashboard = {
459+
...tableVizFormData,
460+
dashboardId: 42,
461+
};
462+
const storeWithDashboard = {
463+
...tableVizStore,
464+
form_data: formDataWithDashboard,
465+
};
466+
467+
// Transform table -> bigNumberTotal
468+
const sfd = new StandardizedFormData(formDataWithDashboard);
469+
const { formData: bntFormData } = sfd.transform(
470+
VizType.BigNumberTotal,
471+
storeWithDashboard,
472+
);
473+
474+
// Verify dashboardId is preserved after transformation
475+
expect(bntFormData.dashboardId).toBe(42);
476+
477+
// Transform back bigNumberTotal -> table
478+
const sfd2 = new StandardizedFormData(bntFormData);
479+
const { formData: tblFormData } = sfd2.transform('table', {
480+
...storeWithDashboard,
481+
form_data: bntFormData,
482+
controls: {
483+
...tableVizStore.controls,
484+
},
485+
});
486+
487+
// Verify dashboardId is still preserved after second transformation
488+
expect(tblFormData.dashboardId).toBe(42);
489+
});
490+
491+
test('handles missing dashboardId gracefully', () => {
492+
// Test with no dashboardId (exploring a chart not from a dashboard)
493+
const formDataNoDashboard = {
494+
...tableVizFormData,
495+
// dashboardId is undefined
496+
};
497+
const storeNoDashboard = {
498+
...tableVizStore,
499+
form_data: formDataNoDashboard,
500+
};
501+
502+
const sfd = new StandardizedFormData(formDataNoDashboard);
503+
const { formData: bntFormData } = sfd.transform(
504+
VizType.BigNumberTotal,
505+
storeNoDashboard,
506+
);
507+
508+
// dashboardId should not be present when it was never set
509+
expect(bntFormData.dashboardId).toBeUndefined();
510+
});
511+
512+
test('handles null dashboardId', () => {
513+
// Test with explicit null dashboardId
514+
const formDataNullDashboard = {
515+
...tableVizFormData,
516+
dashboardId: null,
517+
};
518+
const storeNullDashboard = {
519+
...tableVizStore,
520+
form_data: formDataNullDashboard,
521+
};
522+
523+
const sfd = new StandardizedFormData(formDataNullDashboard);
524+
const { formData: bntFormData } = sfd.transform(
525+
VizType.BigNumberTotal,
526+
storeNullDashboard,
527+
);
528+
529+
// null is falsy, so dashboardId should not be added
530+
expect(bntFormData.dashboardId).toBeUndefined();
531+
});
450532
});
451533

452534
// eslint-disable-next-line no-restricted-globals -- TODO: Migrate from describe blocks

superset-frontend/src/explore/controlUtils/standardizedFormData.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ export const publicControls = [
8181
// advanced analytics - resample
8282
'resample_rule', // via sections.advancedAnalytics
8383
'resample_method', // via sections.advancedAnalytics
84+
// dashboard context
85+
'dashboardId', // preserve dashboard context when changing viz type
8486
];
8587

8688
export class StandardizedFormData {
@@ -216,6 +218,10 @@ export class StandardizedFormData {
216218
});
217219
const targetFormData = {
218220
...getFormDataFromControls(targetControlsState),
221+
// Preserve dashboard context when switching viz types.
222+
...(publicFormData.dashboardId && {
223+
dashboardId: publicFormData.dashboardId,
224+
}),
219225
standardizedFormData: this.serialize(),
220226
};
221227

superset_text.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# under the License.
1717

1818

19-
# To set the images of your preferred database, you may create a mapping here with engine and locations of the relevant images. The image can be hosted locally inside your static/file directory or online (e.g. S3)
19+
# To set the images of your preferred database, you may create a mapping here with engine and locations of the relevant images. The image can be hosted locally inside your static/file directory or online (e.g. S3).
2020

2121
# DB_IMAGES:
2222
# postgresql: "path/to/image/postgres.jpg"

0 commit comments

Comments
 (0)