Day01
This commit is contained in:
parent
c3bdd1d0ff
commit
2f57426444
15
src/components/Card.jsx
Normal file
15
src/components/Card.jsx
Normal file
|
@ -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 (
|
||||||
|
<div className="h-16 w-10">
|
||||||
|
<img src={Poster} alt={Title} className="h-12 w-full" />
|
||||||
|
<h6 className="text-quadnary">{Title}</h6>
|
||||||
|
<p className="text-quadnary">{Year}</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Card;
|
|
@ -1,16 +1,16 @@
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
|
|
||||||
const useFetch = ({ url }) => {
|
const useFetch = (url) => {
|
||||||
const [data, setData] = useState({});
|
const [data, setData] = useState({});
|
||||||
// const [error, setError] = useState("");
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
// console.log("Inside fetch")
|
||||||
const fetch = async () => {
|
const fetch = async () => {
|
||||||
let response;
|
let response;
|
||||||
try {
|
try {
|
||||||
response = await axios.get(url);
|
response = await axios.get(url);
|
||||||
console.log("USEFETCH:",response)
|
// console.log("USEFETCH:", response?.data);
|
||||||
setData(response);
|
setData(response?.data);
|
||||||
} catch {
|
} catch {
|
||||||
console.log("Error in useFetch: ");
|
console.log("Error in useFetch: ");
|
||||||
return "Error Fetching Data";
|
return "Error Fetching Data";
|
||||||
|
@ -21,6 +21,4 @@ const useFetch = ({ url }) => {
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default useFetch;
|
export default useFetch;
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import useFetch from "../hooks/useFetch";
|
import useFetch from "../hooks/useFetch";
|
||||||
|
|
||||||
|
import Card from "../components/Card";
|
||||||
|
|
||||||
const HomePage = () => {
|
const HomePage = () => {
|
||||||
const [inputValue, setInputValue] = useState("")
|
const [inputValue, setInputValue] = useState("");
|
||||||
const [searchValue, setSearchValue] = useState("")
|
const [searchValue, setSearchValue] = useState("");
|
||||||
|
const [data, setData] = useState("");
|
||||||
|
|
||||||
const url = import.meta.env.VITE_BASE_URL;
|
const url = import.meta.env.VITE_BASE_URL;
|
||||||
console.log("URL =", `${url}s=${searchValue}`)
|
const response = useFetch(url + "s=" + searchValue);
|
||||||
const data = useFetch(`${url}s=${searchValue}`);
|
console.log(response)
|
||||||
|
useEffect(() => setData(response), [data]);
|
||||||
// console.log("DATA = ", data)
|
|
||||||
|
|
||||||
const handleSearch = (event) => {
|
const handleSearch = (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
setSearchValue(inputValue);
|
setSearchValue(inputValue);
|
||||||
|
@ -24,14 +25,25 @@ const HomePage = () => {
|
||||||
name="search"
|
name="search"
|
||||||
id="search"
|
id="search"
|
||||||
value={inputValue}
|
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"
|
className="p-1 text-quadnary bg-primary rounded outline-none border border-ternary"
|
||||||
/>
|
/>
|
||||||
<button type="button" className="text-quadnary" onClick={handleSearch}>
|
<button
|
||||||
|
type="button"
|
||||||
|
className="text-quadnary"
|
||||||
|
onClick={handleSearch}
|
||||||
|
>
|
||||||
Search
|
Search
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
{data?.Search?.length > 0 &&
|
||||||
|
<div>
|
||||||
|
{data?.Search?.map((movie) => <Card key={movie?.imdbID} movie={movie}/>)}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue