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 DialogEpisode from './DialogEpisode';
8 import laravelErrorsToFormik from '../../helpers/laravelErrorsToFormik';
9 import { applicableChannels } from '../../helpers/permissions';
10 import { withUser } from '../../hooks/user';
24 const { t } = useTranslation();
26 const available_channels = React.useMemo(() => {
27 return applicableChannels(user, episode, as);
28 }, [as, episode, user]);
30 return <Form noValidate onSubmit={handleSubmit}>
32 <DialogEpisode episode={episode} />
33 <Form.Group controlId="apply.role">
34 <Form.Label>{t('episodes.applyDialog.signUpAs')}</Form.Label>
38 value={t(`crew.roles.${as}`)}
41 <Form.Group controlId="apply.channel_id">
42 <Form.Label>{t('episodes.channel')}</Form.Label>
44 isInvalid={!!(touched.channel_id && errors.channel_id)}
47 onChange={handleChange}
48 value={values.channel_id || 0}
50 <option disabled value={0}>{t('general.pleaseSelect')}</option>
51 {available_channels.map(c =>
52 <option key={c.id} value={c.id}>
57 {touched.channel_id && errors.channel_id ?
58 <Form.Control.Feedback type="invalid">
59 {t(errors.channel_id)}
60 </Form.Control.Feedback>
66 <Button onClick={onCancel} variant="secondary">
70 <Button type="submit" variant="primary">
77 ApplyForm.propTypes = {
79 episode: PropTypes.shape({
81 errors: PropTypes.shape({
82 channel_id: PropTypes.string,
84 handleBlur: PropTypes.func,
85 handleChange: PropTypes.func,
86 handleSubmit: PropTypes.func,
87 onCancel: PropTypes.func,
88 touched: PropTypes.shape({
89 channel_id: PropTypes.bool,
91 user: PropTypes.shape({
93 values: PropTypes.shape({
94 channel_id: PropTypes.number,
98 export default withUser(withFormik({
99 displayName: 'ApplyForm',
100 enableReinitialize: true,
101 handleSubmit: async (values, actions) => {
102 const { setErrors } = actions;
103 const { onSubmit } = actions.props;
105 await onSubmit(values);
107 if (e.response && e.response.data && e.response.data.errors) {
108 setErrors(laravelErrorsToFormik(e.response.data.errors));
112 mapPropsToValues: ({ as, episode, user }) => {
113 const channels = applicableChannels(user, episode, as);
116 channel_id: channels.length ? channels[0].id : 0,
117 episode_id: episode ? episode.id : 0,