all repos — caroster @ 44a9c4d6a42c23de9ece77ee2968e31c3ea93732

[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';
10
11type Props = {
12  paymentLink: string;
13};
14
15const PlusAction = (props: Props) => {
16  const {paymentLink} = props;
17  const {t} = useTranslation();
18  const session = useSession();
19  const isAuthenticated = session.status === 'authenticated';
20  const {locale} = useLocale();
21  const event = useEventCreationStore(s => s.event);
22  const [createEvent] = useCreateEventMutation();
23  const {addToEvent: addToUserEvents} = useAddToEvents();
24  const profile = session?.data?.profile;
25
26  const onClick = async () => {
27    try {
28      const {data} = await createEvent({
29        variables: {
30          eventData: {
31            ...event,
32            lang: locale,
33            email: event.email || profile?.email,
34            unpaid: true,
35          },
36        },
37        refetchQueries: [ProfileDocument],
38      });
39      const createdEvent = data.createEvent.data;
40      addToUserEvents(createdEvent.id);
41      useEventCreationStore.persist.clearStorage();
42      setCookie('redirectPath', `/${locale}/e/${createdEvent.attributes.uuid}`);
43      window.location.href = `${paymentLink}?client_reference_id=${createdEvent.attributes.uuid}&locale=${locale}&prefilled_email=${profile?.email}`;
44    } catch (error) {
45      console.error(error);
46    }
47  };
48
49  if (isAuthenticated)
50    return (
51      <Button
52        fullWidth
53        variant="outlined"
54        onClick={onClick}
55      >{t`event.creation.plus.button`}</Button>
56    );
57  else
58    return (
59      <Link href={`/auth/login?redirectPath=/new/type/`} passHref>
60        <Button variant="outlined" fullWidth>{t`signin.title`}</Button>
61      </Link>
62    );
63};
64
65export default PlusAction;