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 UserSelect from '../common/UserSelect';
11 import laravelErrorsToFormik from '../../helpers/laravelErrorsToFormik';
12 import i18n from '../../i18n';
13 import yup from '../../schema/yup';
24 <Form noValidate onSubmit={handleSubmit}>
27 <Form.Group as={Col} controlId="round.title">
28 <Form.Label>{i18n.t('rounds.title')}</Form.Label>
30 isInvalid={!!(touched.title && errors.title)}
33 onChange={handleChange}
35 value={values.title || ''}
37 {touched.title && errors.title ?
38 <Form.Control.Feedback type="invalid">
39 {i18n.t(errors.title)}
40 </Form.Control.Feedback>
45 <Form.Group as={Col} controlId="round.seed">
46 <Form.Label>{i18n.t('rounds.seed')}</Form.Label>
48 isInvalid={!!(touched.seed && errors.seed)}
51 onChange={handleChange}
53 value={values.seed || ''}
55 {touched.seed && errors.seed ?
56 <Form.Control.Feedback type="invalid">
58 </Form.Control.Feedback>
64 <Form.Label>{i18n.t('rounds.code')}</Form.Label>
67 game={values.game || 'mixed'}
68 isInvalid={!!(touched.code && errors.code)}
71 onChange={handleChange}
72 value={values.code || []}
74 {touched.code && errors.code ?
75 <Form.Control.Feedback type="invalid">
77 </Form.Control.Feedback>
83 <Form.Label>{i18n.t('rounds.rolled_by')}</Form.Label>
86 isInvalid={!!(touched.rolled_by && errors.rolled_by)}
89 onChange={handleChange}
90 value={values.rolled_by || null}
92 {touched.rolled_by && errors.rolled_by ?
93 <Form.Control.Feedback type="invalid">
94 {i18n.t(errors.rolled_by)}
95 </Form.Control.Feedback>
102 <Button onClick={onCancel} variant="secondary">
103 {i18n.t('button.cancel')}
106 <Button type="submit" variant="primary">
107 {i18n.t('button.save')}
112 EditForm.propTypes = {
113 errors: PropTypes.shape({
114 code: PropTypes.arrayOf(PropTypes.string),
115 rolled_by: PropTypes.string,
116 seed: PropTypes.string,
117 title: PropTypes.string,
119 handleBlur: PropTypes.func,
120 handleChange: PropTypes.func,
121 handleSubmit: PropTypes.func,
122 onCancel: PropTypes.func,
123 touched: PropTypes.shape({
124 code: PropTypes.arrayOf(PropTypes.bool),
125 rolled_by: PropTypes.bool,
126 seed: PropTypes.bool,
127 title: PropTypes.bool,
129 values: PropTypes.shape({
130 code: PropTypes.arrayOf(PropTypes.string),
131 game: PropTypes.string,
132 rolled_by: PropTypes.string,
133 seed: PropTypes.string,
134 title: PropTypes.string,
138 export default withFormik({
139 displayName: 'EditForm',
140 enableReinitialize: true,
141 handleSubmit: async (values, actions) => {
142 const { round_id } = values;
143 const { setErrors } = actions;
144 const { onCancel } = actions.props;
146 await axios.put(`/api/rounds/${round_id}`, values);
147 toastr.success(i18n.t('rounds.editSuccess'));
152 toastr.error(i18n.t('rounds.editError'));
153 if (e.response && e.response.data && e.response.data.errors) {
154 setErrors(laravelErrorsToFormik(e.response.data.errors));
158 mapPropsToValues: ({ round }) => ({
159 code: round.code || [],
160 game: round.game || 'mixed',
161 rolled_by: round.rolled_by || null,
163 seed: round.seed || '',
164 title: round.title || '',
166 validationSchema: yup.object().shape({
167 code: yup.array().of(yup.string()),
168 rolled_by: yup.string(),
169 seed: yup.string().url(),
172 })(withTranslation()(EditForm));