chore: App router migration - Fix /event-types/[type] page, move getServerSideProps to /lib for event-types pages (#16457)
* event-types: refactor and extract client code into /modules * revert * remove res in getServerSideProps * remove res in getServerSideProps 2 * Create EventTypeAppDir and fix error in event-types/[type] * add comments * fix type-check * fix 1 more type-check --------- Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
1 parent
976013512c
commit
d1a0fc6604
6 files changed
+774
-26
No files matched your search
@@ -1,67 +0,0 @@
|
||||
import type { GetServerSidePropsContext } from "next";
|
||||
|
||||
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
|
||||
import { asStringOrThrow } from "@lib/asStringOrNull";
|
||||
import type { inferSSRProps } from "@lib/types/inferSSRProps";
|
||||
|
||||
import { ssrInit } from "@server/lib/ssr";
|
||||
|
||||
export type PageProps = inferSSRProps<typeof getServerSideProps>;
|
||||
|
||||
export const getServerSideProps = async (context: GetServerSidePropsContext) => {
|
||||
const { req, res, query } = context;
|
||||
|
||||
const session = await getServerSession({ req, res });
|
||||
|
||||
const typeParam = parseInt(asStringOrThrow(query.type));
|
||||
const ssr = await ssrInit(context);
|
||||
|
||||
if (Number.isNaN(typeParam)) {
|
||||
const notFound = {
|
||||
notFound: true,
|
||||
} as const;
|
||||
|
||||
return notFound;
|
||||
}
|
||||
|
||||
if (!session?.user?.id) {
|
||||
const redirect = {
|
||||
redirect: {
|
||||
permanent: false,
|
||||
destination: "/auth/login",
|
||||
},
|
||||
} as const;
|
||||
return redirect;
|
||||
}
|
||||
const getEventTypeById = async (eventTypeId: number) => {
|
||||
await ssr.viewer.eventTypes.get.prefetch({ id: eventTypeId });
|
||||
try {
|
||||
const { eventType } = await ssr.viewer.eventTypes.get.fetch({ id: eventTypeId });
|
||||
return eventType;
|
||||
} catch (e: unknown) {
|
||||
logger.error(safeStringify(e));
|
||||
// reject, user has no access to this event type.
|
||||
return null;
|
||||
}
|
||||
};
|
||||
const eventType = await getEventTypeById(typeParam);
|
||||
if (!eventType) {
|
||||
const redirect = {
|
||||
redirect: {
|
||||
permanent: false,
|
||||
destination: "/event-types",
|
||||
},
|
||||
} as const;
|
||||
return redirect;
|
||||
}
|
||||
return {
|
||||
props: {
|
||||
eventType,
|
||||
type: typeParam,
|
||||
trpcState: ssr.dehydrate(),
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -1,20 +1,16 @@
|
||||
"use client";
|
||||
|
||||
import { EventType } from "@calcom/features/eventtypes/components/EventType";
|
||||
import { EventTypeAppDir } from "@calcom/features/eventtypes/components/EventTypeAppDir";
|
||||
import type { EventTypeSetupProps } from "@calcom/features/eventtypes/lib/types";
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-empty-function */
|
||||
// eslint-disable-next-line @calcom/eslint/deprecated-imports-next-router
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
|
||||
import type { AppProps } from "@lib/app-providers";
|
||||
import type { PageProps } from "@lib/event-types/[type]/getServerSideProps";
|
||||
|
||||
import { type PageProps } from "~/event-types/views/event-types-single-view.getServerSideProps";
|
||||
|
||||
const EventTypePageWrapper: React.FC<PageProps> & {
|
||||
PageWrapper?: AppProps["Component"]["PageWrapper"];
|
||||
getLayout?: AppProps["Component"]["getLayout"];
|
||||
} = (props) => {
|
||||
const EventTypePageWrapper = (props: PageProps & { isAppDir?: boolean }) => {
|
||||
const { data } = trpc.viewer.eventTypes.get.useQuery({ id: props.type });
|
||||
|
||||
if (!data) return null;
|
||||
@@ -36,7 +32,7 @@ const EventTypePageWrapper: React.FC<PageProps> & {
|
||||
allActiveWorkflows: workflows,
|
||||
};
|
||||
|
||||
return <EventType {...propsData} />;
|
||||
return props.isAppDir ? <EventTypeAppDir {...propsData} /> : <EventType {...propsData} />;
|
||||
};
|
||||
|
||||
export default EventTypePageWrapper;
|
||||
Reference in new issue
Block a user