diff --git a/awx/ui_next/src/screens/Template/TemplateSurvey.jsx b/awx/ui_next/src/screens/Template/TemplateSurvey.jsx index 7ffe646c6c..ae735bd3fb 100644 --- a/awx/ui_next/src/screens/Template/TemplateSurvey.jsx +++ b/awx/ui_next/src/screens/Template/TemplateSurvey.jsx @@ -9,6 +9,7 @@ import ErrorDetail from '@components/ErrorDetail'; import useRequest, { useDismissableError } from '@util/useRequest'; import SurveyList from './shared/SurveyList'; import SurveyQuestionAdd from './shared/SurveyQuestionAdd'; +import SurveyQuestionEdit from './shared/SurveyQuestionEdit'; function TemplateSurvey({ template, i18n }) { const [surveyEnabled, setSurveyEnabled] = useState(template.survey_enabled); @@ -74,6 +75,9 @@ function TemplateSurvey({ template, i18n }) { + + + - {question.question_name} - , - {question.type}, - - {question.default} + + + {question.question_name} + , + {question.type}, + {question.default}, ]} /> diff --git a/awx/ui_next/src/screens/Template/shared/SurveyQuestionEdit.jsx b/awx/ui_next/src/screens/Template/shared/SurveyQuestionEdit.jsx new file mode 100644 index 0000000000..f6f391fb13 --- /dev/null +++ b/awx/ui_next/src/screens/Template/shared/SurveyQuestionEdit.jsx @@ -0,0 +1,63 @@ +import React, { useState } from 'react'; +import { useHistory, useRouteMatch } from 'react-router-dom'; +import ContentLoading from '@components/ContentLoading'; +import { CardBody } from '@components/Card'; +import SurveyQuestionForm from './SurveyQuestionForm'; + +export default function SurveyQuestionEdit({ survey, updateSurvey }) { + const [formError, setFormError] = useState(null); + const history = useHistory(); + const match = useRouteMatch(); + + if (!survey) { + return ; + } + + const question = survey.spec.find(q => q.variable === match.params.variable); + + const navigateToList = () => { + const index = match.url.indexOf('/edit'); + history.push(match.url.substr(0, index)); + }; + + const handleSubmit = async formData => { + if ( + formData.variable !== question.variable && + survey.spec.find(q => q.variable === formData.variable) + ) { + debugger; + setFormError( + new Error(`Survey already contains a question with variable named + “${formData.variable}”`) + ); + return; + } + try { + const questionIndex = survey.spec.findIndex( + q => q.variable === match.params.variable + ); + if (questionIndex === -1) { + throw new Error('Question not found in spec'); + } + await updateSurvey([ + ...survey.spec.slice(0, questionIndex), + formData, + ...survey.spec.slice(questionIndex + 1), + ]); + navigateToList(); + } catch (err) { + setFormError(err); + } + }; + + return ( + + + + ); +} diff --git a/awx/ui_next/src/screens/Template/shared/SurveyQuestionForm.jsx b/awx/ui_next/src/screens/Template/shared/SurveyQuestionForm.jsx index 1c9e4ba37e..541115d8a4 100644 --- a/awx/ui_next/src/screens/Template/shared/SurveyQuestionForm.jsx +++ b/awx/ui_next/src/screens/Template/shared/SurveyQuestionForm.jsx @@ -76,8 +76,8 @@ function SurveyQuestionForm({ variable: question?.variable || '', min: question?.min || 0, max: question?.max || 1024, - default: question?.default, - choices: question?.choices, + default: question?.default || '', + choices: question?.choices || '', new_question: !question, }} onSubmit={handleSubmit}