Преглед изворни кода

refactor validation, init properly

Ivan Galakhov пре 4 година
родитељ
комит
4f9e044abc

+ 41 - 12
dashboard/src/components/porter-form/PorterFormContextProvider.tsx

@@ -58,12 +58,15 @@ export const PorterFormContextProvider: React.FC<Props> = (props) => {
               ...state.components,
               ...state.components,
               [action.id]: {
               [action.id]: {
                 state: action.initValue,
                 state: action.initValue,
-                validation: {
-                  ...{
-                    validated: false,
-                  },
-                  ...action.initValidation,
+              },
+            },
+            validation: {
+              ...state.validation,
+              [action.id]: {
+                ...{
+                  validated: false,
                 },
                 },
+                ...action.initValidation,
               },
               },
             },
             },
           };
           };
@@ -94,9 +97,12 @@ export const PorterFormContextProvider: React.FC<Props> = (props) => {
             ...state.components,
             ...state.components,
             [action.id]: {
             [action.id]: {
               ...state.components[action.id],
               ...state.components[action.id],
-              validation: action.updateFunc(
-                state.components[action.id].validation
-              ),
+            },
+          },
+          validation: {
+            ...state.validation,
+            [action.id]: {
+              ...action.updateFunc(state.validation[action.id]),
             },
             },
           },
           },
         };
         };
@@ -128,8 +134,33 @@ export const PorterFormContextProvider: React.FC<Props> = (props) => {
     return ret;
     return ret;
   };
   };
 
 
+  const getInitialValidation = (data: PorterFormData) => {
+    const ret: Record<string, any> = {};
+    data?.tabs?.map((tab, i) =>
+      tab.sections?.map((section, j) =>
+        section.contents?.map((field, k) => {
+          if (
+            field.type == "heading" ||
+            field.type == "subtitle" ||
+            field.type == "resource-list" ||
+            field.type == "service-ip-list" ||
+            field.type == "velero-create-backup"
+          )
+            return;
+          if (field.required && (field.settings?.default || field.value)) {
+            ret[`${i}-${j}-${k}`] = {
+              validated: true,
+            };
+          }
+        })
+      )
+    );
+    return ret;
+  };
+
   const [state, dispatch] = useReducer(handleAction, {
   const [state, dispatch] = useReducer(handleAction, {
     components: {},
     components: {},
+    validation: getInitialValidation(props.rawFormData),
     variables: {
     variables: {
       ...props.initialVariables,
       ...props.initialVariables,
       ...getInitialVariables(props.rawFormData),
       ...getInitialVariables(props.rawFormData),
@@ -310,7 +341,7 @@ export const PorterFormContextProvider: React.FC<Props> = (props) => {
             return;
             return;
           // fields that have defaults can't be required since we can always
           // fields that have defaults can't be required since we can always
           // compute their value
           // compute their value
-          if (field.required && !field.settings?.default && !field.value) {
+          if (field.required) {
             requiredIds.push(field.id);
             requiredIds.push(field.id);
           }
           }
           if (!mapping[field.variable]) {
           if (!mapping[field.variable]) {
@@ -327,9 +358,7 @@ export const PorterFormContextProvider: React.FC<Props> = (props) => {
     Validate the form based on a list of required ids
     Validate the form based on a list of required ids
    */
    */
   const doValidation = (requiredIds: string[]) =>
   const doValidation = (requiredIds: string[]) =>
-    requiredIds
-      ?.map((id) => state.components[id]?.validation.validated)
-      .every((x) => x);
+    requiredIds?.map((id) => state.validation[id]?.validated).every((x) => x);
 
 
   const formData = computeFormStructure(
   const formData = computeFormStructure(
     restructureToNewFields(props.rawFormData),
     restructureToNewFields(props.rawFormData),

+ 17 - 12
dashboard/src/components/porter-form/field-components/Input.tsx

@@ -8,6 +8,15 @@ import {
   StringInputFieldState,
   StringInputFieldState,
 } from "../types";
 } from "../types";
 
 
+const clipOffUnit = (unit: string, x: string) => {
+  if (typeof x === "string" && unit) {
+    return unit === x.slice(x.length - unit.length, x.length)
+      ? x.slice(0, x.length - unit.length)
+      : x;
+  }
+  return x;
+};
+
 const Input: React.FC<InputField> = ({
 const Input: React.FC<InputField> = ({
   id,
   id,
   variable,
   variable,
@@ -19,16 +28,6 @@ const Input: React.FC<InputField> = ({
   isReadOnly,
   isReadOnly,
   value,
   value,
 }) => {
 }) => {
-  const clipOffUnit = (x: string) => {
-    let unit = settings?.unit;
-    if (typeof x === "string" && unit) {
-      return unit === x.slice(x.length - unit.length, x.length)
-        ? x.slice(0, x.length - unit.length)
-        : x;
-    }
-    return x;
-  };
-
   const {
   const {
     state,
     state,
     variables,
     variables,
@@ -41,7 +40,9 @@ const Input: React.FC<InputField> = ({
         : settings?.default != undefined,
         : settings?.default != undefined,
     },
     },
     initVars: {
     initVars: {
-      [variable]: value ? clipOffUnit(value[0]) : settings?.default,
+      [variable]: value
+        ? clipOffUnit(settings?.unit, value[0])
+        : settings?.default,
     },
     },
   });
   });
 
 
@@ -92,7 +93,11 @@ export const getFinalVariablesForStringInput: GetFinalVariablesFunction = (
   vars,
   vars,
   props: InputField
   props: InputField
 ) => {
 ) => {
-  const val = vars[props.variable] || props.settings?.default;
+  const val =
+    vars[props.variable] ||
+    (props.value
+      ? clipOffUnit(props.settings?.unit, props.value[0])
+      : props.settings?.default);
   return {
   return {
     [props.variable]:
     [props.variable]:
       props.settings?.unit && !props.settings.omitUnitFromValue
       props.settings?.unit && !props.settings.omitUnitFromValue

+ 1 - 2
dashboard/src/components/porter-form/field-components/KeyValueArray.tsx

@@ -138,10 +138,9 @@ const KeyValueArray: React.FC<Props> = (props) => {
       obj[key] = value;
       obj[key] = value;
     });
     });
     return obj;
     return obj;
-  }
+  };
 
 
   const renderEnvModal = () => {
   const renderEnvModal = () => {
-    console.log(state.values)
     if (state.showEnvModal) {
     if (state.showEnvModal) {
       return (
       return (
         <Modal
         <Modal

+ 3 - 1
dashboard/src/components/porter-form/types.ts

@@ -194,9 +194,11 @@ export interface PorterFormState {
   components: {
   components: {
     [key: string]: {
     [key: string]: {
       state: PorterFormFieldFieldState;
       state: PorterFormFieldFieldState;
-      validation: PorterFormFieldValidationState;
     };
     };
   };
   };
+  validation: {
+    [key: string]: PorterFormFieldValidationState;
+  }
   variables: PorterFormVariableList;
   variables: PorterFormVariableList;
 }
 }