Skip to content

Commit

Permalink
remove unused funcitons, rewrite homepage route to use draft mode
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanKiral committed Jan 15, 2025
1 parent 8fee46b commit 7a4a391
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 35 deletions.
13 changes: 3 additions & 10 deletions app/api/homepage/route.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { NextApiHandler } from "next";
import { parseBoolean } from "../../../lib/utils/parseBoolean";
import { envIdCookieName, previewApiKeyCookieName } from "../../../lib/constants/cookies";
import { getHomepage } from "../../../lib/kontentClient";
import { NextRequest } from "next/server";
import { cookies } from "next/headers";
import { cookies, draftMode } from "next/headers";

export const GET = async (req: NextRequest) => {
const searchParams = req.nextUrl.searchParams;
const usePreview = parseBoolean(searchParams.get("preview"));

if (usePreview === null) {
return new Response("Please provide 'preview' query parameter with value 'true' or 'false'.", {status: 400});
}
export const GET = async () => {
const usePreview = (await draftMode()).isEnabled

const cookiesList = await cookies();

Expand Down
2 changes: 1 addition & 1 deletion components/landingPage/ui/previewHomepage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const PreviewHomepage: FC<HomepageProps> = ({homepageData}) => {

useEffect(() => {
const getHomepage = async () => {
const response = await fetch(`/api/homepage?preview=true`);
const response = await fetch(`/api/homepage`);
const data = await response.json();

setHomepage(parseFlatted(stringifyAsType(data)));
Expand Down
19 changes: 1 addition & 18 deletions lib/utils/pageUtils.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
import { getCookie } from "cookies-next";
import { GetStaticPropsContext, PreviewData } from "next";

import { defaultCookieOptions, envIdCookieName, previewApiKeyCookieName } from "../constants/cookies";
import { defaultEnvId } from "./env";

export const getEnvIdFromRouteParams = (context: GetStaticPropsContext): string => {
const envId = context.params?.envId;
const isEnvIdPresent = typeof envId === "string";
if (!isEnvIdPresent) {
console.warn("No envId in the route. Falling back to envId from the environment variable.")
}

return isEnvIdPresent ? envId : defaultEnvId;
}

export const getPreviewApiKeyFromPreviewData = (previewData: PreviewData | undefined) =>
previewData && typeof previewData === 'object' && previewApiKeyCookieName in previewData
? previewData.currentPreviewApiKey as string
: undefined;
import { defaultCookieOptions, envIdCookieName } from "../constants/cookies";

export const getEnvIdFromCookie = () => getCookie(envIdCookieName, defaultCookieOptions);
6 changes: 0 additions & 6 deletions lib/utils/parseBoolean.ts

This file was deleted.

0 comments on commit 7a4a391

Please sign in to comment.