Skip to content

Commit

Permalink
fix: dinamic calculation for pageModel based on url
Browse files Browse the repository at this point in the history
  • Loading branch information
gmolki committed May 30, 2024
1 parent ee3c0bc commit fe1172d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 15 deletions.
16 changes: 12 additions & 4 deletions src/hooks/useFetchBuilderContent.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { fetchBuilderData } from "@/utils/fetchBuilderData";
import { BuilderContent } from "@builder.io/sdk";
import { useRouter } from "next/router";
import { useCallback, useEffect, useState } from "react";
import { useCallback, useEffect, useMemo, useState } from "react";

interface FetchBuilderContentArgs {
pageModel: string;
preloadedPage: BuilderContent | null;
fetchContentFrom?: string;
}

interface FetchBuilderContentResponse {
pageModel: "page" | "blog-article";
content: BuilderContent | null;
isUpToDate: boolean;
isNotFound: boolean;
loading: boolean;
}

export function useFetchBuilderContent({
pageModel,
preloadedPage,
fetchContentFrom,
}: FetchBuilderContentArgs): FetchBuilderContentResponse {
Expand All @@ -27,6 +26,15 @@ export function useFetchBuilderContent({
const [loading, setLoading] = useState(true);
const router = useRouter();

const pageModel = useMemo(() => {
const [urlPath, _hash] =
(fetchContentFrom || router.asPath).split("#") || "/";

if (urlPath.indexOf("/blog/articles/") !== -1) return "blog-article";

return "page";
}, [fetchContentFrom, router.asPath]);

const fetchBuilderContent = useCallback(async () => {
const [urlPath, _hash] =
(fetchContentFrom || router.asPath).split("#") || "/";
Expand Down Expand Up @@ -71,5 +79,5 @@ export function useFetchBuilderContent({
fetchBuilderContent();
}, [fetchBuilderContent]);

return { content, isUpToDate, isNotFound, loading };
return { pageModel, content, isUpToDate, isNotFound, loading };
}
1 change: 0 additions & 1 deletion src/pages/[[...page]].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const getStaticProps: GetStaticProps = (async ({ params }) => {
.toPromise();

const pageProps: DynamicPageProps = {
pageModel: PAGE_MODEL,
page: page || null,
};

Expand Down
12 changes: 5 additions & 7 deletions src/pages/_dynamicPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,22 @@ import MainContentContainer from "@/components/MainContentContainer";
import { useFetchBuilderContent } from "@/hooks/useFetchBuilderContent";

export type DynamicPageProps = {
pageModel?: string;
page: BuilderContent | null;
fetchContentFrom?: string;
};

export default function DynamicPage({
pageModel = "page",
page,
fetchContentFrom,
}: DynamicPageProps) {
const router = useRouter();
const isPreviewing = useIsPreviewing();

const { content, isUpToDate, isNotFound, loading } = useFetchBuilderContent({
pageModel,
preloadedPage: page,
fetchContentFrom,
});
const { pageModel, content, isUpToDate, isNotFound, loading } =
useFetchBuilderContent({
preloadedPage: page,
fetchContentFrom,
});

useEffect(() => {
async function scrollToElement() {
Expand Down
1 change: 0 additions & 1 deletion src/pages/blog/articles/[blogArticle].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const getStaticProps: GetStaticProps = (async ({ params }) => {
.toPromise();

const pageProps: DynamicPageProps = {
pageModel: PAGE_MODEL,
page: page || null,
};

Expand Down
1 change: 0 additions & 1 deletion src/pages/blog/categories/[category].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const getStaticProps: GetStaticProps = (async () => {
.toPromise();

const pageProps: DynamicPageProps = {
pageModel: PAGE_MODEL,
page: page || null,
fetchContentFrom: FETCH_CONTENT_FROM,
};
Expand Down
1 change: 0 additions & 1 deletion src/pages/blog/tags/[tag].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const getStaticProps: GetStaticProps = (async () => {
.toPromise();

const pageProps: DynamicPageProps = {
pageModel: PAGE_MODEL,
page: page || null,
fetchContentFrom: FETCH_CONTENT_FROM,
};
Expand Down

0 comments on commit fe1172d

Please sign in to comment.