You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by li...@apache.org on 2023/09/11 14:03:23 UTC

[shenyu-dashboard] branch master updated: Automatically refresh global data after editing plug-in status (#323)

This is an automated email from the ASF dual-hosted git repository.

likeguo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu-dashboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 1aa6a24b Automatically refresh global data after editing plug-in status (#323)
1aa6a24b is described below

commit 1aa6a24bf25476f406c788b3a64dbbf85c8706dc
Author: Kerwin Bryant <ke...@qq.com>
AuthorDate: Mon Sep 11 22:03:18 2023 +0800

    Automatically refresh global data after editing plug-in status (#323)
    
    * Update index.js
    
    * All plugin update locations need to be unified and adjusted to include logic for refreshing global cache.
    
    * Unify the update logic of the plugin
---
 src/models/plugin.js                 |   8 ++-
 src/routes/Plugin/Common/index.js    |  85 ++++++------------------
 src/routes/Plugin/Discovery/index.js |  91 +++++++-------------------
 src/routes/System/Plugin/index.js    | 123 ++++++-----------------------------
 src/utils/plugin.js                  | 105 ++++++++++++++++++++++++++++++
 5 files changed, 172 insertions(+), 240 deletions(-)

diff --git a/src/models/plugin.js b/src/models/plugin.js
index 08b9eb2a..78cdb82e 100644
--- a/src/models/plugin.js
+++ b/src/models/plugin.js
@@ -104,7 +104,9 @@ export default {
       if (json.code === 200) {
         message.success(getIntlContent('SHENYU.COMMON.RESPONSE.UPDATE.SUCCESS'));
         callback();
-        yield put({ type: "reload", fetchValue });
+        if (fetchValue) {
+          yield put({ type: "reload", fetchValue });
+        }
       } else {
         message.warn(json.message);
       }
@@ -115,7 +117,9 @@ export default {
       if(json.code===200){
         message.success(getIntlContent('SHENYU.COMMON.RESPONSE.UPDATE.SUCCESS'));
         callback();
-        yield put({type: "reload", fetchValue});
+        if (fetchValue) {
+          yield put({ type: "reload", fetchValue });
+        }
       } else {
         message.warn(json.message)
       }
diff --git a/src/routes/Plugin/Common/index.js b/src/routes/Plugin/Common/index.js
index 1b52a5ba..1323218d 100644
--- a/src/routes/Plugin/Common/index.js
+++ b/src/routes/Plugin/Common/index.js
@@ -23,7 +23,7 @@ import Selector from "./Selector";
 import Rule from "./Rule";
 import { getIntlContent, getCurrentLocale } from "../../../utils/IntlUtils";
 import AuthButton from "../../../utils/AuthButton";
-import AddModal from "../../System/Plugin/AddModal";
+import { getUpdateModal, updatePluginsEnabled } from "../../../utils/plugin";
 
 const { Search } = Input;
 const { Title } = Typography;
@@ -254,25 +254,12 @@ export default class Common extends Component {
   togglePluginStatus = () => {
     const { dispatch, plugins } = this.props;
     const pluginName = this.props.match.params ? this.props.match.params.id : "";
-    const plugin = this.getPlugin(plugins, pluginName)
-    const { name, id, role, config, sort, file} = plugin;
-    const enabled = !this.state.isPluginEnabled
-    const enabledStr = enabled ? '1' : '0';
-    dispatch({
-      type: "plugin/update",
-      payload: {
-        config,
-        role,
-        name,
-        enabled,
-        id,
-        sort,
-        file
-      },
-      fetchValue: {
-        name: pluginName,
-        enabled: enabledStr
-      },
+    const plugin = this.getPlugin(plugins, pluginName);
+    const enabled = !this.state.isPluginEnabled;
+    updatePluginsEnabled({
+      list: [ plugin.id ],
+      enabled,
+      dispatch,
       callback: () => {
         plugin.enabled = enabled;
         this.setState({ isPluginEnabled: enabled })
@@ -285,53 +272,19 @@ export default class Common extends Component {
     const { dispatch, plugins } = this.props;
     const pluginName = this.props.match.params ? this.props.match.params.id : "";
     const plugin= this.getPlugin(plugins, pluginName);
-    plugin.enabled = this.state.isPluginEnabled;
-    dispatch({
-      type: "plugin/fetchByPluginId",
-      payload: {
-        pluginId: plugin.id,
-        type: "3"
+    getUpdateModal({
+      pluginId: plugin.id,
+      dispatch,
+      callback: (popup) => {
+        this.setState({ popup });
+      },
+      updatedCallback: ({ enabled }) => {
+        this.setState({ isPluginEnabled: enabled });
+        this.closeModal();
+      },
+      canceledCallback: () => {
+        this.closeModal();
       },
-      callback: pluginConfigList => {
-        this.setState({
-          popup: (
-            <AddModal
-              disabled={true}
-              {...plugin}
-              {...pluginConfigList}
-              handleOk={values => {
-              const { name, enabled, id, role, config, sort, file } = values;
-              const enabledStr = enabled ? '1' : '0';
-                dispatch({
-                  type: "plugin/update",
-                  payload: {
-                    config,
-                    role,
-                    name,
-                    enabled,
-                    id,
-                    sort,
-                    file
-                  },
-                  fetchValue: {
-                    name: pluginName,
-                    enabled: enabledStr,
-                    currentPage: 1,
-                    pageSize: 50
-                  },
-                  callback: () => {
-                    this.setState({ isPluginEnabled: enabled })
-                    this.closeModal();
-                  }
-                });
-              }}
-              handleCancel={() => {
-                this.closeModal();
-              }}
-            />
-          )
-        });
-      }
     });
   };
 
diff --git a/src/routes/Plugin/Discovery/index.js b/src/routes/Plugin/Discovery/index.js
index 2958ffa5..68f82e12 100644
--- a/src/routes/Plugin/Discovery/index.js
+++ b/src/routes/Plugin/Discovery/index.js
@@ -23,8 +23,8 @@ import tcpStyles from './tcp.less'
 import DiscoveryConfigModal from "./DiscoveryConfigModal";
 import ProxySelectorModal from "./ProxySelectorModal";
 import {TcpCard} from "./TcpCard";
-import AddModal from "../../System/Plugin/AddModal";
 import AuthButton from "../../../utils/AuthButton";
+import { getUpdateModal, updatePluginsEnabled } from "../../../utils/plugin";
 
 const {Search} = Input;
 const {Title} = Typography;
@@ -130,28 +130,16 @@ export default class TCPProxy extends Component {
   togglePluginStatus = () => {
     const {dispatch, plugins} = this.props;
     const {pluginName} = this.state
-    const {name, id, role, config, sort, file} = this.getPlugin(plugins, pluginName);
-    const enabled = !this.state.isPluginEnabled
-    const enabledStr = enabled ? '1' : '0';
-    dispatch({
-      type: "plugin/update",
-      payload: {
-        config,
-        role,
-        name,
-        enabled,
-        id,
-        sort,
-        file
-      },
-      fetchValue: {
-        name: pluginName,
-        enabled: enabledStr,
-        currentPage: 1,
-        pageSize: 50
-      },
+    const plugin = this.getPlugin(plugins, pluginName);
+    const enabled = !this.state.isPluginEnabled;
+    updatePluginsEnabled({
+      list: [ plugin.id ],
+      enabled,
+      dispatch,
       callback: () => {
-        this.setState({isPluginEnabled: enabled})
+        plugin.enabled = enabled;
+        this.setState({ isPluginEnabled: enabled })
+        this.closeModal();
       }
     });
   }
@@ -244,54 +232,19 @@ export default class TCPProxy extends Component {
     const {dispatch, plugins} = this.props;
     const {pluginName} = this.props;
     const plugin = this.getPlugin(plugins, pluginName);
-    plugin.enabled = this.state.isPluginEnabled;
-    dispatch({
-      type: "plugin/fetchByPluginId",
-      payload: {
-        pluginId: plugin.id,
-        type: "3"
+    getUpdateModal({
+      pluginId: plugin.id,
+      dispatch,
+      callback: (popup) => {
+        this.setState({ popup });
+      },
+      updatedCallback: ({ enabled }) => {
+        this.setState({ isPluginEnabled: enabled });
+        this.closeModal();
+      },
+      canceledCallback: () => {
+        this.closeModal();
       },
-
-      callback: pluginConfigList => {
-        this.setState({
-          popup: (
-            <AddModal
-              disabled={true}
-              {...plugin}
-              {...pluginConfigList}
-              handleOk={values => {
-                const {name, enabled, id, role, config, sort, file} = values;
-                const enabledStr = enabled ? '1' : '0';
-                dispatch({
-                  type: "plugin/update",
-                  payload: {
-                    config,
-                    role,
-                    name,
-                    enabled,
-                    id,
-                    sort,
-                    file
-                  },
-                  fetchValue: {
-                    name: pluginName,
-                    enabled: enabledStr,
-                    currentPage: 1,
-                    pageSize: 50
-                  },
-                  callback: () => {
-                    this.setState({isPluginEnabled: enabled})
-                    this.closeModal();
-                  }
-                });
-              }}
-              handleCancel={() => {
-                this.closeModal();
-              }}
-            />
-          )
-        });
-      }
     });
   };
 
diff --git a/src/routes/System/Plugin/index.js b/src/routes/System/Plugin/index.js
index 20f00f5f..0d77b586 100644
--- a/src/routes/System/Plugin/index.js
+++ b/src/routes/System/Plugin/index.js
@@ -23,7 +23,7 @@ import { resizableComponents } from "../../../utils/resizable";
 import AddModal from "./AddModal";
 import { getCurrentLocale, getIntlContent } from "../../../utils/IntlUtils";
 import AuthButton from "../../../utils/AuthButton";
-import { resetAuthMenuCache } from "../../../utils/AuthRoute";
+import { getUpdateModal, updatePluginsEnabled, refreshGlobalCacheOnUpdated } from "../../../utils/plugin";
 
 const { Text } = Typography;
 
@@ -101,20 +101,6 @@ export default class Plugin extends Component {
     });
   }
 
-  getAllPlugins = page => {
-    const { dispatch } = this.props;
-    const { name, enabled, pageSize } = this.state;
-    dispatch({
-      type: "plugin/fetch",
-      payload: {
-        name,
-        enabled,
-        currentPage: page,
-        pageSize
-      }
-    });
-  };
-
   pageOnchange = page => {
     this.setState({ currentPage: page }, this.query);
   };
@@ -133,54 +119,20 @@ export default class Plugin extends Component {
 
   editClick = record => {
     const { dispatch } = this.props;
-    dispatch({
-      type: "plugin/fetchItem",
-      payload: {
-        id: record.id
+    getUpdateModal({
+      pluginId: record.id,
+      dispatch,
+      fetchValue: this.currentQueryPayload(),
+      callback: (popup) => {
+        this.setState({ popup });
+      },
+      updatedCallback: () => {
+        this.setState({ selectedRowKeys: [] });
+        this.closeModal(true);
+      },
+      canceledCallback: () => {
+        this.closeModal();
       },
-      callback: plugin => {
-        dispatch({
-          type: "plugin/fetchByPluginId",
-          payload: {
-            pluginId: record.id,
-            type: "3"
-          },
-          callback: pluginConfigList => {
-            this.setState({
-              popup: (
-                <AddModal
-                  disabled={true}
-                  {...plugin}
-                  {...pluginConfigList}
-                  handleOk={values => {
-                    const { name, enabled, id, role, config, sort, file } = values;
-                    dispatch({
-                      type: "plugin/update",
-                      payload: {
-                        config,
-                        role,
-                        name,
-                        enabled,
-                        id,
-                        sort,
-                        file
-                      },
-                      fetchValue: this.currentQueryPayload(),
-                      callback: () => {
-                        this.setState({ selectedRowKeys: [] });
-                        this.closeModal(true);
-                      }
-                    });
-                  }}
-                  handleCancel={() => {
-                    this.closeModal();
-                  }}
-                />
-              )
-            });
-          }
-        });
-      }
     });
   };
 
@@ -227,13 +179,7 @@ export default class Plugin extends Component {
         }),
         callback: () => {
           this.setState({ selectedRowKeys: [] });
-          dispatch({
-            type: "global/fetchPlugins",
-            payload: {
-              callback: () => {}
-            }
-          });
-          this.fetchPermissions();
+          refreshGlobalCacheOnUpdated({ dispatch });
         }
       });
     } else {
@@ -263,13 +209,7 @@ export default class Plugin extends Component {
               fetchValue: this.currentQueryPayload(),
               callback: () => {
                 this.closeModal(true);
-                dispatch({
-                  type: "global/fetchPlugins",
-                  payload: {
-                    callback: () => {}
-                  }
-                });
-                this.fetchPermissions();
+                refreshGlobalCacheOnUpdated({ dispatch });
               }
             });
           }}
@@ -281,18 +221,6 @@ export default class Plugin extends Component {
     });
   };
 
-  fetchPermissions = () => {
-    const { dispatch } = this.props;
-    dispatch({
-      type: "global/refreshPermission",
-      payload: {
-        callback: () => {
-          resetAuthMenuCache();
-        }
-      }
-    });
-  };
-
   // 批量启用或禁用
   enableClick = () => {
     const { dispatch } = this.props;
@@ -304,12 +232,10 @@ export default class Plugin extends Component {
           id: selectedRowKeys[0]
         },
         callback: user => {
-          dispatch({
-            type: "plugin/updateEn",
-            payload: {
-              list: selectedRowKeys,
-              enabled: !user.enabled
-            },
+          updatePluginsEnabled({
+            list: selectedRowKeys,
+            enabled: !user.enabled,
+            dispatch,
             fetchValue: this.currentQueryPayload(),
             callback: () => {
               this.setState({ selectedRowKeys: [] });
@@ -331,15 +257,6 @@ export default class Plugin extends Component {
     });
   };
 
-  operateChange = (checked, record) => {
-    const { dispatch } = this.props;
-    const { id } = record;
-    dispatch({
-      type: "plugin/changeStatus",
-      payload: { id, enabled: checked }
-    });
-  };
-
   changeLocale(locale) {
     this.setState({
       localeName: locale
diff --git a/src/utils/plugin.js b/src/utils/plugin.js
new file mode 100644
index 00000000..7b210e2e
--- /dev/null
+++ b/src/utils/plugin.js
@@ -0,0 +1,105 @@
+/*
+ * 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 React from 'react';
+import { resetAuthMenuCache } from "./AuthRoute";
+import AddModal from "../routes/System/Plugin/AddModal";
+
+export function refreshGlobalCacheOnUpdated({ dispatch }) {
+  dispatch({
+    type: "global/fetchPlugins",
+    payload: {
+      callback: () => {}
+    }
+  });
+
+  dispatch({
+    type: "global/refreshPermission",
+    payload: {
+      callback: () => {
+        resetAuthMenuCache();
+      }
+    }
+  });
+}
+
+export function getUpdateModal({ pluginId, dispatch, fetchValue, callback, updatedCallback, canceledCallback }) {
+  dispatch({
+    type: "plugin/fetchItem",
+    payload: {
+      id: pluginId
+    },
+    callback: plugin => {
+      dispatch({
+        type: "plugin/fetchByPluginId",
+        payload: {
+          pluginId: plugin.id,
+          type: "3"
+        },
+        callback: pluginConfigList => {
+          callback(
+            <AddModal
+              disabled={true}
+              {...plugin}
+              {...pluginConfigList}
+              handleOk={values => {
+                  const { name, enabled, id, role, config, sort, file } = values;
+                  dispatch({
+                    type: "plugin/update",
+                    payload: {
+                      config,
+                      role,
+                      name,
+                      enabled,
+                      id,
+                      sort,
+                      file
+                    },
+                    fetchValue,
+                    callback: () => {
+                      if (updatedCallback) {
+                        updatedCallback(values);
+                      }
+                      refreshGlobalCacheOnUpdated({ dispatch });
+                    }
+                  });
+                }}
+              handleCancel={canceledCallback}
+            />
+          );
+        }
+      });
+    }
+  });
+}
+
+export function updatePluginsEnabled({ list, enabled, dispatch, fetchValue, callback }) {
+  dispatch({
+    type: "plugin/updateEn",
+    payload: {
+      list,
+      enabled
+    },
+    fetchValue,
+    callback: () => {
+      if (callback) {
+        callback();
+      }
+      refreshGlobalCacheOnUpdated({ dispatch });
+    }
+  });
+}