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>
63 <Form.Group as={Col} controlId="round.spoiler">
64 <Form.Label>{i18n.t('rounds.spoiler')}</Form.Label>
66 isInvalid={!!(touched.spoiler && errors.spoiler)}
69 onChange={handleChange}
71 value={values.spoiler || ''}
73 {touched.spoiler && errors.spoiler ?
74 <Form.Control.Feedback type="invalid">
75 {i18n.t(errors.spoiler)}
76 </Form.Control.Feedback>
82 <Form.Label>{i18n.t('rounds.code')}</Form.Label>
85 game={values.game || 'mixed'}
86 isInvalid={!!(touched.code && errors.code)}
89 onChange={handleChange}
90 value={values.code || []}
92 {touched.code && errors.code ?
93 <Form.Control.Feedback type="invalid">
95 </Form.Control.Feedback>
100 <Form.Group as={Col}>
101 <Form.Label>{i18n.t('rounds.rolled_by')}</Form.Label>
104 isInvalid={!!(touched.rolled_by && errors.rolled_by)}
107 onChange={handleChange}
108 value={values.rolled_by || null}
110 {touched.rolled_by && errors.rolled_by ?
111 <Form.Control.Feedback type="invalid">
112 {i18n.t(errors.rolled_by)}
113 </Form.Control.Feedback>
120 <Button onClick={onCancel} variant="secondary">
121 {i18n.t('button.cancel')}
124 <Button type="submit" variant="primary">
125 {i18n.t('button.save')}
130 EditForm.propTypes = {
131 errors: PropTypes.shape({
132 code: PropTypes.arrayOf(PropTypes.string),
133 rolled_by: PropTypes.string,
134 seed: PropTypes.string,
135 spoiler: PropTypes.string,
136 title: PropTypes.string,
138 handleBlur: PropTypes.func,
139 handleChange: PropTypes.func,
140 handleSubmit: PropTypes.func,
141 onCancel: PropTypes.func,
142 touched: PropTypes.shape({
143 code: PropTypes.arrayOf(PropTypes.bool),
144 rolled_by: PropTypes.bool,
145 seed: PropTypes.bool,
146 spoiler: PropTypes.bool,
147 title: PropTypes.bool,
149 values: PropTypes.shape({
150 code: PropTypes.arrayOf(PropTypes.string),
151 game: PropTypes.string,
152 rolled_by: PropTypes.string,
153 seed: PropTypes.string,
154 spoiler: PropTypes.string,
155 title: PropTypes.string,
159 export default withFormik({
160 displayName: 'EditForm',
161 enableReinitialize: true,
162 handleSubmit: async (values, actions) => {
163 const { round_id } = values;
164 const { setErrors } = actions;
165 const { onCancel } = actions.props;
167 await axios.put(`/api/rounds/${round_id}`, values);
168 toastr.success(i18n.t('rounds.editSuccess'));
173 toastr.error(i18n.t('rounds.editError'));
174 if (e.response && e.response.data && e.response.data.errors) {
175 setErrors(laravelErrorsToFormik(e.response.data.errors));
179 mapPropsToValues: ({ round }) => ({
180 code: round.code || [],
181 game: round.game || 'mixed',
182 rolled_by: round.rolled_by || null,
184 seed: round.seed || '',
185 spoiler: round.spoiler || '',
186 title: round.title || '',
188 validationSchema: yup.object().shape({
189 code: yup.array().of(yup.string()),
190 rolled_by: yup.string().nullable(),
191 seed: yup.string().url(),
192 spoiler: yup.string().url(),
195 })(withTranslation()(EditForm));