diff --git a/src/app/(withGlobalData)/layout.tsx b/src/app/(withGlobalData)/layout.tsx new file mode 100644 index 0000000..338fa39 --- /dev/null +++ b/src/app/(withGlobalData)/layout.tsx @@ -0,0 +1,15 @@ +import { getMovies } from "@/lib/db"; +import { GlobalStoreProvider } from "../store/globalStore"; + +export const revalidate = 0; +export default async function WithGlobalDataLayout({ + children, +}: { + children: React.ReactNode; +}) { + const movies = await getMovies(); + + return ( + {children} + ); +} diff --git a/src/app/page.tsx b/src/app/(withGlobalData)/page.tsx similarity index 100% rename from src/app/page.tsx rename to src/app/(withGlobalData)/page.tsx diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 79ff54d..2dca72d 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -2,7 +2,6 @@ import type { Metadata } from "next"; import { Geist, Geist_Mono } from "next/font/google"; import "./globals.css"; import { Navbar } from "@/components/organisms/Navbar"; -import { GlobalStoreProvider } from "./store/globalStore"; const geistSans = Geist({ variable: "--font-geist-sans", @@ -29,7 +28,7 @@ export default async function RootLayout({ - {children} + {children} ); diff --git a/src/app/store/globalStore.tsx b/src/app/store/globalStore.tsx index f8eda30..35d84a1 100644 --- a/src/app/store/globalStore.tsx +++ b/src/app/store/globalStore.tsx @@ -1,7 +1,6 @@ "use client"; -import { getMovies } from "@/lib/db"; import { movies } from "@/lib/db/schema"; -import { createContext, FC, use, useEffect, useState } from "react"; +import { createContext, FC, use, useState } from "react"; type Movie = typeof movies.$inferSelect; @@ -21,16 +20,11 @@ const globalStore = createContext({ type Props = { children: React.ReactNode; + initialMovies: Movie[]; }; -export const GlobalStoreProvider: FC = ({ children }) => { - const [movies, setMovies] = useState([]); - - useEffect(() => { - getMovies().then((movies) => { - setMovies(movies); - }); - }, []); +export const GlobalStoreProvider: FC = ({ children, initialMovies }) => { + const [movies, setMovies] = useState(initialMovies); const addMovie = async (movie: Movie) => { if (movies.find((m) => m.id === movie.id)) return;