From 91d4948564b45d564ed05b03e29a46be2724ccfc Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Fri, 24 Apr 2020 10:05:51 -0700 Subject: [PATCH] use PromptDetail in launch prompt preview --- .../components/ErrorDetail/getErrorMessage.js | 2 +- .../components/LaunchPrompt/LaunchPrompt.jsx | 35 +++++++++++++++++-- .../components/LaunchPrompt/PreviewStep.jsx | 17 +++++++-- .../components/LaunchPrompt/SurveyStep.jsx | 20 ++--------- .../components/LaunchPrompt/mergeExtraVars.js | 5 +++ .../components/PromptDetail/PromptDetail.jsx | 17 +++++++-- 6 files changed, 69 insertions(+), 27 deletions(-) diff --git a/awx/ui_next/src/components/ErrorDetail/getErrorMessage.js b/awx/ui_next/src/components/ErrorDetail/getErrorMessage.js index ca6fbb23be..1c2450a64d 100644 --- a/awx/ui_next/src/components/ErrorDetail/getErrorMessage.js +++ b/awx/ui_next/src/components/ErrorDetail/getErrorMessage.js @@ -1,5 +1,5 @@ export default function getErrorMessage(response) { - if (!response.data) { + if (!response?.data) { return null; } if (typeof response.data === 'string') { diff --git a/awx/ui_next/src/components/LaunchPrompt/LaunchPrompt.jsx b/awx/ui_next/src/components/LaunchPrompt/LaunchPrompt.jsx index 521be1001b..2db38bd918 100644 --- a/awx/ui_next/src/components/LaunchPrompt/LaunchPrompt.jsx +++ b/awx/ui_next/src/components/LaunchPrompt/LaunchPrompt.jsx @@ -1,8 +1,11 @@ -import React from 'react'; +import React, { useCallback, useEffect } from 'react'; import { Wizard } from '@patternfly/react-core'; import { withI18n } from '@lingui/react'; import { t } from '@lingui/macro'; import { Formik } from 'formik'; +import { JobTemplatesAPI, WorkflowJobTemplatesAPI } from '@api'; +import useRequest from '@util/useRequest'; +import ContentError from '@components/ContentError'; import InventoryStep from './InventoryStep'; import CredentialsStep from './CredentialsStep'; import OtherPromptsStep from './OtherPromptsStep'; @@ -11,6 +14,30 @@ import PreviewStep from './PreviewStep'; import mergeExtraVars from './mergeExtraVars'; function LaunchPrompt({ config, resource, onLaunch, onCancel, i18n }) { + const { + result: survey, + request: fetchSurvey, + error: surveyError, + } = useRequest( + useCallback(async () => { + if (!config.survey_enabled) { + return {}; + } + const { data } = + resource.type === 'workflow_job_template' + ? await WorkflowJobTemplatesAPI.readSurvey(resource.id) + : await JobTemplatesAPI.readSurvey(resource.id); + return data; + }, [config.survey_enabled, resource]) + ); + useEffect(() => { + fetchSurvey(); + }, [fetchSurvey]); + + if (surveyError) { + return ; + } + const steps = []; const initialValues = {}; if (config.ask_inventory_on_launch) { @@ -73,12 +100,14 @@ function LaunchPrompt({ config, resource, onLaunch, onCancel, i18n }) { initialValues.survey = {}; steps.push({ name: i18n._(t`Survey`), - component: , + component: , }); } steps.push({ name: i18n._(t`Preview`), - component: , + component: ( + + ), nextButtonText: i18n._(t`Launch`), }); diff --git a/awx/ui_next/src/components/LaunchPrompt/PreviewStep.jsx b/awx/ui_next/src/components/LaunchPrompt/PreviewStep.jsx index b681a402bf..711a52ee02 100644 --- a/awx/ui_next/src/components/LaunchPrompt/PreviewStep.jsx +++ b/awx/ui_next/src/components/LaunchPrompt/PreviewStep.jsx @@ -1,7 +1,20 @@ import React from 'react'; +import { useFormikContext } from 'formik'; +import PromptDetail from '@components/PromptDetail'; +import { encodeExtraVars } from './mergeExtraVars'; -function PreviewStep() { - return
Preview of selected values will appear here
; +function PreviewStep({ resource, config, survey }) { + const { values } = useFormikContext(); + return ( + + ); } export default PreviewStep; diff --git a/awx/ui_next/src/components/LaunchPrompt/SurveyStep.jsx b/awx/ui_next/src/components/LaunchPrompt/SurveyStep.jsx index c7b7d9da64..5cd158ad59 100644 --- a/awx/ui_next/src/components/LaunchPrompt/SurveyStep.jsx +++ b/awx/ui_next/src/components/LaunchPrompt/SurveyStep.jsx @@ -23,24 +23,8 @@ import { combine, } from '@util/validators'; -function SurveyStep({ template, i18n }) { - const { result: survey, request: fetchSurvey, isLoading, error } = useRequest( - useCallback(async () => { - const { data } = - template.type === 'workflow_job_template' - ? await WorkflowJobTemplatesAPI.readSurvey(template.id) - : await JobTemplatesAPI.readSurvey(template.id); - return data; - }, [template]) - ); - useEffect(() => { - fetchSurvey(); - }, [fetchSurvey]); - - if (error) { - return ; - } - if (isLoading || !survey) { +function SurveyStep({ survey, i18n }) { + if (!survey) { return ; } diff --git a/awx/ui_next/src/components/LaunchPrompt/mergeExtraVars.js b/awx/ui_next/src/components/LaunchPrompt/mergeExtraVars.js index f324f23f6b..681dc8a059 100644 --- a/awx/ui_next/src/components/LaunchPrompt/mergeExtraVars.js +++ b/awx/ui_next/src/components/LaunchPrompt/mergeExtraVars.js @@ -9,3 +9,8 @@ export default function mergeExtraVars(extraVars, survey = {}) { } // TODO: "safe" version that obscures passwords for preview step + +export function encodeExtraVars(extraVars, survey = {}) { + const vars = mergeExtraVars(extraVars, survey); + return yaml.safeDump(vars); +} diff --git a/awx/ui_next/src/components/PromptDetail/PromptDetail.jsx b/awx/ui_next/src/components/PromptDetail/PromptDetail.jsx index 05ff230175..b9d0011f3f 100644 --- a/awx/ui_next/src/components/PromptDetail/PromptDetail.jsx +++ b/awx/ui_next/src/components/PromptDetail/PromptDetail.jsx @@ -78,7 +78,7 @@ function omitOverrides(resource, overrides) { // TODO: When prompting is hooked up, update function // to filter based on prompt overrides -function partitionPromptDetails(resource, launchConfig) { +function partitionPromptDetails(resource, userResponses, launchConfig) { const { defaults = {} } = launchConfig; const overrides = {}; @@ -150,7 +150,12 @@ function partitionPromptDetails(resource, launchConfig) { return [withoutOverrides, overrides]; } -function PromptDetail({ i18n, resource, launchConfig = {} }) { +function PromptDetail({ + i18n, + resource, + launchConfig = {}, + promptResponses = {}, +}) { const VERBOSITY = { 0: i18n._(t`0 (Normal)`), 1: i18n._(t`1 (Verbose)`), @@ -159,7 +164,13 @@ function PromptDetail({ i18n, resource, launchConfig = {} }) { 4: i18n._(t`4 (Connection Debug)`), }; - const [details, overrides] = partitionPromptDetails(resource, launchConfig); + // const [details, overrides] = partitionPromptDetails( + // resource, + // promptResponses, + // launchConfig + // ); + const overrides = promptResponses; + const details = omitOverrides(resource, overrides); const hasOverrides = Object.keys(overrides).length > 0; return (