From 2f57426444bd8d0cfb8d6d7779b5e6737c95ddbf Mon Sep 17 00:00:00 2001 From: rishav_dml Date: Mon, 6 Jan 2025 20:10:32 +0530 Subject: [PATCH] Day01 --- src/components/Card.jsx | 15 +++++++++++++++ src/hooks/useFetch.jsx | 12 +++++------- src/pages/HomePage.jsx | 30 +++++++++++++++++++++--------- 3 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 src/components/Card.jsx diff --git a/src/components/Card.jsx b/src/components/Card.jsx new file mode 100644 index 0000000..245a292 --- /dev/null +++ b/src/components/Card.jsx @@ -0,0 +1,15 @@ +import React from "react"; + +const Card = ({movie}) => { + const { Title, Year, imdbID, Poster } = movie + console.log("Card", Title, Year, imdbID, Poster) + return ( +
+ {Title} +
{Title}
+

{Year}

+
+ ); +}; + +export default Card; diff --git a/src/hooks/useFetch.jsx b/src/hooks/useFetch.jsx index 5e5653c..a8f8036 100644 --- a/src/hooks/useFetch.jsx +++ b/src/hooks/useFetch.jsx @@ -1,16 +1,16 @@ import axios from "axios"; import { useState, useEffect } from "react"; -const useFetch = ({ url }) => { +const useFetch = (url) => { const [data, setData] = useState({}); - // const [error, setError] = useState(""); useEffect(() => { + // console.log("Inside fetch") const fetch = async () => { let response; try { - response = await axios.get(url); - console.log("USEFETCH:",response) - setData(response); + response = await axios.get(url); + // console.log("USEFETCH:", response?.data); + setData(response?.data); } catch { console.log("Error in useFetch: "); return "Error Fetching Data"; @@ -21,6 +21,4 @@ const useFetch = ({ url }) => { return data; }; - - export default useFetch; diff --git a/src/pages/HomePage.jsx b/src/pages/HomePage.jsx index b082f4d..1ff1706 100644 --- a/src/pages/HomePage.jsx +++ b/src/pages/HomePage.jsx @@ -1,16 +1,17 @@ import React, { useEffect, useState } from "react"; import useFetch from "../hooks/useFetch"; +import Card from "../components/Card"; + const HomePage = () => { - const [inputValue, setInputValue] = useState("") - const [searchValue, setSearchValue] = useState("") + const [inputValue, setInputValue] = useState(""); + const [searchValue, setSearchValue] = useState(""); + const [data, setData] = useState(""); const url = import.meta.env.VITE_BASE_URL; - console.log("URL =", `${url}s=${searchValue}`) - const data = useFetch(`${url}s=${searchValue}`); - - // console.log("DATA = ", data) - + const response = useFetch(url + "s=" + searchValue); + console.log(response) + useEffect(() => setData(response), [data]); const handleSearch = (event) => { event.preventDefault(); setSearchValue(inputValue); @@ -24,14 +25,25 @@ const HomePage = () => { name="search" id="search" value={inputValue} - onChange={(event) => setInputValue(event?.target?.value)} + onChange={(event) => + setInputValue(event?.target?.value) + } className="p-1 text-quadnary bg-primary rounded outline-none border border-ternary" /> - + {data?.Search?.length > 0 && +
+ {data?.Search?.map((movie) => )} +
+ } ); };