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.
This commit is contained in:
Jake McDermott 2021-05-03 11:11:58 -04:00
parent 1c73407edf
commit b94a9c19e7
No known key found for this signature in database
GPG Key ID: 0E56ED990CDFCB4F
2 changed files with 4 additions and 6 deletions

View File

@ -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) {

View File

@ -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,
});
};