wire in SurveyQuestionAdd form to post to API

This commit is contained in:
Keith Grant 2020-03-16 10:50:40 -07:00
parent 1412bf6232
commit 782d465c78
2 changed files with 18 additions and 5 deletions

View File

@ -3,13 +3,25 @@ import { useHistory, useRouteMatch } from 'react-router-dom';
import { CardBody } from '@components/Card';
import SurveyQuestionForm from './SurveyQuestionForm';
export default function SurveyQuestionAdd({ template }) {
export default function SurveyQuestionAdd({ survey, updateSurvey }) {
const [formError, setFormError] = useState(null);
const history = useHistory();
const match = useRouteMatch();
const handleSubmit = async formData => {
//
const handleSubmit = async question => {
if (survey.spec.find(q => q.variable === question.variable)) {
setFormError(
new Error(`Survey already contains a question with variable named
${question.variable}`)
);
return;
}
try {
await updateSurvey(survey.spec.concat(question));
history.push(match.url.replace('/add', ''));
} catch (err) {
setFormError(err);
}
};
const handleCancel = () => {
@ -21,6 +33,7 @@ export default function SurveyQuestionAdd({ template }) {
<SurveyQuestionForm
handleSubmit={handleSubmit}
handleCancel={handleCancel}
submitError={formError}
/>
</CardBody>
);

View File

@ -15,7 +15,7 @@ import FormField, {
import AnsibleSelect from '@components/AnsibleSelect';
import { required, noWhiteSpace, combine } from '@util/validators';
function AnswerType({ i18n }) {
function AnswerTypeField({ i18n }) {
const [field] = useField({
name: 'type',
validate: required(i18n._(t`Select a value for this field`), i18n),
@ -112,7 +112,7 @@ function SurveyQuestionForm({
etc.). Variable names with spaces are not allowed.`
)}
/>
<AnswerType i18n={i18n} />
<AnswerTypeField i18n={i18n} />
<CheckboxField
id="question-required"
name="required"