From 9051d4887cd8549f3fe391e1b6bf564477bcce81 Mon Sep 17 00:00:00 2001 From: Norbert Maciaszek Date: Thu, 21 Aug 2025 17:52:17 +0200 Subject: [PATCH] feat: update layout to fetch and provide initial movie data; enhance GlobalStoreProvider to accept initialMovies prop for improved state management --- src/app/layout.tsx | 36 ++++++++++++++--------------------- src/app/store/globalStore.tsx | 18 +++++++----------- src/lib/db/index.ts | 5 +++++ 3 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 6a80a26..7d7a638 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -1,37 +1,29 @@ import "./globals.css"; -import type { Metadata } from "next"; -import { Geist, Geist_Mono } from "next/font/google"; import { Navbar } from "@/components/organisms/Navbar"; -import { GlobalStoreProvider } from "./store/globalStore"; import { AuroraBackground } from "@/components/effects"; - -const geistSans = Geist({ - variable: "--font-geist-sans", - subsets: ["latin"], -}); - -const geistMono = Geist_Mono({ - variable: "--font-geist-mono", - subsets: ["latin"], -}); - -export const metadata: Metadata = { - title: "Movie List", - description: "Personal watch list and progress tracker", -}; +import { GlobalStoreProvider } from "./store/globalStore"; +import { getMovies } from "@/lib/db"; export default async function RootLayout({ children, }: Readonly<{ children: React.ReactNode; }>) { + const movies = await getMovies(); + return ( - - + + Homepage | Movie List + + + + +
{children}
diff --git a/src/app/store/globalStore.tsx b/src/app/store/globalStore.tsx index 1dd44db..cd9d568 100644 --- a/src/app/store/globalStore.tsx +++ b/src/app/store/globalStore.tsx @@ -1,7 +1,7 @@ "use client"; import { addMovieToDB, deleteMovieFromDB, updateMovieInDB } 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,19 +21,15 @@ const globalStore = createContext({ type Props = { children: React.ReactNode; + initialMovies?: Movie[]; }; -export const GlobalStoreProvider: FC = ({ children }) => { +export const GlobalStoreProvider: FC = ({ + children, + initialMovies = [], +}) => { // Optimistic update - const [movies, setMovies] = useState([]); - - useEffect(() => { - fetch("/api/movies") - .then((res) => res.json()) - .then((data) => { - setMovies(data); - }); - }, []); + const [movies, setMovies] = useState(initialMovies); const addMovie = async (movie: Movie) => { if (movies.find((m) => m.id === movie.id)) return; diff --git a/src/lib/db/index.ts b/src/lib/db/index.ts index ec25d1e..11a12fd 100644 --- a/src/lib/db/index.ts +++ b/src/lib/db/index.ts @@ -3,6 +3,7 @@ import { drizzle } from "drizzle-orm/libsql"; import { movies } from "./schema"; import { eq } from "drizzle-orm"; import { Movie } from "@/types/global"; +import { revalidatePath } from "next/cache"; const db = drizzle(process.env.DB_FILE_NAME!); @@ -18,10 +19,13 @@ export const addMovieToDB = async (movie: Movie) => { genre_ids: JSON.stringify(movie.genre_ids), }) .onConflictDoNothing(); + + revalidatePath("/", "layout"); }; export const deleteMovieFromDB = async (id: number) => { await db.delete(movies).where(eq(movies.id, id)); + revalidatePath("/", "layout"); }; export const updateMovieInDB = async ( @@ -29,4 +33,5 @@ export const updateMovieInDB = async ( movie: Partial ) => { await db.update(movies).set(movie).where(eq(movies.id, movieId)); + revalidatePath("/", "layout"); };