1 import { withFormik } from 'formik';
2 import PropTypes from 'prop-types';
3 import React from 'react';
4 import { Button, Col, Form, Modal, Row } from 'react-bootstrap';
5 import { useTranslation } from 'react-i18next';
7 import DialogEpisode from './DialogEpisode';
8 import ToggleSwitch from '../common/ToggleSwitch';
9 import laravelErrorsToFormik from '../../helpers/laravelErrorsToFormik';
10 import { withUser } from '../../hooks/user';
12 const channelCompare = (a, b) => a.channel.title.localeCompare(b.channel.title);
14 const RestreamAddForm = ({
25 const { t } = useTranslation();
27 return <Form noValidate onSubmit={handleSubmit}>
29 <DialogEpisode episode={episode} />
30 <Form.Group controlId="episodes.channel_id">
31 <Form.Label>{t('episodes.channel')}</Form.Label>
33 isInvalid={!!(touched.channel_id && errors.channel_id)}
36 onChange={handleChange}
37 value={values.channel_id || 0}
39 <option disabled value={0}>{t('general.pleaseSelect')}</option>
40 {((user && user.channel_crews) || []).sort(channelCompare).map(c =>
41 <option key={c.id} value={c.channel_id}>
46 {touched.channel_id && errors.channel_id ?
47 <Form.Control.Feedback type="invalid">
48 {t(errors.channel_id)}
49 </Form.Control.Feedback>
53 <Form.Group as={Col} sm={6} controlId="episodes.accept_comms">
54 <Form.Label className="d-block">
55 {t('episodes.restreamDialog.acceptComms')}
59 isInvalid={!!(touched.accept_comms && errors.accept_comms)}
62 onChange={handleChange}
63 value={!!values.accept_comms}
65 {touched.accept_comms && errors.accept_comms ?
66 <Form.Control.Feedback type="invalid">
67 {t(errors.accept_comms)}
68 </Form.Control.Feedback>
71 <Form.Group as={Col} sm={6} controlId="episodes.accept_tracker">
72 <Form.Label className="d-block">
73 {t('episodes.restreamDialog.acceptTracker')}
77 isInvalid={!!(touched.accept_tracker && errors.accept_tracker)}
80 onChange={handleChange}
81 value={!!values.accept_tracker}
83 {touched.accept_tracker && errors.accept_tracker ?
84 <Form.Control.Feedback type="invalid">
85 {t(errors.accept_tracker)}
86 </Form.Control.Feedback>
93 <Button onClick={onCancel} variant="secondary">
97 <Button type="submit" variant="primary">
104 RestreamAddForm.propTypes = {
105 episode: PropTypes.shape({
106 event: PropTypes.shape({
107 title: PropTypes.string,
109 players: PropTypes.arrayOf(PropTypes.shape({
111 start: PropTypes.string,
113 errors: PropTypes.shape({
114 accept_comms: PropTypes.string,
115 accept_tracker: PropTypes.string,
116 channel_id: PropTypes.string,
118 handleBlur: PropTypes.func,
119 handleChange: PropTypes.func,
120 handleSubmit: PropTypes.func,
121 onCancel: PropTypes.func,
122 touched: PropTypes.shape({
123 accept_comms: PropTypes.bool,
124 accept_tracker: PropTypes.bool,
125 channel_id: PropTypes.bool,
127 user: PropTypes.shape({
128 channel_crews: PropTypes.arrayOf(PropTypes.shape({
131 values: PropTypes.shape({
132 accept_comms: PropTypes.bool,
133 accept_tracker: PropTypes.bool,
134 channel_id: PropTypes.number,
138 export default withUser(withFormik({
139 displayName: 'RestreamAddForm',
140 enableReinitialize: true,
141 handleSubmit: async (values, actions) => {
142 const { setErrors } = actions;
143 const { onSubmit } = actions.props;
145 await onSubmit(values);
147 if (e.response && e.response.data && e.response.data.errors) {
148 setErrors(laravelErrorsToFormik(e.response.data.errors));
152 mapPropsToValues: ({ episode, user }) => ({
154 accept_tracker: false,
155 channel_id: user && user.channel_crews && user.channel_crews.length
156 ? user.channel_crews[0].channel_id : 0,
157 episode_id: episode ? episode.id : 0,
159 })(RestreamAddForm));