villebro commented on a change in pull request #14710:
URL: https://github.com/apache/superset/pull/14710#discussion_r637033171



##########
File path: 
superset-frontend/src/filters/components/Select/SelectFilterPlugin.tsx
##########
@@ -18,31 +18,87 @@
  */
 import {
   AppSection,
+  DataMask,
   ensureIsArray,
+  ExtraFormData,
   GenericDataType,
+  JsonObject,
   smartDateDetailedFormatter,
   t,
   tn,
 } from '@superset-ui/core';
-import React, { useEffect, useState } from 'react';
+import React, { useEffect, useReducer, useState } from 'react';
 import { Select } from 'src/common/components';
-import { FIRST_VALUE, PluginFilterSelectProps, SelectValue } from './types';
+import { debounce } from 'lodash';
+import { SLOW_DEBOUNCE } from 'src/constants';
+import { PluginFilterSelectProps, SelectValue } from './types';
 import { StyledSelect, Styles } from '../common';
 import { getDataRecordFormatter, getSelectExtraFormData } from '../../utils';
 
 const { Option } = Select;
 
+const debouncedOwnStateFunc = debounce(
+  (dispatch: { (action: DataMaskAction): void }, val: string) => {
+    dispatch({
+      type: 'ownState',
+      ownState: {
+        search: val,
+      },
+    });
+  },
+  SLOW_DEBOUNCE,
+);
+
+type DataMaskAction =
+  | { type: 'ownState'; ownState: JsonObject }
+  | {
+      type: 'filterState';
+      extraFormData: ExtraFormData;
+      filterState: { value: SelectValue };
+    };
+
+function reducer(state: DataMask, action: DataMaskAction): DataMask {
+  switch (action.type) {
+    case 'ownState':
+      return {
+        ...state,
+        ownState: {
+          ...(state.ownState || {}),
+          ...action.ownState,
+        },
+      };
+    case 'filterState':
+      return {
+        ...state,
+        extraFormData: action.extraFormData,
+        filterState: {
+          ...(state.filterState || {}),
+          ...action.filterState,
+        },
+      };
+    default:
+      return {
+        ...state,
+      };
+  }
+}

Review comment:
       I decided to migrate from multiple `useState`s and `useEffects` to 
`useReduce`, as the full `dataMask` will be updated by different actions but 
needs to be submitted as a whole to `setDataMask`. Therefore, `useReducer` 
seemed like a cleaner approach.




-- 
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