]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/components/tournament/DiscordForm.js
tournament settings
[alttp.git] / resources / js / components / tournament / DiscordForm.js
diff --git a/resources/js/components/tournament/DiscordForm.js b/resources/js/components/tournament/DiscordForm.js
new file mode 100644 (file)
index 0000000..f9eba93
--- /dev/null
@@ -0,0 +1,83 @@
+import axios from 'axios';
+import { withFormik } from 'formik';
+import PropTypes from 'prop-types';
+import React from 'react';
+import { Button, Col, Form, Row } from 'react-bootstrap';
+import { withTranslation } from 'react-i18next';
+import toastr from 'toastr';
+
+import laravelErrorsToFormik from '../../helpers/laravelErrorsToFormik';
+import i18n from '../../i18n';
+import yup from '../../schema/yup';
+
+const DiscordForm = ({
+       errors,
+       handleBlur,
+       handleChange,
+       handleSubmit,
+       touched,
+       values,
+}) =>
+<Form noValidate onSubmit={handleSubmit}>
+       <fieldset>
+               <legend>{i18n.t('tournaments.discordSettings')}</legend>
+               <Form.Group controlId="tournament.discord_round_template">
+                       <Form.Label>
+                               {i18n.t('tournaments.discordRoundTemplate')}
+                       </Form.Label>
+                       <Form.Control
+                               isInvalid={!!(touched.round_template && errors.round_template)}
+                               name="round_template"
+                               onBlur={handleBlur}
+                               onChange={handleChange}
+                               type="text"
+                               value={values.round_template || ''}
+                       />
+               </Form.Group>
+               <Button className="mt-3" type="submit" variant="primary">
+                       {i18n.t('button.save')}
+               </Button>
+       </fieldset>
+</Form>;
+
+DiscordForm.propTypes = {
+       errors: PropTypes.shape({
+               round_template: PropTypes.string,
+       }),
+       handleBlur: PropTypes.func,
+       handleChange: PropTypes.func,
+       handleSubmit: PropTypes.func,
+       touched: PropTypes.shape({
+               round_template: PropTypes.bool,
+       }),
+       values: PropTypes.shape({
+               round_template: PropTypes.string,
+       }),
+};
+
+export default withFormik({
+       displayName: 'DiscordForm',
+       enableReinitialize: true,
+       handleSubmit: async (values, actions) => {
+               const { round_template } = values;
+               const { setErrors } = actions;
+               const { tournament } = actions.props;
+               try {
+                       await axios.post(`/api/tournaments/${tournament.id}/discord-settings`, {
+                               round_template,
+                       });
+                       toastr.success(i18n.t('tournaments.discordSettingsSuccess'));
+               } catch (e) {
+                       toastr.error(i18n.t('tournaments.discordSettingsError'));
+                       if (e.response && e.response.data && e.response.data.errors) {
+                               setErrors(laravelErrorsToFormik(e.response.data.errors));
+                       }
+               }
+       },
+       mapPropsToValues: ({ tournament }) => ({
+               round_template: tournament.discord_round_template || '',
+       }),
+       validationSchema: yup.object().shape({
+               round_template: yup.string(),
+       }),
+})(withTranslation()(DiscordForm));