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 SeedCodeInput from './SeedCodeInput';
10 import laravelErrorsToFormik from '../../helpers/laravelErrorsToFormik';
11 import i18n from '../../i18n';
12 import yup from '../../schema/yup';
23 <Form noValidate onSubmit={handleSubmit}>
26 <Form.Group as={Col} controlId="round.title">
27 <Form.Label>{i18n.t('rounds.title')}</Form.Label>
29 isInvalid={!!(touched.title && errors.title)}
32 onChange={handleChange}
34 value={values.title || ''}
36 {touched.title && errors.title ?
37 <Form.Control.Feedback type="invalid">
38 {i18n.t(errors.title)}
39 </Form.Control.Feedback>
44 <Form.Group as={Col} controlId="round.seed">
45 <Form.Label>{i18n.t('rounds.seed')}</Form.Label>
47 isInvalid={!!(touched.seed && errors.seed)}
50 onChange={handleChange}
52 value={values.seed || ''}
54 {touched.seed && errors.seed ?
55 <Form.Control.Feedback type="invalid">
57 </Form.Control.Feedback>
63 <Form.Label>{i18n.t('rounds.code')}</Form.Label>
66 game={values.game || 'mixed'}
67 isInvalid={!!(touched.code && errors.code)}
70 onChange={handleChange}
71 value={values.code || []}
73 {touched.code && errors.code ?
74 <Form.Control.Feedback type="invalid">
76 </Form.Control.Feedback>
83 <Button onClick={onCancel} variant="secondary">
84 {i18n.t('button.cancel')}
87 <Button type="submit" variant="primary">
88 {i18n.t('button.save')}
93 EditForm.propTypes = {
94 errors: PropTypes.shape({
95 code: PropTypes.arrayOf(PropTypes.string),
96 seed: PropTypes.string,
97 title: PropTypes.string,
99 handleBlur: PropTypes.func,
100 handleChange: PropTypes.func,
101 handleSubmit: PropTypes.func,
102 onCancel: PropTypes.func,
103 touched: PropTypes.shape({
104 code: PropTypes.arrayOf(PropTypes.bool),
105 seed: PropTypes.bool,
106 title: PropTypes.bool,
108 values: PropTypes.shape({
109 code: PropTypes.arrayOf(PropTypes.string),
110 game: PropTypes.string,
111 seed: PropTypes.string,
112 title: PropTypes.string,
116 export default withFormik({
117 displayName: 'EditForm',
118 enableReinitialize: true,
119 handleSubmit: async (values, actions) => {
120 const { round_id } = values;
121 const { setErrors } = actions;
122 const { onCancel } = actions.props;
124 await axios.put(`/api/rounds/${round_id}`, values);
125 toastr.success(i18n.t('rounds.editSuccess'));
130 toastr.error(i18n.t('rounds.editError'));
131 if (e.response && e.response.data && e.response.data.errors) {
132 setErrors(laravelErrorsToFormik(e.response.data.errors));
136 mapPropsToValues: ({ round }) => ({
137 code: round.code || [],
138 game: round.game || 'mixed',
140 seed: round.seed || '',
141 title: round.title || '',
143 validationSchema: yup.object().shape({
144 code: yup.array().of(yup.string()),
145 seed: yup.string().url(),
148 })(withTranslation()(EditForm));