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 LargeCheck from '../common/LargeCheck';
10 import laravelErrorsToFormik from '../../helpers/laravelErrorsToFormik';
11 import { findResult } from '../../helpers/User';
12 import { formatTime, parseTime } from '../../helpers/Result';
13 import i18n from '../../i18n';
14 import yup from '../../schema/yup';
26 <Form noValidate onSubmit={handleSubmit}>
30 <Form.Group as={Col} sm={9} controlId="report.time">
31 <Form.Label>{i18n.t('results.reportTime')}</Form.Label>
33 isInvalid={!!(touched.time && errors.time)}
36 onChange={handleChange}
37 placeholder={values.forfeit ? 'DNF' : '1:22:59'}
39 value={values.time || ''}
41 {touched.time && errors.time ?
42 <Form.Control.Feedback type="invalid">
44 </Form.Control.Feedback>
47 {parseTime(values.time) ?
49 'results.reportPreview',
50 { time: formatTime({ time: parseTime(values.time) })},
56 <Form.Group as={Col} sm={3} controlId="report.forfeit">
57 <Form.Label>{i18n.t('results.forfeit')}</Form.Label>
60 isInvalid={!!(touched.forfeit && errors.forfeit)}
63 onChange={handleChange}
64 value={!!values.forfeit}
69 <Form.Group controlId="report.vod">
70 <Form.Label>{i18n.t('results.vod')}</Form.Label>
72 isInvalid={!!(touched.vod && errors.vod)}
75 onChange={handleChange}
76 placeholder="https://twitch.tv/youtube"
78 value={values.vod || ''}
80 {touched.vod && errors.vod ?
81 <Form.Control.Feedback type="invalid">
83 </Form.Control.Feedback>
86 {i18n.t('results.vodNote')}
90 <Form.Group controlId="report.comment">
91 <Form.Label>{i18n.t('results.comment')}</Form.Label>
94 isInvalid={!!(touched.comment && errors.comment)}
97 onChange={handleChange}
99 value={values.comment || ''}
105 <Button onClick={onCancel} variant="secondary">
106 {i18n.t('button.cancel')}
109 <Button type="submit" variant="primary">
110 {i18n.t('button.save')}
115 ReportForm.propTypes = {
116 errors: PropTypes.shape({
117 comment: PropTypes.string,
118 forfeit: PropTypes.string,
119 time: PropTypes.string,
120 vod: PropTypes.string,
122 handleBlur: PropTypes.func,
123 handleChange: PropTypes.func,
124 handleSubmit: PropTypes.func,
125 onCancel: PropTypes.func,
126 round: PropTypes.shape({
127 locked: PropTypes.bool,
129 touched: PropTypes.shape({
130 comment: PropTypes.bool,
131 forfeit: PropTypes.bool,
132 time: PropTypes.bool,
135 values: PropTypes.shape({
136 comment: PropTypes.string,
137 forfeit: PropTypes.bool,
138 time: PropTypes.string,
139 vod: PropTypes.string,
143 export default withFormik({
144 displayName: 'ReportForm',
145 enableReinitialize: true,
146 handleSubmit: async (values, actions) => {
147 const { comment, forfeit, round_id, time, user_id, vod } = values;
148 const { setErrors } = actions;
149 const { onCancel } = actions.props;
151 await axios.post('/api/results', {
155 time: parseTime(time) || 0,
159 toastr.success(i18n.t('results.reportSuccess'));
164 toastr.error(i18n.t('results.reportError'));
165 if (e.response && e.response.data && e.response.data.errors) {
166 setErrors(laravelErrorsToFormik(e.response.data.errors));
170 mapPropsToValues: ({ round, user }) => {
171 const result = findResult(user, round);
173 comment: result && result.comment ? result.comment : '',
174 forfeit: result ? !!result.forfeit : false,
176 time: result && result.time ? formatTime(result) : '',
178 vod: result && result.vod ? result.vod : '',
181 validationSchema: yup.object().shape({
182 comment: yup.string(),
183 forfeit: yup.boolean().required(),
184 time: yup.string().time().when('forfeit', {
186 then: () => yup.string().required().time(),
188 vod: yup.string().url(),
190 })(withTranslation()(ReportForm));