+import axios from 'axios';
+import { withFormik } from 'formik';
+import PropTypes from 'prop-types';
+import React from 'react';
+import { Button, Col, Form, Modal, Row } from 'react-bootstrap';
+import { withTranslation } from 'react-i18next';
+
+import i18n from '../../i18n';
+import { formatTime, parseTime } from '../../helpers/Result';
+import yup from '../../schema/yup';
+
+const ReportForm = ({
+ errors,
+ handleBlur,
+ handleChange,
+ handleSubmit,
+ onCancel,
+ touched,
+ values,
+}) =>
+<Form noValidate onSubmit={handleSubmit}>
+ <Modal.Body>
+ <Row>
+ <Form.Group as={Col} controlId="report.time">
+ <Form.Label>{i18n.t('results.reportTime')}</Form.Label>
+ <Form.Control
+ isInvalid={!!(touched.time && errors.time)}
+ name="time"
+ onBlur={handleBlur}
+ onChange={handleChange}
+ placeholder="1:22:59"
+ type="text"
+ value={values.time || ''}
+ />
+ {touched.time && errors.time ?
+ <Form.Control.Feedback type="invalid">
+ {i18n.t(errors.time)}
+ </Form.Control.Feedback>
+ :
+ <Form.Text muted>
+ {parseTime(values.time) ?
+ i18n.t(
+ 'results.reportPreview',
+ { time: formatTime({ time: parseTime(values.time) })},
+ )
+ : null}
+ </Form.Text>
+ }
+ </Form.Group>
+ </Row>
+ </Modal.Body>
+ <Modal.Footer>
+ {onCancel ?
+ <Button onClick={onCancel} variant="secondary">
+ {i18n.t('button.cancel')}
+ </Button>
+ : null}
+ <Button type="submit" variant="primary">
+ {i18n.t('button.save')}
+ </Button>
+ </Modal.Footer>
+</Form>;
+
+ReportForm.propTypes = {
+ errors: PropTypes.shape({
+ time: PropTypes.string,
+ }),
+ handleBlur: PropTypes.func,
+ handleChange: PropTypes.func,
+ handleSubmit: PropTypes.func,
+ onCancel: PropTypes.func,
+ touched: PropTypes.shape({
+ time: PropTypes.bool,
+ }),
+ values: PropTypes.shape({
+ time: PropTypes.string,
+ }),
+};
+
+export default withFormik({
+ displayName: 'ReportForm',
+ enableReinitialize: true,
+ handleSubmit: async (values, actions) => {
+ const { participant_id, round_id, time } = values;
+ const { onCancel } = actions.props;
+ await axios.post('/api/results', {
+ participant_id,
+ round_id,
+ time: parseTime(time),
+ });
+ if (onCancel) {
+ onCancel();
+ }
+ },
+ mapPropsToValues: ({ participant, round }) => ({
+ participant_id: participant.id,
+ round_id: round.id,
+ time: '',
+ }),
+ validationSchema: yup.object().shape({
+ time: yup.string().required().time(),
+ }),
+})(withTranslation()(ReportForm));