import React, {useState, useReducer, useEffect} from 'react'; import {useTranslation} from 'react-i18next'; import {useAuth} from 'strapi-react-context'; import AppBar from '@material-ui/core/AppBar'; import Toolbar from '@material-ui/core/Toolbar'; import Container from '@material-ui/core/Container'; import Typography from '@material-ui/core/Typography'; import IconButton from '@material-ui/core/IconButton'; import Icon from '@material-ui/core/Icon'; import {makeStyles} from '@material-ui/core/styles'; import {useEvent, EventProvider} from '../contexts/Event'; import {useToast} from '../contexts/Toast'; import Layout from '../layouts/Default'; import Loading from './Loading'; import EventMenu from '../containers/EventMenu'; import EventDetails from '../containers/EventDetails'; import EventFab from '../containers/EventFab'; import CarColumns from '../containers/CarColumns'; import NewCarDialog from '../containers/NewCarDialog'; import AddToMyEventDialog from '../containers/AddToMyEventDialog'; import {useHistory} from 'react-router-dom'; const Event = () => { const {t} = useTranslation(); const history = useHistory(); const {addToast} = useToast(); const [anchorEl, setAnchorEl] = useState(null); const [isAddToMyEvent, setIsAddToMyEvent] = useState(false); const [detailsOpen, toggleDetails] = useReducer(i => !i, false); const classes = useStyles({detailsOpen}); const [openNewCar, toggleNewCar] = useReducer(i => !i, false); const {event, isEditing, setIsEditing, updateEvent} = useEvent(); const {token} = useAuth(); useEffect(() => { window.scrollTo(0, 0); }, []); useEffect(() => { if (!detailsOpen) setIsEditing(false); }, [detailsOpen]); // eslint-disable-line react-hooks/exhaustive-deps const onEventSave = async e => { try { await updateEvent(); setIsEditing(false); } catch (error) { console.error(error); addToast(t('event.errors.cant_update')); } }; const onShare = async () => { if (!event) return null; // If navigator as share capability if (!!navigator.share) return await navigator.share({ title: `Caroster ${event.name}`, url: `${window.location.href}`, }); // Else copy URL in clipboard else if (!!navigator.clipboard) { await navigator.clipboard.writeText(window.location.href); addToast(t('event.actions.copied')); return true; } }; const addToMyEvents = () => { if (!event) return; window.localStorage.setItem('addToMyEvents', event.id); setIsAddToMyEvent(true); }; const signUp = () => { if (!event) return; history.push({ pathname: '/register', state: {event: event.id}, }); }; const signIn = history.push.bind(undefined, '/login'); const goToDashboard = history.push.bind(undefined, '/dashboard'); const goProfile = history.push.bind(undefined, '/profile'); const goAbout = () => (window.location.href = t('meta.about_href')); if (!event) return ; return (
{event.name} {detailsOpen && !isEditing && ( setIsEditing(true)} > edit )} {detailsOpen && isEditing && ( done )}
{!detailsOpen && ( <> share setAnchorEl(e.currentTarget)} > more_vert )} {detailsOpen && ( { setIsEditing(false); toggleDetails(); }} > close )}
setIsAddToMyEvent(false)} event={event} />
); }; const useStyles = makeStyles(theme => ({ container: { padding: theme.spacing(2), }, appbar: ({detailsOpen}) => ({ overflow: 'hidden', height: detailsOpen ? '100vh' : theme.mixins.toolbar.minHeight, transition: 'height 0.3s ease', zIndex: theme.zIndex.appBar, position: 'fixed', top: 0, }), name: { flexGrow: 1, display: 'flex', alignItems: 'center', }, shareIcon: { marginRight: theme.spacing(0), }, })); const EventWithContext = props => ( ); export default EventWithContext;