frontend/containers/EventTypeCard/PlusAction.tsx (view raw)
1import {Button} from '@mui/material';
2import {useSession} from 'next-auth/react';
3import {useTranslation} from 'next-i18next';
4import useLocale from '../../hooks/useLocale';
5import useEventCreationStore from '../../stores/useEventCreationStore';
6import {ProfileDocument, useCreateEventMutation} from '../../generated/graphql';
7import useAddToEvents from '../../hooks/useAddToEvents';
8import {setCookie} from '../../lib/cookies';
9import {useRouter} from 'next/router';
10
11type Props = {};
12
13const PlusAction = (props: Props) => {
14 const {t} = useTranslation();
15 const session = useSession();
16 const router = useRouter();
17 const isAuthenticated = session.status === 'authenticated';
18 const {locale} = useLocale();
19 const event = useEventCreationStore(s => s.event);
20 const [createEvent] = useCreateEventMutation();
21 const {addToEvent: addToUserEvents} = useAddToEvents();
22 const profile = session?.data?.profile;
23
24 const onClick = async () => {
25 try {
26 const {data} = await createEvent({
27 variables: {
28 eventData: {
29 ...event,
30 lang: locale,
31 email: event.email || profile?.email,
32 unpaid: true,
33 },
34 },
35 refetchQueries: [ProfileDocument],
36 });
37 const createdEvent = data.createEvent.data;
38 useEventCreationStore.persist.clearStorage();
39
40 if (isAuthenticated) {
41 addToUserEvents(createdEvent.id);
42 setCookie(
43 'redirectPath',
44 `/${locale}/e/${createdEvent.attributes.uuid}`
45 );
46 router.push(
47 `/${locale}/new/prices?eventId=${createdEvent.attributes.uuid}`
48 );
49 } else {
50 setCookie(
51 'redirectPath',
52 `/${locale}/new/prices?eventId=${createdEvent.attributes.uuid}`
53 );
54 router.push('/auth/login');
55 }
56 } catch (error) {
57 console.error(error);
58 }
59 };
60
61 return (
62 <Button fullWidth variant="outlined" onClick={onClick}>
63 {t`event.creation.plus.button`}
64 </Button>
65 );
66};
67
68export default PlusAction;