From b94a9c19e74fd20aff6a8066ad2f193a9202ef6d Mon Sep 17 00:00:00 2001 From: Jake McDermott Date: Mon, 3 May 2021 11:11:58 -0400 Subject: [PATCH] Avoid prop reference error when recreating survey The survey object is undefined when recreating a survey after deleting it. Add optional chaining on survey fields to avoid prop reference error. --- .../src/screens/Template/Survey/SurveyQuestionAdd.jsx | 6 ++---- awx/ui_next/src/screens/Template/TemplateSurvey.jsx | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.jsx b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.jsx index f7a7c9d289..96c0e9eb35 100644 --- a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.jsx +++ b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.jsx @@ -11,7 +11,7 @@ export default function SurveyQuestionAdd({ survey, updateSurvey }) { const handleSubmit = async question => { const formData = { ...question }; try { - if (survey.spec?.some(q => q.variable === formData.variable)) { + if (survey?.spec?.some(q => q.variable === formData.variable)) { setFormError( new Error( `Survey already contains a question with variable named “${formData.variable}”` @@ -41,9 +41,7 @@ export default function SurveyQuestionAdd({ survey, updateSurvey }) { formData.choices = choices.trim(); } delete formData.formattedChoices; - - const newSpec = survey.spec ? survey.spec.concat(formData) : [formData]; - + const newSpec = survey?.spec ? survey.spec.concat(formData) : [formData]; await updateSurvey(newSpec); history.push(match.url.replace('/add', '')); } catch (err) { diff --git a/awx/ui_next/src/screens/Template/TemplateSurvey.jsx b/awx/ui_next/src/screens/Template/TemplateSurvey.jsx index 60531837b9..e7821090be 100644 --- a/awx/ui_next/src/screens/Template/TemplateSurvey.jsx +++ b/awx/ui_next/src/screens/Template/TemplateSurvey.jsx @@ -53,8 +53,8 @@ function TemplateSurvey({ template, canEdit }) { ); const updateSurveySpec = spec => { updateSurvey({ - name: survey.name || '', - description: survey.description || '', + name: survey?.name || '', + description: survey?.description || '', spec, }); };