handle any errors thrown in survey handleSubmit

This commit is contained in:
Keith Grant 2020-03-20 10:53:59 -07:00
parent 564012b2c8
commit 56d31fec77
2 changed files with 19 additions and 20 deletions

View File

@ -9,15 +9,15 @@ export default function SurveyQuestionAdd({ survey, updateSurvey }) {
const match = useRouteMatch();
const handleSubmit = async question => {
if (survey.spec?.some(q => q.variable === question.variable)) {
setFormError(
new Error(
`Survey already contains a question with variable named “${question.variable}`
)
);
return;
}
try {
if (survey.spec?.some(q => q.variable === question.variable)) {
setFormError(
new Error(
`Survey already contains a question with variable named “${question.variable}`
)
);
return;
}
const newSpec = survey.spec ? survey.spec.concat(question) : [question];
await updateSurvey(newSpec);
history.push(match.url.replace('/add', ''));

View File

@ -21,19 +21,18 @@ export default function SurveyQuestionEdit({ survey, updateSurvey }) {
};
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 {
if (
formData.variable !== question.variable &&
survey.spec.find(q => q.variable === formData.variable)
) {
setFormError(
new Error(
`Survey already contains a question with variable named “${formData.variable}`
)
);
return;
}
const questionIndex = survey.spec.findIndex(
q => q.variable === match.params.variable
);