Sha256: 0923645d1d9525d5ee683cb56d871fbfdbeb52d78c6f7305bf91e56d7bfced62

Contents?: true

Size: 1.72 KB

Versions: 24

Compression:

Stored size: 1.72 KB

Contents

import { useState, useEffect } from 'react';
import { useSelector } from 'react-redux';
import {
  selectTemplateInputs,
  selectAdvancedTemplateInputs,
} from './JobWizardSelectors';
import { isPositiveNumber, isValidDate } from './steps/form/FormHelpers';
import './JobWizard.scss';

export const useValidation = ({ advancedValues, templateValues }) => {
  const [valid, setValid] = useState({});
  const templateInputs = useSelector(selectTemplateInputs);
  const advancedTemplateInputs = useSelector(selectAdvancedTemplateInputs);
  useEffect(() => {
    setValid({
      hostsAndInputs: true,
      advanced: true,
      schedule: true,
    });
    const inputValidation = (inputs, values, setInvalid) => {
      inputs.forEach(({ name, required, value_type: valueType }) => {
        const value = values[name];
        if (required && !value) {
          setInvalid();
        }
        if (value && valueType === 'date') {
          if (!isValidDate(value) && !isValidDate(new Date(value))) {
            setInvalid();
          }
        }
      });
    };
    inputValidation(templateInputs, templateValues, () =>
      setValid(currValid => ({ ...currValid, hostsAndInputs: false }))
    );

    inputValidation(advancedTemplateInputs, advancedValues.templateValues, () =>
      setValid(currValid => ({ ...currValid, advanced: false }))
    );
    [
      advancedValues.timeoutToKill,
      advancedValues.timeToPickup,
      advancedValues.concurrencyLevel,
    ].forEach(value => {
      if (value && !isPositiveNumber(value)) {
        setValid(currValid => ({ ...currValid, advanced: false }));
      }
    });

    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [advancedValues, templateValues]);
  return [valid, setValid];
};

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
foreman_remote_execution-14.1.2 webpack/JobWizard/validation.js
foreman_remote_execution-14.1.1 webpack/JobWizard/validation.js
foreman_remote_execution-14.1.0 webpack/JobWizard/validation.js
foreman_remote_execution-14.0.2 webpack/JobWizard/validation.js
foreman_remote_execution-14.0.1 webpack/JobWizard/validation.js
foreman_remote_execution-13.2.6 webpack/JobWizard/validation.js
foreman_remote_execution-14.0.0 webpack/JobWizard/validation.js
foreman_remote_execution-13.2.5 webpack/JobWizard/validation.js
foreman_remote_execution-13.2.4 webpack/JobWizard/validation.js
foreman_remote_execution-13.2.3 webpack/JobWizard/validation.js
foreman_remote_execution-13.2.2 webpack/JobWizard/validation.js
foreman_remote_execution-12.0.7 webpack/JobWizard/validation.js
foreman_remote_execution-13.2.1 webpack/JobWizard/validation.js
foreman_remote_execution-13.2.0 webpack/JobWizard/validation.js
foreman_remote_execution-13.0.0 webpack/JobWizard/validation.js
foreman_remote_execution-12.0.5 webpack/JobWizard/validation.js
foreman_remote_execution-12.0.4 webpack/JobWizard/validation.js
foreman_remote_execution-12.0.1 webpack/JobWizard/validation.js
foreman_remote_execution-12.0.2 webpack/JobWizard/validation.js
foreman_remote_execution-11.1.3 webpack/JobWizard/validation.js