1 import { withFormik } from 'formik';
2 import PropTypes from 'prop-types';
3 import React from 'react';
4 import { Button, Form, Modal } from 'react-bootstrap';
5 import { useTranslation } from 'react-i18next';
7 import laravelErrorsToFormik from '../../helpers/laravelErrorsToFormik';
8 import yup from '../../schema/yup';
10 const CommandForm = ({
20 const { t } = useTranslation();
31 const RESTRICTIONS = [
37 return <Form noValidate onSubmit={handleSubmit}>
39 <Form.Group controlId="command.name">
40 <Form.Label>{t('twitchBot.commandName')}</Form.Label>
43 isInvalid={!!(touched.name && errors.name)}
46 onChange={handleChange}
50 value={values.name || ''}
52 {touched.name && errors.name ?
53 <Form.Control.Feedback type="invalid">
55 </Form.Control.Feedback>
58 <Form.Group controlId="command.restrict">
59 <Form.Label>{t('twitchBot.commandRestriction')}</Form.Label>
61 isInvalid={!!(touched.restrict && errors.restrict)}
64 onChange={handleChange}
65 value={values.restrict || 'none'}
67 {RESTRICTIONS.map(r =>
68 <option key={r} value={r}>
69 {t(`twitchBot.commandRestrictions.${r}`)}
73 {touched.restrict && errors.restrict ?
74 <Form.Control.Feedback type="invalid">
76 </Form.Control.Feedback>
79 <Form.Group controlId="command.command">
80 <Form.Label>{t('twitchBot.commandType')}</Form.Label>
82 isInvalid={!!(touched.command && errors.command)}
85 onChange={handleChange}
86 value={values.command || 'none'}
89 <option key={c} value={c}>
90 {t(`twitchBot.commandTypes.${c}`)}
94 {touched.command && errors.command ?
95 <Form.Control.Feedback type="invalid">
97 </Form.Control.Feedback>
103 <Button onClick={onCancel} variant="secondary">
107 <Button type="submit" variant="primary">
114 CommandForm.propTypes = {
115 errors: PropTypes.shape({
116 command: PropTypes.string,
117 name: PropTypes.string,
118 restrict: PropTypes.string,
120 handleBlur: PropTypes.func,
121 handleChange: PropTypes.func,
122 handleSubmit: PropTypes.func,
123 name: PropTypes.string,
124 onCancel: PropTypes.func,
125 touched: PropTypes.shape({
126 command: PropTypes.bool,
127 name: PropTypes.bool,
128 restrict: PropTypes.bool,
130 values: PropTypes.shape({
131 command: PropTypes.string,
132 name: PropTypes.string,
133 restrict: PropTypes.string,
137 export default withFormik({
138 displayName: 'CommandForm',
139 enableReinitialize: true,
140 handleSubmit: async (values, actions) => {
141 const { setErrors } = actions;
142 const { onSubmit } = actions.props;
144 await onSubmit(values);
146 if (e.response && e.response.data && e.response.data.errors) {
147 setErrors(laravelErrorsToFormik(e.response.data.errors));
151 mapPropsToValues: ({ name, settings }) => {
153 command: (settings && settings.command) || 'none',
155 restrict: (settings && settings.restrict) || 'none',
158 validationSchema: yup.object().shape({
159 command: yup.string(),
160 name: yup.string().required(),
161 restrict: yup.string(),