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 { getName } from '../../helpers/Crew';
8 import laravelErrorsToFormik from '../../helpers/laravelErrorsToFormik';
9 import { withUser } from '../../helpers/UserContext';
11 const RestreamAddForm = ({
22 const { t } = useTranslation();
24 return <Form noValidate onSubmit={handleSubmit}>
31 {t('episodes.startTime', { date: new Date(episode.start) })}
34 {episode.players.map(p => getName(p)).join(', ')}
37 <Form.Group controlId="episodes.channel_id">
38 <Form.Label>{t('episodes.channel')}</Form.Label>
40 isInvalid={!!(touched.channel_id && errors.channel_id)}
43 onChange={handleChange}
44 value={values.channel_id || 0}
46 <option disabled value={0}>{t('general.pleaseSelect')}</option>
47 {((user && user.channel_crews) || []).map(c =>
48 <option key={c.id} value={c.channel_id}>
53 {touched.channel_id && errors.channel_id ?
54 <Form.Control.Feedback type="invalid">
55 {t(errors.channel_id)}
56 </Form.Control.Feedback>
62 <Button onClick={onCancel} variant="secondary">
66 <Button type="submit" variant="primary">
73 RestreamAddForm.propTypes = {
74 episode: PropTypes.shape({
75 event: PropTypes.shape({
76 title: PropTypes.string,
78 players: PropTypes.arrayOf(PropTypes.shape({
80 start: PropTypes.string,
82 errors: PropTypes.shape({
83 channel_id: PropTypes.string,
85 handleBlur: PropTypes.func,
86 handleChange: PropTypes.func,
87 handleSubmit: PropTypes.func,
88 onCancel: PropTypes.func,
89 touched: PropTypes.shape({
90 channel_id: PropTypes.bool,
92 user: PropTypes.shape({
93 channel_crews: PropTypes.arrayOf(PropTypes.shape({
96 values: PropTypes.shape({
97 channel_id: PropTypes.number,
101 export default withUser(withFormik({
102 displayName: 'RestreamAddForm',
103 enableReinitialize: true,
104 handleSubmit: async (values, actions) => {
105 const { setErrors } = actions;
106 const { onSubmit } = actions.props;
108 await onSubmit(values);
110 if (e.response && e.response.data && e.response.data.errors) {
111 setErrors(laravelErrorsToFormik(e.response.data.errors));
115 mapPropsToValues: ({ episode, user }) => ({
116 channel_id: user && user.channel_crews && user.channel_crews.length
117 ? user.channel_crews[0].channel_id : 0,
118 episode_id: episode ? episode.id : 0,
120 })(RestreamAddForm));