All files / app/features/communities/events CreateEventPage.tsx

97.61% Statements 41/42
92.85% Branches 13/14
87.5% Functions 7/8
97.56% Lines 40/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 1541x 1x 1x 1x 1x 1x   1x 1x 1x   1x 1x 1x   1x 1x 1x         1x   3x           1x 18x 18x 18x     18x       18x       18x         18x               3x 3x 3x         3x           3x 1x                       2x                         3x       3x         3x         3x     3x           18x   18x                             35x                                  
import { Typography } from "@material-ui/core";
import Button from "components/Button";
import HtmlMeta from "components/HtmlMeta";
import ProfileIncompleteDialog from "components/ProfileIncompleteDialog/ProfileIncompleteDialog";
import useAccountInfo from "features/auth/useAccountInfo";
import { communityEventsBaseKey } from "features/queryKeys";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { COMMUNITIES, GLOBAL } from "i18n/namespaces";
import { useRouter } from "next/router";
import { Event } from "proto/events_pb";
import { useMutation, useQueryClient } from "react-query";
import { dashboardRoute, routeToEvent } from "routes";
import { service } from "service";
import type { CreateEventInput } from "service/events";
import dayjs, { TIME_FORMAT } from "utils/dayjs";
import makeStyles from "utils/makeStyles";
import stringOrFirstString from "utils/stringOrFirstString";
 
import EventForm, {
  CreateEventVariables,
  useEventFormStyles,
} from "./EventForm";
 
const useStyles = makeStyles((theme) => ({
  disclaimer: {
    color: theme.palette.grey[600],
  },
}));
 
export default function CreateEventPage() {
  const { t } = useTranslation([GLOBAL, COMMUNITIES]);
  const classes = { ...useEventFormStyles(), ...useStyles() };
  const router = useRouter();
 
  const urlCommunityIdString =
    typeof window !== "undefined"
      ? stringOrFirstString(router.query.communityId)
      : undefined;
  const urlCommunityId =
    urlCommunityIdString && !isNaN(Number.parseInt(urlCommunityIdString))
      ? Number.parseInt(urlCommunityIdString)
      : undefined;
 
  const queryClient = useQueryClient();
  const {
    mutate: createEvent,
    error,
    isLoading,
  } = useMutation<
    Event.AsObject,
    RpcError,
    CreateEventVariables,
    { parentCommunityId?: number }
  >(
    (data) => {
      let createEventInput: CreateEventInput;
      const startTime = dayjs(data.startTime, TIME_FORMAT);
      const endTime = dayjs(data.endTime, TIME_FORMAT);
      const finalStartDate = data.startDate
        .startOf("day")
        .add(startTime.get("hour"), "hour")
        .add(startTime.get("minute"), "minute")
        .toDate();
      const finalEndDate = data.endDate
        .startOf("day")
        .add(endTime.get("hour"), "hour")
        .add(endTime.get("minute"), "minute")
        .toDate();
 
      if (data.isOnline) {
        createEventInput = {
          isOnline: data.isOnline,
          title: data.title,
          content: data.content,
          photoKey: data.eventImage,
          startTime: finalStartDate,
          endTime: finalEndDate,
          // TODO: not hardcode this and allow user to specify community ID?
          parentCommunityId: 1,
          link: data.link,
        };
      } else {
        createEventInput = {
          isOnline: data.isOnline,
          title: data.title,
          content: data.content,
          photoKey: data.eventImage,
          startTime: finalStartDate,
          endTime: finalEndDate,
          address: data.location.name,
          lat: data.location.location.lat,
          lng: data.location.location.lng,
          parentCommunityId: urlCommunityId,
        };
      }
      return service.events.createEvent(createEventInput);
    },
    {
      onMutate({ parentCommunityId }) {
        return {
          parentCommunityId: parentCommunityId ?? urlCommunityId,
        };
      },
      onSuccess(event, __, context) {
        queryClient.invalidateQueries(
          context?.parentCommunityId
            ? [communityEventsBaseKey, context.parentCommunityId]
            : communityEventsBaseKey
        );
        router.push(routeToEvent(event.eventId, event.slug));
      },
      onSettled() {
        window.scroll({ top: 0, behavior: "smooth" });
      },
    }
  );
 
  const { data: accountInfo, isLoading: isAccountInfoLoading } =
    useAccountInfo();
 
  return (
    <>
      <HtmlMeta title={t("communities:create_event_page_title")} />
      <ProfileIncompleteDialog
        open={!isAccountInfoLoading && !accountInfo?.profileComplete}
        onClose={() => router.push(dashboardRoute)}
        attempted_action="create_event"
      />
      <EventForm
        error={error}
        isMutationLoading={isLoading}
        mutate={createEvent}
        title={t("communities:create_event_page_title")}
      >
        {({ isMutationLoading }) => (
          <>
            <Button
              className={classes.submitButton}
              loading={isMutationLoading}
              type="submit"
            >
              {t("global:create")}
            </Button>
            <Typography className={classes.disclaimer} variant="body1">
              {t("communities:create_event_disclaimer")}
            </Typography>
          </>
        )}
      </EventForm>
    </>
  );
}