]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/components/results/ReportForm.js
server calculated scoring
[alttp.git] / resources / js / components / results / ReportForm.js
index fc98c7eefe0fcb8ddddae3cea6fbbca6d0826a4b..28a757a78ed9326a916aa6f3f24a18b3f7fedda6 100644 (file)
@@ -4,9 +4,13 @@ import PropTypes from 'prop-types';
 import React from 'react';
 import { Button, Col, Form, Modal, Row } from 'react-bootstrap';
 import { withTranslation } from 'react-i18next';
+import toastr from 'toastr';
 
-import i18n from '../../i18n';
+import LargeCheck from '../common/LargeCheck';
+import laravelErrorsToFormik from '../../helpers/laravelErrorsToFormik';
+import { findResult } from '../../helpers/Participant';
 import { formatTime, parseTime } from '../../helpers/Result';
+import i18n from '../../i18n';
 import yup from '../../schema/yup';
 
 const ReportForm = ({
@@ -21,14 +25,14 @@ const ReportForm = ({
 <Form noValidate onSubmit={handleSubmit}>
        <Modal.Body>
                <Row>
-                       <Form.Group as={Col} controlId="report.time">
+                       <Form.Group as={Col} sm={9} 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"
+                                       placeholder={values.forfeit ? 'DNF' : '1:22:59'}
                                        type="text"
                                        value={values.time || ''}
                                />
@@ -47,6 +51,17 @@ const ReportForm = ({
                                        </Form.Text>
                                }
                        </Form.Group>
+                       <Form.Group as={Col} sm={3} controlId="report.forfeit">
+                               <Form.Label>{i18n.t('results.forfeit')}</Form.Label>
+                               <Form.Control
+                                       as={LargeCheck}
+                                       isInvalid={!!(touched.forfeit && errors.forfeit)}
+                                       name="forfeit"
+                                       onBlur={handleBlur}
+                                       onChange={handleChange}
+                                       value={!!values.forfeit}
+                               />
+                       </Form.Group>
                </Row>
        </Modal.Body>
        <Modal.Footer>
@@ -63,6 +78,7 @@ const ReportForm = ({
 
 ReportForm.propTypes = {
        errors: PropTypes.shape({
+               forfeit: PropTypes.string,
                time: PropTypes.string,
        }),
        handleBlur: PropTypes.func,
@@ -70,9 +86,11 @@ ReportForm.propTypes = {
        handleSubmit: PropTypes.func,
        onCancel: PropTypes.func,
        touched: PropTypes.shape({
+               forfeit: PropTypes.bool,
                time: PropTypes.bool,
        }),
        values: PropTypes.shape({
+               forfeit: PropTypes.bool,
                time: PropTypes.string,
        }),
 };
@@ -81,23 +99,41 @@ export default withFormik({
        displayName: 'ReportForm',
        enableReinitialize: true,
        handleSubmit: async (values, actions) => {
-               const { participant_id, round_id, time } = values;
+               const { forfeit, participant_id, round_id, time } = values;
+               const { setErrors } = actions;
                const { onCancel } = actions.props;
-               await axios.post('/api/results', {
-                       participant_id,
-                       round_id,
-                       time: parseTime(time),
-               });
-               if (onCancel) {
-                       onCancel();
+               try {
+                       await axios.post('/api/results', {
+                               forfeit,
+                               participant_id,
+                               round_id,
+                               time: parseTime(time) || 0,
+                       });
+                       toastr.success(i18n.t('results.reportSuccess'));
+                       if (onCancel) {
+                               onCancel();
+                       }
+               } catch (e) {
+                       toastr.error(i18n.t('results.reportError'));
+                       if (e.response && e.response.data && e.response.data.errors) {
+                               setErrors(laravelErrorsToFormik(e.response.data.errors));
+                       }
                }
        },
-       mapPropsToValues: ({ participant, round }) => ({
-               participant_id: participant.id,
-               round_id: round.id,
-               time: '',
-       }),
+       mapPropsToValues: ({ participant, round }) => {
+               const result = findResult(participant, round);
+               return {
+                       forfeit: result ? !!result.forfeit : false,
+                       participant_id: participant.id,
+                       round_id: round.id,
+                       time: result && result.time ? formatTime(result) : '',
+               };
+       },
        validationSchema: yup.object().shape({
-               time: yup.string().required().time(),
+               forfeit: yup.boolean().required(),
+               time: yup.string().time().when('forfeit', {
+                       is: false,
+                       then: yup.string().required().time(),
+               }),
        }),
 })(withTranslation()(ReportForm));