wip - got context working with favorites page using interleaving

This commit is contained in:
cscough 2024-05-24 15:51:51 -04:00
parent 9acb10e643
commit acca5c387f
7 changed files with 48 additions and 19 deletions

View File

@ -1,15 +1,15 @@
'use client'
import { FavoriteContext } from "@/components/FavoriteContext";
import { useState } from "react";
import { useContext } from "react";
export default () => {
const [favorites, setFavorites] = useState([]);
const {favorites} = useContext(FavoriteContext)
return (
<FavoriteContext.Provider value={{ favorites, setFavorites }}>
<ul>
{favorites.map((f, i) => (
<li key={i}>{f}</li>
<li key={i}>{f.Title}</li>
))}
</ul>
</FavoriteContext.Provider>
);
};

View File

@ -1,9 +1,10 @@
import FavoriteList from "./favoriteList";
export default () => {
// TODO: add context provider
return (
<div>
<h1>Your Favorites!</h1>
<FavoriteList/>
</div>
);
};

View File

@ -41,6 +41,7 @@ export default ({ movie }: IMovieCardProps) => {
<h3 className="mb-2 text-lg font-semibold">{movie.Title}</h3>
<p className="mb-2 text-sm text-gray-700">Circa: {movie.Year}</p>
</div>
<div className="flex gap-3">
<Link
href={`/Movie?i=${movie.imdbID}`}
className="block rounded-lg bg-purple-500 px-4 py-2 text-center font-semibold text-white hover:bg-purple-600"
@ -48,6 +49,7 @@ export default ({ movie }: IMovieCardProps) => {
Movie Details
</Link>
<FavoriteBtn movie={movie} />
</div>
</ImgCard>
);
};

View File

@ -16,6 +16,7 @@ export const stringifyQueryParams = (searchParams: object) =>
export default async ({ searchParams }: INextJsProps) => {
const queryParamString = stringifyQueryParams(searchParams!);
console.log(queryParamString)
const { Search } = await queryOMDb(queryParamString);
const movies = Search || [];

View File

@ -2,6 +2,7 @@ import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";
import NavBar from "@/components/navBar";
import { FavoriteProvider } from "@/components/FavoriteContext";
const inter = Inter({ subsets: ["latin"] });
@ -19,7 +20,9 @@ export default function RootLayout({
<html lang="en">
<body className={`${inter.className} mb-4`}>
<NavBar />
<FavoriteProvider>
{children}
</FavoriteProvider>
</body>
</html>
);

View File

@ -5,13 +5,20 @@ import { FavoriteContext } from "./FavoriteContext";
export default ({ movie }: any) => {
const { favorites, setFavorites } = useContext(FavoriteContext);
console.log("favorites", favorites);
const filteredFavorites = favorites.filter(f => f.imdbID != movie.imdbID)
const isFavorite = favorites.filter(f => f.imdbID == movie.imdbID).length == 1
return (
<button
onClick={() => {
if (isFavorite) {
setFavorites([...filteredFavorites])
} else {
setFavorites([...favorites, movie]);
}
}}
className="block text-yellow-500"
className={`block text-yellow-500 rounded my-auto w-6 ${isFavorite ? 'bg-black' : 'bg-white'}`}
>
</button>

View File

@ -1,5 +1,20 @@
import { createContext } from "react";
export const FavoriteContext = createContext({
'use client'
import { IMovieCardItem } from "@/app/Search/MovieCard";
import { PropsWithChildren, createContext, useState } from "react";
interface IFavoriteContext {
favorites: Array<IMovieCardItem>
setFavorites: (value: Array<IMovieCardItem>) => void
}
export const FavoriteContext = createContext<IFavoriteContext>({
favorites: [],
// setFavorites: () => {},
setFavorites: (_value: Array<IMovieCardItem>) => { },
});
// Using interleaving to render this context provider client-side, but still utilize server-side renderung for children components
export const FavoriteProvider = ({ children }: PropsWithChildren) => {
const [favorites, setFavorites] = useState<Array<IMovieCardItem>>([])
return (
<FavoriteContext.Provider value={{ favorites, setFavorites }}>{children}</FavoriteContext.Provider>
)
}