wip - got context working with favorites page using interleaving
This commit is contained in:
parent
9acb10e643
commit
acca5c387f
|
@ -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>
|
||||
);
|
||||
};
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import FavoriteList from "./favoriteList";
|
||||
|
||||
export default () => {
|
||||
// TODO: add context provider
|
||||
return (
|
||||
<div>
|
||||
<h1>Your Favorites!</h1>
|
||||
<FavoriteList/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
|
|
@ -41,13 +41,15 @@ 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>
|
||||
<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"
|
||||
>
|
||||
Movie Details
|
||||
</Link>
|
||||
<FavoriteBtn movie={movie} />
|
||||
<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"
|
||||
>
|
||||
Movie Details
|
||||
</Link>
|
||||
<FavoriteBtn movie={movie} />
|
||||
</div>
|
||||
</ImgCard>
|
||||
);
|
||||
};
|
||||
|
|
|
@ -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 || [];
|
||||
|
||||
|
|
|
@ -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>
|
||||
);
|
||||
|
|
|
@ -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={() => {
|
||||
setFavorites([...favorites, movie]);
|
||||
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>
|
||||
|
|
|
@ -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>
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue
Block a user