From 1ac92b0493409db78a0552f5480ade0319f791ab Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Mon, 4 May 2020 15:57:32 -0700 Subject: [PATCH] add rough jt launch prompt validation --- .../components/LaunchPrompt/LaunchPrompt.jsx | 22 +++++++++------- .../src/components/LaunchPrompt/hooks.js | 26 ++++++++++++------- .../LaunchPrompt/steps/useCredentialsStep.jsx | 9 +++++++ .../LaunchPrompt/steps/useInventoryStep.jsx | 9 +++++++ .../steps/useOtherPromptsStep.jsx | 9 +++++++ .../LaunchPrompt/steps/useSurveyStep.jsx | 20 ++++++++++++++ 6 files changed, 76 insertions(+), 19 deletions(-) diff --git a/awx/ui_next/src/components/LaunchPrompt/LaunchPrompt.jsx b/awx/ui_next/src/components/LaunchPrompt/LaunchPrompt.jsx index bc77a30555..1134d20ff0 100644 --- a/awx/ui_next/src/components/LaunchPrompt/LaunchPrompt.jsx +++ b/awx/ui_next/src/components/LaunchPrompt/LaunchPrompt.jsx @@ -18,11 +18,15 @@ import mergeExtraVars from './mergeExtraVars'; import { useSteps, useVisitedSteps } from './hooks'; function LaunchPrompt({ config, resource, onLaunch, onCancel, i18n }) { - const { steps, initialValues, isReady, contentError } = useSteps( - config, - resource, - i18n - ); + // const [formErrors, setFormErrors] = useState({}); + const { + steps, + initialValues, + isReady, + validate, + formErrors, + contentError, + } = useSteps(config, resource, i18n); const [visitedSteps, visitStep] = useVisitedSteps(config); if (contentError) { @@ -33,10 +37,10 @@ function LaunchPrompt({ config, resource, onLaunch, onCancel, i18n }) { } // TODO move into hook? - const validate = values => { - // return {}; - return { limit: ['required field'] }; - }; + // const validate = values => { + // // return {}; + // return { limit: ['required field'] }; + // }; // TODO move into hook? const submit = values => { diff --git a/awx/ui_next/src/components/LaunchPrompt/hooks.js b/awx/ui_next/src/components/LaunchPrompt/hooks.js index 5248c588bc..872133f782 100644 --- a/awx/ui_next/src/components/LaunchPrompt/hooks.js +++ b/awx/ui_next/src/components/LaunchPrompt/hooks.js @@ -5,16 +5,8 @@ import useOtherPromptsStep from './steps/useOtherPromptsStep'; import useSurveyStep from './steps/useSurveyStep'; import usePreviewStep from './steps/usePreviewStep'; -// const INVENTORY = 'inventory'; -// const CREDENTIALS = 'credentials'; -// const PASSWORDS = 'passwords'; -// const OTHER_PROMPTS = 'other'; -// const SURVEY = 'survey'; -// const PREVIEW = 'preview'; - export function useSteps(config, resource, i18n) { - // TODO pass in form errors? - const formErrors = {}; + const [formErrors, setFormErrors] = useState({}); const inventory = useInventoryStep(config, resource, i18n); const credentials = useCredentialsStep(config, resource, i18n); const otherPrompts = useOtherPromptsStep(config, resource, i18n); @@ -54,7 +46,21 @@ export function useSteps(config, resource, i18n) { survey.error || preview.error; - return { steps, initialValues, isReady, contentError }; + const validate = values => { + const errors = { + ...inventory.validate(values), + ...credentials.validate(values), + ...otherPrompts.validate(values), + ...survey.validate(values), + }; + setFormErrors(errors); + if (Object.keys(errors).length) { + return errors; + } + return false; + }; + + return { steps, initialValues, isReady, validate, formErrors, contentError }; } export function usePromptErrors(config) { diff --git a/awx/ui_next/src/components/LaunchPrompt/steps/useCredentialsStep.jsx b/awx/ui_next/src/components/LaunchPrompt/steps/useCredentialsStep.jsx index 9fb9d88ecd..a8d5d0053b 100644 --- a/awx/ui_next/src/components/LaunchPrompt/steps/useCredentialsStep.jsx +++ b/awx/ui_next/src/components/LaunchPrompt/steps/useCredentialsStep.jsx @@ -5,9 +5,18 @@ import CredentialsStep from './CredentialsStep'; const STEP_ID = 'credentials'; export default function useCredentialsStep(config, resource, i18n) { + const validate = values => { + const errors = {}; + if (!values.credentials || !values.credentials.length) { + errors.credentials = i18n._(t`Credentials must be selected`); + } + return errors; + }; + return { step: getStep(config, i18n), initialValues: getInitialValues(config, resource), + validate, isReady: true, error: null, }; diff --git a/awx/ui_next/src/components/LaunchPrompt/steps/useInventoryStep.jsx b/awx/ui_next/src/components/LaunchPrompt/steps/useInventoryStep.jsx index 7aaed7d8e3..91988f83d0 100644 --- a/awx/ui_next/src/components/LaunchPrompt/steps/useInventoryStep.jsx +++ b/awx/ui_next/src/components/LaunchPrompt/steps/useInventoryStep.jsx @@ -5,9 +5,18 @@ import InventoryStep from './InventoryStep'; const STEP_ID = 'inventory'; export default function useInventoryStep(config, resource, i18n) { + const validate = values => { + const errors = {}; + if (!values.inventory) { + errors.inventory = i18n._(t`An inventory must be selected`); + } + return errors; + }; + return { step: getStep(config, i18n), initialValues: getInitialValues(config, resource), + validate, isReady: true, error: null, }; diff --git a/awx/ui_next/src/components/LaunchPrompt/steps/useOtherPromptsStep.jsx b/awx/ui_next/src/components/LaunchPrompt/steps/useOtherPromptsStep.jsx index 7b5f5d2cce..1977070e28 100644 --- a/awx/ui_next/src/components/LaunchPrompt/steps/useOtherPromptsStep.jsx +++ b/awx/ui_next/src/components/LaunchPrompt/steps/useOtherPromptsStep.jsx @@ -5,9 +5,18 @@ import OtherPromptsStep from './OtherPromptsStep'; const STEP_ID = 'other'; export default function useOtherPrompt(config, resource, i18n) { + const validate = values => { + const errors = {}; + if (config.ask_job_type_on_launch && !values.job_type) { + errors.job_type = i18n._(t`This field must not be blank`); + } + return errors; + }; + return { step: getStep(config, i18n), initialValues: getInitialValues(config, resource), + validate, isReady: true, error: null, }; diff --git a/awx/ui_next/src/components/LaunchPrompt/steps/useSurveyStep.jsx b/awx/ui_next/src/components/LaunchPrompt/steps/useSurveyStep.jsx index 5383e629ba..6e52748ee9 100644 --- a/awx/ui_next/src/components/LaunchPrompt/steps/useSurveyStep.jsx +++ b/awx/ui_next/src/components/LaunchPrompt/steps/useSurveyStep.jsx @@ -27,6 +27,7 @@ export default function useSurveyStep(config, resource, i18n) { return { step: getStep(config, survey, i18n), initialValues: getInitialValues(config, survey), + validate: getValidate(config, survey, i18n), survey, isReady: !isLoading && !!survey, error, @@ -58,3 +59,22 @@ function getInitialValues(config, survey) { }); return values; } + +function getValidate(config, survey, i18n) { + return values => { + if (!config.survey_enabled || !survey || !survey.spec) { + return {}; + } + const errors = {}; + survey.spec.forEach(question => { + // TODO validate min/max + // TODO allow 0 + if (question.required && !values[question.variable]) { + errors[`survey_${question.variable}`] = i18n._( + t`This field must not be blank` + ); + } + }); + return errors; + }; +}