1 import axios from 'axios';
2 import { withFormik } from 'formik';
3 import PropTypes from 'prop-types';
4 import React from 'react';
5 import { Button, Col, Form, Modal, Row } from 'react-bootstrap';
6 import { withTranslation } from 'react-i18next';
7 import toastr from 'toastr';
9 import laravelErrorsToFormik from '../../helpers/laravelErrorsToFormik';
10 import i18n from '../../i18n';
11 import yup from '../../schema/yup';
22 <Form noValidate onSubmit={handleSubmit}>
25 <Form.Group as={Col} controlId="round.title">
26 <Form.Label>{i18n.t('rounds.title')}</Form.Label>
28 isInvalid={!!(touched.title && errors.title)}
31 onChange={handleChange}
33 value={values.title || ''}
35 {touched.title && errors.title ?
36 <Form.Control.Feedback type="invalid">
37 {i18n.t(errors.title)}
38 </Form.Control.Feedback>
43 <Form.Group as={Col} controlId="round.seed">
44 <Form.Label>{i18n.t('rounds.seed')}</Form.Label>
46 isInvalid={!!(touched.seed && errors.seed)}
49 onChange={handleChange}
51 value={values.seed || ''}
53 {touched.seed && errors.seed ?
54 <Form.Control.Feedback type="invalid">
56 </Form.Control.Feedback>
63 <Button onClick={onCancel} variant="secondary">
64 {i18n.t('button.cancel')}
67 <Button type="submit" variant="primary">
68 {i18n.t('button.save')}
73 EditForm.propTypes = {
74 errors: PropTypes.shape({
75 seed: PropTypes.string,
76 title: PropTypes.string,
78 handleBlur: PropTypes.func,
79 handleChange: PropTypes.func,
80 handleSubmit: PropTypes.func,
81 onCancel: PropTypes.func,
82 touched: PropTypes.shape({
84 title: PropTypes.bool,
86 values: PropTypes.shape({
87 seed: PropTypes.string,
88 title: PropTypes.string,
92 export default withFormik({
93 displayName: 'EditForm',
94 enableReinitialize: true,
95 handleSubmit: async (values, actions) => {
96 const { round_id } = values;
97 const { setErrors } = actions;
98 const { onCancel } = actions.props;
100 await axios.put(`/api/rounds/${round_id}`, values);
101 toastr.success(i18n.t('rounds.editSuccess'));
106 toastr.error(i18n.t('rounds.editError'));
107 if (e.response && e.response.data && e.response.data.errors) {
108 setErrors(laravelErrorsToFormik(e.response.data.errors));
112 mapPropsToValues: ({ round }) => ({
114 seed: round.seed || '',
115 title: round.title || '',
117 validationSchema: yup.object().shape({
118 seed: yup.string().url(),
121 })(withTranslation()(EditForm));