frontend/containers/CreateEvent/Step1.tsx (view raw)
1import React, {useState, useEffect, useMemo} from 'react';
2import {makeStyles} from '@material-ui/core/styles';
3import Typography from '@material-ui/core/Typography';
4import TextField from '@material-ui/core/TextField';
5import Button from '@material-ui/core/Button';
6import Checkbox from '@material-ui/core/Checkbox';
7import FormControlLabel from '@material-ui/core/FormControlLabel';
8import {useTranslation} from 'react-i18next';
9import useDebounce from '../../hooks/useDebounce';
10import useProfile from '../../hooks/useProfile';
11import {CardActions} from '@material-ui/core';
12import {isValidEmail} from '../../lib/formValidation';
13
14const Step1 = ({nextStep, event, addToEvent}) => {
15 const {t} = useTranslation();
16 const {connected, user} = useProfile();
17 const classes = useStyles({connected});
18
19 // States
20 const [name, setName] = useState(event.name ?? '');
21 const [email, setEmail] = useState(event.email ?? '');
22 const [emailIsValid, setEmailIsValid] = useState(false);
23 const [newsletter, setNewsletter] = useState(false);
24 const debouncedEmail = useDebounce(email, 400);
25
26 useEffect(() => {
27 setEmailIsValid(isValidEmail(debouncedEmail));
28 }, [debouncedEmail]);
29
30 const canSubmit = useMemo(() => {
31 const n = name.length > 0;
32 const e = email.length > 0 && emailIsValid;
33 return connected ? n : n && e;
34 }, [name, email, emailIsValid, connected]);
35
36 const onNext = submitEvent => {
37 if (submitEvent.preventDefault) submitEvent.preventDefault();
38 addToEvent({
39 name,
40 email: connected ? user.email : email,
41 newsletter: connected ? true : newsletter,
42 });
43 nextStep();
44 return false;
45 };
46
47 return (
48 <form onSubmit={onNext}>
49 <TextField
50 label={t('event.creation.name')}
51 fullWidth
52 autoFocus
53 margin="dense"
54 value={name}
55 onChange={e => setName(e.target.value)}
56 id="NewEventName"
57 name="name"
58 />
59 {!connected && (
60 <>
61 <TextField
62 label={t('event.creation.creator_email')}
63 fullWidth
64 value={email}
65 onChange={e => setEmail(e.target.value)}
66 name="email"
67 type="email"
68 id="NewEventEmail"
69 />
70 <FormControlLabel
71 className={classes.newsletter}
72 label={t('event.creation.newsletter')}
73 control={
74 <Checkbox
75 name="newsletter"
76 color="primary"
77 id="NewEventNewsletter"
78 checked={newsletter}
79 onChange={e => setNewsletter(e.target.checked)}
80 />
81 }
82 />
83 </>
84 )}
85 <Button
86 className={classes.button}
87 type="submit"
88 variant="contained"
89 color="secondary"
90 fullWidth
91 disabled={!canSubmit}
92 aria-disabled={!canSubmit}
93 >
94 {t('event.creation.next')}
95 </Button>
96
97 {!connected && (
98 <div className={classes.addFromAccountSection}>
99 <Typography variant="body1">
100 {t('event.creation.addFromAccount.title')}
101 </Typography>
102 <Typography variant="body2">
103 {t('event.creation.addFromAccount.subtitle')}
104 </Typography>
105 <CardActions className={classes.actions}>
106 <Button variant="text" href="/auth/register">
107 {t('event.creation.addFromAccount.actions.register')}
108 </Button>
109 <Button color="primary" href="/auth/login">
110 {t('event.creation.addFromAccount.actions.login')}
111 </Button>
112 </CardActions>
113 </div>
114 )}
115 </form>
116 );
117};
118
119const useStyles = makeStyles(theme => ({
120 button: {
121 marginTop: theme.spacing(2),
122 },
123 newsletter: {
124 marginTop: theme.spacing(2),
125 },
126 addFromAccountSection: {
127 marginTop: theme.spacing(8),
128 textAlign: 'center',
129 },
130 actions: {
131 marginTop: theme.spacing(1),
132 justifyContent: 'space-evenly',
133 textAlign: 'center',
134 },
135}));
136
137export default Step1;