all repos — caroster @ v8.0

[Octree] Group carpool to your event https://caroster.io

frontend/containers/EventTypeCard/PlusAction.tsx (view raw)

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