ktmud commented on a change in pull request #9593:
URL: 
https://github.com/apache/incubator-superset/pull/9593#discussion_r467313600



##########
File path: superset-frontend/src/dashboard/util/getFilterConfigsFromFormdata.js
##########
@@ -20,89 +20,85 @@
 import { TIME_FILTER_MAP } from '../../visualizations/FilterBox/FilterBox';
 import { TIME_FILTER_LABELS } from '../../explore/constants';
 
-export default function getFilterConfigsFromFormdata(form_data = {}) {
+/**
+ * Parse filters for Table chart. All non-metric columns are considered
+ * filterable values.
+ */
+function getFilterConfigsFromTableChart(form_data = {}) {
+  const { groupby = [], all_columns = [] } = form_data;
+  const configs = { columns: {}, labels: {} };
+  // `groupby` is from GROUP BY mode (aggregations)
+  // `all_columns` is from NOT GROUP BY mode (raw records)
+  const columns = groupby.concat(all_columns);
+  columns.forEach(column => {
+    configs.columns[column] = undefined;
+    configs.labels[column] = column;
+  });
+  return configs;
+}
+
+/**
+ * Parse filter configs for FilterBox.
+ */
+function getFilterConfigsFromFilterBox(form_data = {}) {
   const {
     date_filter,
     filter_configs = [],
     show_druid_time_granularity,
     show_druid_time_origin,
     show_sqla_time_column,
     show_sqla_time_granularity,
+    table_filter,
   } = form_data;
-  let configs = filter_configs.reduce(
-    ({ columns, labels }, config) => {
-      let defaultValues = config.defaultValue;
-      // defaultValue could be ; separated values,
-      // could be null or ''
-      if (config.defaultValue) {
-        defaultValues = config.defaultValue.split(';');
-      }
-      const updatedColumns = {
-        ...columns,
-        [config.column]: config.vals || defaultValues,
-      };
-      const updatedLabels = {
-        ...labels,
-        [config.column]: config.label,
-      };
+  const configs = { columns: {}, labels: {} };
 
-      return {
-        columns: updatedColumns,
-        labels: updatedLabels,
-      };
-    },
-    { columns: {}, labels: {} },
-  );
+  filter_configs.forEach(({ column, label, defaultValue, vals }) => {
+    const defaultValues =
+      typeof defaultValue === 'string' ? defaultValue.split(';') : 
defaultValue;
+    configs.columns[column] = vals || defaultValues;
+    configs.labels[column] = label;
+  });
 
   if (date_filter) {
-    let updatedColumns = {
-      ...configs.columns,
-      [TIME_FILTER_MAP.time_range]: form_data.time_range,
-    };
-    const updatedLabels = {
-      ...configs.labels,
-      ...Object.entries(TIME_FILTER_MAP).reduce(
-        (map, [key, value]) => ({
-          ...map,
-          [value]: TIME_FILTER_LABELS[key],
-        }),
-        {},
-      ),
-    };

Review comment:
       Interestingly, I find reduce harder to read also because I was not able 
to follow it sequentially. It kind of places the input and output in a very 
twisted flow:
   
   ```js
   input.reduce((intermediateOutput, input) => {
      return intermediateOutput
   }, output);
   ```
   
   Maybe I just didn't use it often enough, but every time I encounter a reduce 
function, I always have to think what the output look like and sometimes it 
could even be difficult to find where the output object was initialized.
   
   For this specific case, the original code looks like this:
   
   ```js
       const updatedLabels  = {
         ...configs.labels,
         ...Object.entries(TIME_FILTER_MAP).reduce(
           (map, [key, value]) => ({
             ...map,
             [value]: TIME_FILTER_LABELS[key],
           }),
           {},
         ),
       };
       configs.labels = updatedLabels;
   ```
   
   Without reduce, it's just:
   
   ```js
       Object.entries(TIME_FILTER_MAP).forEach(([key, column]) => {
         configs.labels[column] = TIME_FILTER_LABELS[key];
       });
   ```
   
   Is reduce really faster to write and read?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to