Просмотр исходного кода

Calculate the initial value in advance

Taichi Masuyama 4 лет назад
Родитель
Сommit
da1723aac6

+ 19 - 21
packages/app/src/components/Admin/SlackIntegration/ManageCommandsProcess.jsx

@@ -33,28 +33,26 @@ const ManageCommandsProcess = ({
     create: false,
     togetter: [],
   });
-  const getInitialCurrentPermissionTypes = () => {
-    const getPermissionTypeFromValue = (value) => {
-      if (Array.isArray(value)) {
-        return PermissionTypes.ALLOW_SPECIFIED;
-      }
-      if (typeof value === 'boolean') {
-        return value ? PermissionTypes.ALLOW_ALL : PermissionTypes.DENY_ALL;
-      }
-      logger.error('The value type must be boolean or string[]');
-    };
-    const initialValue = {};
-    Object.entries(permissionsForBroadcastUseCommandsState).forEach((entry) => {
-      const [commandName, value] = entry;
-      initialValue[commandName] = getPermissionTypeFromValue(value);
-    });
-    Object.entries(permissionsForSingleUseCommandsState).forEach((entry) => {
-      const [commandName, value] = entry;
-      initialValue[commandName] = getPermissionTypeFromValue(value);
-    });
-    return initialValue;
+
+  const getPermissionTypeFromValue = (value) => {
+    if (Array.isArray(value)) {
+      return PermissionTypes.ALLOW_SPECIFIED;
+    }
+    if (typeof value === 'boolean') {
+      return value ? PermissionTypes.ALLOW_ALL : PermissionTypes.DENY_ALL;
+    }
+    logger.error('The value type must be boolean or string[]');
   };
-  const [currentPermissionTypes, setCurrentPermissionTypes] = useState(getInitialCurrentPermissionTypes());
+  const initialValue = {};
+  Object.entries(permissionsForBroadcastUseCommandsState).forEach((entry) => {
+    const [commandName, value] = entry;
+    initialValue[commandName] = getPermissionTypeFromValue(value);
+  });
+  Object.entries(permissionsForSingleUseCommandsState).forEach((entry) => {
+    const [commandName, value] = entry;
+    initialValue[commandName] = getPermissionTypeFromValue(value);
+  });
+  const [currentPermissionTypes, setCurrentPermissionTypes] = useState(initialValue);
 
   // returns new state
   const getUpdatedPermissionSettings = (prevState, commandName, value) => {