X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=resources%2Fjs%2Fcomponents%2Fresults%2FReportForm.js;h=2e85f4a4c3abfb9c334b3cd18253bf91727001f3;hb=3a069159f7971758817bb281f72a41ddc7d7a958;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..2e85f4a 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/User'; import { formatTime, parseTime } from '../../helpers/Result'; +import i18n from '../../i18n'; import yup from '../../schema/yup'; const ReportForm = ({ @@ -15,39 +19,86 @@ const ReportForm = ({ handleChange, handleSubmit, onCancel, + round, touched, values, }) =>
- - - {i18n.t('results.reportTime')} - - {touched.time && errors.time ? - - {i18n.t(errors.time)} - - : - - {parseTime(values.time) ? - i18n.t( - 'results.reportPreview', - { time: formatTime({ time: parseTime(values.time) })}, - ) - : null} - - } - - + {!round.locked ? + + + {i18n.t('results.reportTime')} + + {touched.time && errors.time ? + + {i18n.t(errors.time)} + + : + + {parseTime(values.time) ? + i18n.t( + 'results.reportPreview', + { time: formatTime({ time: parseTime(values.time) })}, + ) + : null} + + } + + + {i18n.t('results.forfeit')} + + + + : null} + + {i18n.t('results.vod')} + + {touched.vod && errors.vod ? + + {i18n.t(errors.vod)} + + : + + {i18n.t('results.vodNote')} + + } + + + {i18n.t('results.comment')} + + {onCancel ? @@ -63,17 +114,29 @@ const ReportForm = ({ ReportForm.propTypes = { errors: PropTypes.shape({ + comment: PropTypes.string, + forfeit: PropTypes.string, time: PropTypes.string, + vod: PropTypes.string, }), handleBlur: PropTypes.func, handleChange: PropTypes.func, handleSubmit: PropTypes.func, onCancel: PropTypes.func, + round: PropTypes.shape({ + locked: PropTypes.bool, + }), touched: PropTypes.shape({ + comment: PropTypes.bool, + forfeit: PropTypes.bool, time: PropTypes.bool, + vod: PropTypes.bool, }), values: PropTypes.shape({ + comment: PropTypes.string, + forfeit: PropTypes.bool, time: PropTypes.string, + vod: PropTypes.string, }), }; @@ -81,23 +144,47 @@ export default withFormik({ displayName: 'ReportForm', enableReinitialize: true, handleSubmit: async (values, actions) => { - const { participant_id, round_id, time } = values; + const { comment, forfeit, round_id, time, user_id, vod } = 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', { + comment, + forfeit, + round_id, + time: parseTime(time) || 0, + user_id, + vod, + }); + 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: ({ round, user }) => { + const result = findResult(user, round); + return { + comment: result && result.comment ? result.comment : '', + forfeit: result ? !!result.forfeit : false, + round_id: round.id, + time: result && result.time ? formatTime(result) : '', + user_id: user.id, + vod: result && result.vod ? result.vod : '', + }; + }, validationSchema: yup.object().shape({ - time: yup.string().required().time(), + comment: yup.string(), + forfeit: yup.boolean().required(), + time: yup.string().time().when('forfeit', { + is: false, + then: () => yup.string().required().time(), + }), + vod: yup.string().url(), }), })(withTranslation()(ReportForm));