X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=resources%2Fjs%2Fcomponents%2Fresults%2FReportForm.js;h=28a757a78ed9326a916aa6f3f24a18b3f7fedda6;hb=d32516335ea2534e15256c948e9c38d3de40794b;hp=fc98c7eefe0fcb8ddddae3cea6fbbca6d0826a4b;hpb=d748feb96453d74aeffec648d6f5f68d9ef3b520;p=alttp.git diff --git a/resources/js/components/results/ReportForm.js b/resources/js/components/results/ReportForm.js index fc98c7e..28a757a 100644 --- a/resources/js/components/results/ReportForm.js +++ b/resources/js/components/results/ReportForm.js @@ -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 = ({
- + {i18n.t('results.reportTime')} @@ -47,6 +51,17 @@ const ReportForm = ({ } + + {i18n.t('results.forfeit')} + + @@ -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));