williaster commented on a change in pull request #3581: Dashboard refactory
URL:
https://github.com/apache/incubator-superset/pull/3581#discussion_r147906645
##########
File path: superset/assets/javascripts/dashboard/reducers.js
##########
@@ -0,0 +1,193 @@
+import { combineReducers } from 'redux';
+import d3 from 'd3';
+
+import charts, { chart } from '../chart/chartReducer';
+import * as actions from './actions';
+import { getParam } from '../modules/utils';
+import { alterInArr, removeFromArr } from '../reduxUtils';
+import { applyDefaultFormData } from '../explore/stores/store';
+
+export function getInitialState(bootstrapData) {
+ const { user_id, datasources, common } = bootstrapData;
+ delete common.locale;
+ delete common.language_pack;
+
+ const dashboard = Object.assign({}, bootstrapData.dashboard_data);
+ const filters = {};
+ try {
+ // allow request parameter overwrite dashboard metadata
+ const filterData = JSON.parse(getParam('preselect_filters') ||
dashboard.metadata.default_filters);
+ for (const key in filterData) {
+ const sliceId = parseInt(key, 10);
+ filters[sliceId] = filterData[key];
+ }
+ } catch (e) {
+ //
+ }
+
+ dashboard.posDict = {};
+ dashboard.layout = [];
+ if (dashboard.position_json) {
+ dashboard.position_json.forEach((position) => {
+ dashboard.posDict[position.slice_id] = position;
+ });
+ }
+ dashboard.slices.forEach((slice, index) => {
+ const sliceId = slice.slice_id;
+ let pos = dashboard.posDict[sliceId];
+ if (!pos) {
+ pos = {
+ col: (index * 4 + 1) % 12,
+ row: Math.floor((index) / 3) * 4,
+ size_x: 4,
+ size_y: 4,
+ };
+ }
+
+ dashboard.layout.push({
+ i: String(sliceId),
+ x: pos.col - 1,
+ y: pos.row,
+ w: pos.size_x,
+ minW: 2,
+ h: pos.size_y,
+ });
+ });
+
+ // will use charts action/reducers to handle chart render
+ const initCharts = {};
+ dashboard.slices.forEach((slice) => {
+ const chartKey = 'slice_' + slice.slice_id;
+ initCharts[chartKey] =
+ Object.assign({}, chart, {
+ chartKey,
+ slice_id: slice.slice_id,
+ form_data: slice.form_data,
+ formData: applyDefaultFormData(slice.form_data),
+ });
+ });
+
+ // also need to add formData for dashboard.slices
+ dashboard.slices = dashboard.slices.map(
+ slice => (Object.assign({}, slice,
+ { formData: applyDefaultFormData(slice.form_data) })));
+
+ return {
+ charts: initCharts,
+ dashboard: Object.assign({}, { filters, dashboard, userId: user_id,
datasources, common }),
Review comment:
nit object spread
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services