zhaoyongjie commented on a change in pull request #17680:
URL: https://github.com/apache/superset/pull/17680#discussion_r767135411



##########
File path: 
superset-frontend/src/dashboard/components/nativeFilters/FilterBar/index.tsx
##########
@@ -186,23 +191,47 @@ const FilterBar: React.FC<FiltersBarProps> = ({
     [dataMaskSelected, dispatch, setDataMaskSelected, tab],
   );
 
+  const getFilterKeyForUrl = async (value: string) => {
+    let key;
+    try {
+      key = await createFilterKey(dashboardId, value);
+    } catch (err) {
+      return null;
+    }
+    return key;
+  };
+

Review comment:
       remove `getFilterKeyForUrl`.

##########
File path: superset-frontend/src/dashboard/actions/hydrate.js
##########
@@ -67,10 +67,10 @@ export const hydrateDashboard =
     dashboardData,
     chartData,
     filterboxMigrationState = FILTER_BOX_MIGRATION_STATES.NOOP,
+    dataMaskApplied,
   ) =>
   (dispatch, getState) => {
     const { user, common } = getState();
-

Review comment:
       remove unnecessary change

##########
File path: superset-frontend/src/utils/urlUtils.ts
##########
@@ -28,6 +28,9 @@ export function getUrlParam(param: UrlParam & { type: 
'number' }): number;
 export function getUrlParam(param: UrlParam & { type: 'boolean' }): boolean;

Review comment:
       This file unnecessary change.

##########
File path: 
superset-frontend/src/dashboard/components/nativeFilters/FilterBar/keyValue.tsx
##########
@@ -0,0 +1,45 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { SupersetClient } from '@superset-ui/core';

Review comment:
       ```suggestion
   import { SupersetClient, logging } from '@superset-ui/core';
   ```

##########
File path: 
superset-frontend/src/dashboard/components/nativeFilters/FilterBar/index.tsx
##########
@@ -186,23 +191,47 @@ const FilterBar: React.FC<FiltersBarProps> = ({
     [dataMaskSelected, dispatch, setDataMaskSelected, tab],
   );
 
+  const getFilterKeyForUrl = async (value: string) => {
+    let key;
+    try {
+      key = await createFilterKey(dashboardId, value);
+    } catch (err) {
+      return null;
+    }
+    return key;
+  };
+
   const publishDataMask = useCallback(
-    (dataMaskSelected: DataMaskStateWithId) => {
+    async (dataMaskSelected: DataMaskStateWithId) => {
       const { location } = history;
       const { search } = location;
       const previousParams = new URLSearchParams(search);
       const newParams = new URLSearchParams();
-
+      let dataMaskKey = '';
       previousParams.forEach((value, key) => {
         if (key !== URL_PARAMS.nativeFilters.name) {
           newParams.append(key, value);
         }
       });
 
-      newParams.set(
-        URL_PARAMS.nativeFilters.name,
-        rison.encode(replaceUndefinedByNull(dataMaskSelected)),
-      );
+      const getParam = getUrlParam(URL_PARAMS.nativeFilters);
+      const dataMask = JSON.stringify(dataMaskSelected);
+
+      if (typeof getParam === 'object') {
+        const res = await getFilterKeyForUrl(rison.encode(getParam));
+        if (res === null) {
+          dataMaskKey = rison.encode(replaceUndefinedByNull(dataMaskSelected));
+        } else dataMaskKey = res;
+      } else if (typeof getParam === 'string') {
+        try {
+          const res = await updateFilterKey(dashboardId, dataMask, getParam);
+          if (res === 'Value updated successfully.') {
+            dataMaskKey = getParam;
+          }
+          // eslint-disable-next-line no-empty
+        } catch {}
+      }

Review comment:
       ```suggestion
         const nativeFiltersCacheKey = getUrlParam(
           URL_PARAMS.nativeFiltersByCacheKey,
         );
         if (
           nativeFiltersCacheKey &&
           (await updateFilterKey(dashboardId, dataMask, nativeFiltersCacheKey))
         ) {
           dataMaskKey = nativeFiltersCacheKey;
         } else {
           const nativeFiltersObj = getUrlParam(URL_PARAMS.nativeFilters);
           dataMaskKey =
             (await createFilterKey(
               dashboardId,
               rison.encode(nativeFiltersObj),
             )) ?? rison.encode(replaceUndefinedByNull(dataMaskSelected));
         }
   ```

##########
File path: 
superset-frontend/src/dashboard/components/nativeFilters/FilterBar/keyValue.tsx
##########
@@ -0,0 +1,45 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { SupersetClient } from '@superset-ui/core';
+
+export const updateFilterKey = (dashId: string, value: string, key: string) =>
+  SupersetClient.put({
+    endpoint: `api/v1/dashboard/${dashId}/filter_state/${key}/`,
+    jsonPayload: { value },
+  })
+    .then(r => r.json.message)
+    .catch(err => err);

Review comment:
       ```suggestion
       .then(r => r.response.ok)
       .catch(err => {
         logging.error(err);
         return null;
       });
   ```

##########
File path: 
superset-frontend/src/dashboard/components/nativeFilters/FilterBar/keyValue.tsx
##########
@@ -0,0 +1,45 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { SupersetClient } from '@superset-ui/core';
+
+export const updateFilterKey = (dashId: string, value: string, key: string) =>
+  SupersetClient.put({
+    endpoint: `api/v1/dashboard/${dashId}/filter_state/${key}/`,
+    jsonPayload: { value },
+  })
+    .then(r => r.json.message)
+    .catch(err => err);
+
+export const createFilterKey = (dashId: string, value: string) =>
+  SupersetClient.post({
+    endpoint: `api/v1/dashboard/${dashId}/filter_state`,
+    jsonPayload: { value },
+  })
+    .then(r => r.json.key)
+    .catch(err => console.log('err', err));
+
+export const getFilterValue = (
+  dashId: string | number | undefined,
+  key: string,
+) =>
+  SupersetClient.get({
+    endpoint: `api/v1/dashboard/${dashId}/filter_state/${key}/`,
+  })
+    .then(({ json }) => JSON.parse(json.value))
+    .catch(err => err);

Review comment:
       ```suggestion
       .catch(err => {
         logging.error(err);
         return null;
       });
   ```

##########
File path: 
superset-frontend/src/dashboard/components/nativeFilters/FilterBar/index.tsx
##########
@@ -186,23 +191,47 @@ const FilterBar: React.FC<FiltersBarProps> = ({
     [dataMaskSelected, dispatch, setDataMaskSelected, tab],
   );
 
+  const getFilterKeyForUrl = async (value: string) => {
+    let key;
+    try {
+      key = await createFilterKey(dashboardId, value);
+    } catch (err) {
+      return null;
+    }
+    return key;
+  };
+
   const publishDataMask = useCallback(
-    (dataMaskSelected: DataMaskStateWithId) => {
+    async (dataMaskSelected: DataMaskStateWithId) => {
       const { location } = history;
       const { search } = location;
       const previousParams = new URLSearchParams(search);
       const newParams = new URLSearchParams();
-
+      let dataMaskKey = '';

Review comment:
       ```suggestion
         let dataMaskKey: string;
   ```

##########
File path: superset-frontend/src/constants.ts
##########
@@ -37,7 +37,7 @@ export const URL_PARAMS = {
   },
   nativeFilters: {
     name: 'native_filters',
-    type: 'rison',
+    type: 'rison | string',
   },

Review comment:
       ```suggestion
     nativeFilters: {
       name: 'native_filters',
       type: 'rison',
     },
     nativeFiltersByCacheKey: {
       name: 'native_filters',
       type: 'string',
     },
   ```




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

To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org
For additional commands, e-mail: notifications-h...@superset.apache.org

Reply via email to