1 import axios from 'axios';
2 import moment from 'moment';
3 import React from 'react';
4 import { Container } from 'react-bootstrap';
5 import { Helmet } from 'react-helmet';
6 import { withTranslation } from 'react-i18next';
7 import { useParams } from 'react-router-dom';
8 import toastr from 'toastr';
10 import NotFound from './NotFound';
11 import CanonicalLinks from '../components/common/CanonicalLinks';
12 import ErrorBoundary from '../components/common/ErrorBoundary';
13 import ErrorMessage from '../components/common/ErrorMessage';
14 import Loading from '../components/common/Loading';
15 import EpisodeList from '../components/episodes/List';
16 import Detail from '../components/events/Detail';
17 import Dialog from '../components/techniques/Dialog';
20 } from '../helpers/permissions';
21 import { useUser } from '../helpers/UserContext';
22 import i18n from '../i18n';
25 const params = useParams();
26 const { name } = params;
27 const user = useUser();
29 const [error, setError] = React.useState(null);
30 const [loading, setLoading] = React.useState(true);
31 const [event, setEvent] = React.useState(null);
33 const [editContent, setEditContent] = React.useState(null);
34 const [episodes, setEpisodes] = React.useState([]);
35 const [showContentDialog, setShowContentDialog] = React.useState(false);
37 const actions = React.useMemo(() => ({
38 editContent: mayEditContent(user) ? content => {
39 setEditContent(content);
40 setShowContentDialog(true);
44 const fetchEpisodes = React.useCallback((controller, event) => {
49 axios.get(`/api/episodes`, {
50 signal: controller.signal,
52 after: moment().subtract(3, 'hours').toISOString(),
53 before: moment().add(14, 'days').toISOString(),
57 setEpisodes(response.data || []);
59 if (!axios.isCancel(e)) {
65 const saveContent = React.useCallback(async values => {
67 const response = await axios.put(`/api/content/${values.id}`, {
68 parent_id: event.description_id,
71 toastr.success(i18n.t('content.saveSuccess'));
74 description: response.data,
76 setShowContentDialog(false);
78 toastr.error(i18n.t('content.saveError'));
80 }, [event && event.description_id]);
82 React.useEffect(() => {
83 const ctrl = new AbortController();
86 .get(`/api/events/${name}`, { signal: ctrl.signal })
90 setEvent(response.data);
102 React.useEffect(() => {
103 const controller = new AbortController();
104 fetchEpisodes(controller, event);
105 const timer = setInterval(() => {
106 fetchEpisodes(controller, event);
110 clearInterval(timer);
112 }, [event, fetchEpisodes]);
119 return <ErrorMessage error={error} />;
126 return <ErrorBoundary>
128 <title>{event.title}</title>
130 <CanonicalLinks base={`/events/${event.name}`} />
132 <Detail actions={actions} event={event} />
133 {episodes.length ? <>
134 <h2>{i18n.t('events.upcomingEpisodes')}</h2>
135 <EpisodeList episodes={episodes} />
139 content={editContent}
140 language={i18n.language}
141 onHide={() => { setShowContentDialog(false); }}
142 onSubmit={saveContent}
143 show={showContentDialog}
148 export default withTranslation()(Event);