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 '../../helpers/UserContext';
12 const RestreamAddForm = ({
23 const { t } = useTranslation();
25 return <Form noValidate onSubmit={handleSubmit}>
27 <DialogEpisode episode={episode} />
28 <Form.Group controlId="episodes.channel_id">
29 <Form.Label>{t('episodes.channel')}</Form.Label>
31 isInvalid={!!(touched.channel_id && errors.channel_id)}
34 onChange={handleChange}
35 value={values.channel_id || 0}
37 <option disabled value={0}>{t('general.pleaseSelect')}</option>
38 {((user && user.channel_crews) || []).map(c =>
39 <option key={c.id} value={c.channel_id}>
44 {touched.channel_id && errors.channel_id ?
45 <Form.Control.Feedback type="invalid">
46 {t(errors.channel_id)}
47 </Form.Control.Feedback>
51 <Form.Group as={Col} sm={6} controlId="episodes.accept_comms">
52 <Form.Label className="d-block">
53 {t('episodes.restreamDialog.acceptComms')}
57 isInvalid={!!(touched.accept_comms && errors.accept_comms)}
60 onChange={handleChange}
61 value={!!values.accept_comms}
63 {touched.accept_comms && errors.accept_comms ?
64 <Form.Control.Feedback type="invalid">
65 {t(errors.accept_comms)}
66 </Form.Control.Feedback>
69 <Form.Group as={Col} sm={6} controlId="episodes.accept_tracker">
70 <Form.Label className="d-block">
71 {t('episodes.restreamDialog.acceptTracker')}
75 isInvalid={!!(touched.accept_tracker && errors.accept_tracker)}
78 onChange={handleChange}
79 value={!!values.accept_tracker}
81 {touched.accept_tracker && errors.accept_tracker ?
82 <Form.Control.Feedback type="invalid">
83 {t(errors.accept_tracker)}
84 </Form.Control.Feedback>
91 <Button onClick={onCancel} variant="secondary">
95 <Button type="submit" variant="primary">
102 RestreamAddForm.propTypes = {
103 episode: PropTypes.shape({
104 event: PropTypes.shape({
105 title: PropTypes.string,
107 players: PropTypes.arrayOf(PropTypes.shape({
109 start: PropTypes.string,
111 errors: PropTypes.shape({
112 accept_comms: PropTypes.string,
113 accept_tracker: PropTypes.string,
114 channel_id: PropTypes.string,
116 handleBlur: PropTypes.func,
117 handleChange: PropTypes.func,
118 handleSubmit: PropTypes.func,
119 onCancel: PropTypes.func,
120 touched: PropTypes.shape({
121 accept_comms: PropTypes.bool,
122 accept_tracker: PropTypes.bool,
123 channel_id: PropTypes.bool,
125 user: PropTypes.shape({
126 channel_crews: PropTypes.arrayOf(PropTypes.shape({
129 values: PropTypes.shape({
130 accept_comms: PropTypes.bool,
131 accept_tracker: PropTypes.bool,
132 channel_id: PropTypes.number,
136 export default withUser(withFormik({
137 displayName: 'RestreamAddForm',
138 enableReinitialize: true,
139 handleSubmit: async (values, actions) => {
140 const { setErrors } = actions;
141 const { onSubmit } = actions.props;
143 await onSubmit(values);
145 if (e.response && e.response.data && e.response.data.errors) {
146 setErrors(laravelErrorsToFormik(e.response.data.errors));
150 mapPropsToValues: ({ episode, user }) => ({
152 accept_tracker: false,
153 channel_id: user && user.channel_crews && user.channel_crews.length
154 ? user.channel_crews[0].channel_id : 0,
155 episode_id: episode ? episode.id : 0,
157 })(RestreamAddForm));