app/src/pages/Event.js (view raw)
1import React, {useState, useReducer, useEffect} from 'react';
2import {useTranslation} from 'react-i18next';
3import AppBar from '@material-ui/core/AppBar';
4import Toolbar from '@material-ui/core/Toolbar';
5import Container from '@material-ui/core/Container';
6import Typography from '@material-ui/core/Typography';
7import IconButton from '@material-ui/core/IconButton';
8import Icon from '@material-ui/core/Icon';
9import {makeStyles} from '@material-ui/core/styles';
10import {useEvent, EventProvider} from '../contexts/Event';
11import {useToast} from '../contexts/Toast';
12import Layout from '../layouts/Default';
13import Loading from '../pages/Loading';
14import EventMenu from '../containers/EventMenu';
15import EventDetails from '../containers/EventDetails';
16import EventFab from '../containers/EventFab';
17import CarColumns from '../containers/CarColumns';
18import NewCarDialog from '../containers/NewCarDialog';
19
20const Event = () => {
21 const {t} = useTranslation();
22 const {addToast} = useToast();
23 const [anchorEl, setAnchorEl] = useState(null);
24 const [detailsOpen, toggleDetails] = useReducer(i => !i, false);
25 const classes = useStyles({detailsOpen});
26 const [openNewCar, toggleNewCar] = useReducer(i => !i, false);
27 const {event, isEditing, setIsEditing, updateEvent} = useEvent();
28
29 useEffect(() => {
30 window.scrollTo(0, 0);
31 }, []);
32
33 useEffect(() => {
34 if (!detailsOpen) setIsEditing(false);
35 }, [detailsOpen]); // eslint-disable-line react-hooks/exhaustive-deps
36
37 const onEventSave = async e => {
38 try {
39 await updateEvent();
40 setIsEditing(false);
41 } catch (error) {
42 console.error(error);
43 addToast(t('event.errors.cant_update'));
44 }
45 };
46
47 const onShare = async () => {
48 if (!event) return null;
49 // If navigator as share capability
50 if (!!navigator.share)
51 return await navigator.share({
52 title: `Caroster ${event.name}`,
53 url: `${window.location.href}`,
54 });
55 // Else copy URL in clipboard
56 else if (!!navigator.clipboard) {
57 await navigator.clipboard.writeText(window.location.href);
58 addToast(t('event.actions.copied'));
59 return true;
60 }
61 };
62
63 if (!event) return <Loading />;
64
65 return (
66 <Layout>
67 <AppBar
68 position="static"
69 color="primary"
70 className={classes.appbar}
71 id={(isEditing && 'EditEvent') || (detailsOpen && 'Details') || 'Menu'}
72 >
73 <Toolbar>
74 <div className={classes.name}>
75 <Typography variant="h6" noWrap id="MenuHeaderTitle">
76 {event.name}
77 </Typography>
78 {detailsOpen && !isEditing && (
79 <IconButton
80 color="inherit"
81 edge="end"
82 id="CloseDetailsBtn"
83 onClick={() => setIsEditing(true)}
84 >
85 <Icon>edit</Icon>
86 </IconButton>
87 )}
88 {detailsOpen && isEditing && (
89 <IconButton
90 color="inherit"
91 edge="end"
92 id="EditEventSubmit"
93 onClick={onEventSave}
94 >
95 <Icon>done</Icon>
96 </IconButton>
97 )}
98 </div>
99 {!detailsOpen && (
100 <>
101 <IconButton
102 color="inherit"
103 edge="end"
104 id="ShareBtn"
105 onClick={onShare}
106 className={classes.shareIcon}
107 >
108 <Icon>share</Icon>
109 </IconButton>
110 <IconButton
111 color="inherit"
112 edge="end"
113 id="MenuMoreInfo"
114 onClick={e => setAnchorEl(e.currentTarget)}
115 >
116 <Icon>more_vert</Icon>
117 </IconButton>
118 </>
119 )}
120 {detailsOpen && (
121 <IconButton
122 color="inherit"
123 edge="end"
124 id="CloseDetailsBtn"
125 onClick={() => {
126 setIsEditing(false);
127 toggleDetails();
128 }}
129 >
130 <Icon>close</Icon>
131 </IconButton>
132 )}
133 <EventMenu
134 anchorEl={anchorEl}
135 setAnchorEl={setAnchorEl}
136 actions={[
137 {
138 label: detailsOpen
139 ? t('event.actions.hide_details')
140 : t('event.actions.show_details'),
141 onClick: toggleDetails,
142 id: 'DetailsTab',
143 },
144 {
145 label: t('event.actions.add_car'),
146 onClick: toggleNewCar,
147 id: 'NewCarTab',
148 },
149 {
150 label: t('event.actions.invite'),
151 onClick: () => {},
152 id: 'InviteTab',
153 },
154 ]}
155 />
156 </Toolbar>
157 <Container className={classes.container} maxWidth="sm">
158 <EventDetails toggleDetails={toggleDetails} />
159 </Container>
160 </AppBar>
161 <CarColumns toggleNewCar={toggleNewCar} />
162 <EventFab toggleNewCar={toggleNewCar} open={openNewCar} />
163 <NewCarDialog open={openNewCar} toggle={toggleNewCar} />
164 </Layout>
165 );
166};
167
168const useStyles = makeStyles(theme => ({
169 container: {
170 padding: theme.spacing(2),
171 },
172 appbar: ({detailsOpen}) => ({
173 overflow: 'hidden',
174 height: detailsOpen ? '100vh' : theme.mixins.toolbar.minHeight,
175 transition: 'height 0.3s ease',
176 zIndex: theme.zIndex.appBar,
177 position: 'fixed',
178 top: 0,
179 }),
180 name: {
181 flexGrow: 1,
182 display: 'flex',
183 alignItems: 'center',
184 },
185 shareIcon: {
186 marginRight: theme.spacing(0),
187 },
188}));
189
190const EventWithContext = props => (
191 <EventProvider {...props}>
192 <Event {...props} />
193 </EventProvider>
194);
195export default EventWithContext;