1 import { withFormik } from 'formik';
2 import PropTypes from 'prop-types';
3 import React from 'react';
4 import { Button, Col, Form, Row } from 'react-bootstrap';
5 import { useTranslation } from 'react-i18next';
7 import { formatTime, parseTime } from '../../helpers/Result';
8 import yup from '../../schema/yup';
10 const ChatSettingsForm = ({
20 const { t } = useTranslation();
22 return <Form noValidate onSubmit={handleSubmit}>
24 <Form.Group as={Col} md={6} controlId="chatSettings.wait_msgs_min">
25 <Form.Label>{t('twitchBot.chatWaitMsgsMin')}</Form.Label>
27 isInvalid={!!(touched.wait_msgs_min && errors.wait_msgs_min)}
31 onChange={handleChange}
33 value={values.wait_msgs_min || 1}
36 <Form.Group as={Col} md={6} controlId="chatSettings.wait_msgs_max">
37 <Form.Label>{t('twitchBot.chatWaitMsgsMax')}</Form.Label>
39 isInvalid={!!(touched.wait_msgs_max && errors.wait_msgs_max)}
43 onChange={handleChange}
45 value={values.wait_msgs_max || 10}
48 <Form.Group as={Col} md={6} controlId="chatSettings.wait_time_min">
49 <Form.Label>{t('twitchBot.chatWaitTimeMin')}</Form.Label>
51 isInvalid={!!(touched.wait_time_min && errors.wait_time_min)}
54 onChange={handleChange}
56 value={values.wait_time_min || '0'}
58 {touched.wait_time_min && errors.wait_time_min ?
59 <Form.Control.Feedback type="invalid">
60 {t(errors.wait_time_min)}
61 </Form.Control.Feedback>
64 {formatTime({ time: parseTime(values.wait_time_min)})}
68 <Form.Group as={Col} md={6} controlId="chatSettings.wait_time_max">
69 <Form.Label>{t('twitchBot.chatWaitTimeMax')}</Form.Label>
71 isInvalid={!!(touched.wait_time_max && errors.wait_time_max)}
74 onChange={handleChange}
76 value={values.wait_time_max || '15:00'}
78 {touched.wait_time_max && errors.wait_time_max ?
79 <Form.Control.Feedback type="invalid">
80 {t(errors.wait_time_max)}
81 </Form.Control.Feedback>
84 {formatTime({ time: parseTime(values.wait_time_max)})}
89 <div className="button-bar mt-3">
90 <Button disabled={!dirty || isSubmitting} type="submit" variant="primary">
97 ChatSettingsForm.propTypes = {
98 dirty: PropTypes.bool,
99 errors: PropTypes.shape({
100 wait_msgs_max: PropTypes.string,
101 wait_msgs_min: PropTypes.string,
102 wait_time_min: PropTypes.string,
103 wait_time_max: PropTypes.string,
105 handleBlur: PropTypes.func,
106 handleChange: PropTypes.func,
107 handleSubmit: PropTypes.func,
108 isSubmitting: PropTypes.bool,
109 touched: PropTypes.shape({
110 wait_msgs_max: PropTypes.bool,
111 wait_msgs_min: PropTypes.bool,
112 wait_time_min: PropTypes.bool,
113 wait_time_max: PropTypes.bool,
115 values: PropTypes.shape({
116 wait_msgs_max: PropTypes.number,
117 wait_msgs_min: PropTypes.number,
118 wait_time_min: PropTypes.string,
119 wait_time_max: PropTypes.string,
123 export default withFormik({
124 displayName: 'ChatSettingsForm',
125 enableReinitialize: true,
126 handleSubmit: async (values, actions) => {
127 const { onSubmit } = actions.props;
130 wait_time_min: parseTime(values.wait_time_min) || 0,
131 wait_time_max: parseTime(values.wait_time_max) || 0,
134 mapPropsToValues: ({ channel }) => ({
135 wait_msgs_min: channel.chat_settings.wait_msgs_min || 1,
136 wait_msgs_max: channel.chat_settings.wait_msgs_max || 10,
137 wait_time_min: channel.chat_settings.wait_time_min
138 ? formatTime({ time: channel.chat_settings.wait_time_min }) : '0',
139 wait_time_max: channel.chat_settings.wait_time_max
140 ? formatTime({ time: channel.chat_settings.wait_time_max }) : '15:00',
142 validationSchema: yup.object().shape({
143 wait_msgs_min: yup.number().min(1),
144 wait_msgs_max: yup.number().min(1),
145 wait_time_min: yup.string().time(),
146 wait_time_max: yup.string().time(),
148 })(ChatSettingsForm);