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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 3x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 2x 2x 2x 2x 2x 2x 2x 8x 8x 8x | import { styled, Typography } from "@mui/material";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import Button from "components/Button";
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import HeaderButton from "components/HeaderButton";
import HtmlMeta from "components/HtmlMeta";
import { BackIcon } from "components/Icons";
import ProfileIncompleteDialog from "components/ProfileIncompleteDialog/ProfileIncompleteDialog";
import useAccountInfo from "features/auth/useAccountInfo";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { COMMUNITIES, GLOBAL, PROFILE } from "i18n/namespaces";
import { useRouter } from "next/router";
import { Event } from "proto/events_pb";
import { dashboardRoute, eventsRoute, routeToEvent } from "routes";
import { service } from "service";
import type { CreateEventInput } from "service/events";
import { theme } from "theme";
import { sendNativeBack, useIsNativeEmbed } from "utils/nativeLink";
import stringOrFirstString from "utils/stringOrFirstString";
import { communityEventsBaseKey } from "../../queryKeys";
import EventForm, { CreateEventVariables } from "./EventForm";
import { useEvent } from "./hooks";
const StyledBackButton = styled(HeaderButton)(() => ({
gridArea: "backButton",
width: "2.5rem",
height: "2.5rem",
marginTop: theme.spacing(2),
}));
export default function CreateEventPage() {
const { t } = useTranslation([GLOBAL, COMMUNITIES, PROFILE]);
const router = useRouter();
const isNativeEmbed = useIsNativeEmbed();
const urlCommunityIdString =
typeof window !== "undefined"
? stringOrFirstString(router.query.communityId)
: undefined;
const urlCommunityId =
urlCommunityIdString && !isNaN(Number.parseInt(urlCommunityIdString))
? Number.parseInt(urlCommunityIdString)
: undefined;
const duplicateEventIdString =
typeof window !== "undefined"
? stringOrFirstString(router.query.duplicateEventId)
: undefined;
const duplicateEventId =
duplicateEventIdString && !isNaN(Number.parseInt(duplicateEventIdString))
? Number.parseInt(duplicateEventIdString)
: undefined;
const {
data: eventToDuplicate,
isLoading: isDuplicateEventLoading,
error: duplicateEventError,
} = useEvent({ eventId: duplicateEventId!, enabled: !!duplicateEventId });
const queryClient = useQueryClient();
const {
mutate: createEvent,
error,
isPending,
} = useMutation<
Event.AsObject,
RpcError,
CreateEventVariables,
{ parentCommunityId?: number }
>({
mutationFn: (data) => {
// Use uploaded photo, or reuse photo from event being duplicated
const photoKey =
data.eventImage || eventToDuplicate?.photoKey || undefined;
const createEventInput: CreateEventInput = {
title: data.title,
content: data.content,
photoKey,
startTime: data.startDate.toPlainDateTime(data.startTime),
endTime: data.endDate.toPlainDateTime(data.endTime),
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({
queryKey: [
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();
const handleBackClick = () => {
if (isNativeEmbed) {
sendNativeBack();
return;
}
if (window.history.length > 1) {
router.back();
} else {
router.push(eventsRoute);
}
};
Iif (isDuplicateEventLoading) {
return <CenteredSpinner />;
}
return (
<>
<HtmlMeta title={t("communities:create_event_page_title")} />
<ProfileIncompleteDialog
open={!isAccountInfoLoading && !accountInfo?.profileComplete}
onClose={() => router.push(dashboardRoute)}
attempted_action="create_event"
/>
<StyledBackButton onClick={handleBackClick}>
<BackIcon />
</StyledBackButton>
<EventForm
error={error || duplicateEventError}
isMutationLoading={isPending}
mutate={createEvent}
title={t("communities:create_event_page_title")}
isEdit={false}
event={eventToDuplicate}
>
{({ isMutationLoading }) => (
<>
<Button
loading={isMutationLoading}
type="submit"
sx={{ justifySelf: "start" }}
>
{t("global:create")}
</Button>
<Typography variant="body1" sx={{ color: theme.palette.grey[600] }}>
{t("communities:create_event_disclaimer")}
</Typography>
</>
)}
</EventForm>
</>
);
}
|