-
Notifications
You must be signed in to change notification settings - Fork 8
/
Home.js
75 lines (70 loc) · 2.44 KB
/
Home.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import React, { useState, useEffect } from "react"
import Shimmer from "./Shimmer"
import { SWIGGY_PUBLIC_API } from "../Config"
import RestaurantCard from "./RestaurantCard"
import { filterRestaurant } from "../utils/helper"
import useOnline from "../utils/useOnline"
const Home = () => {
// console.log("---Home---")
// -----useState -------
const [searchText, setSearchText] = useState("")
const [allRestaurant, setAllRestaurant] = useState([])
const [filteredRestaurant, setFilteredRestaurant] = useState([])
const [error, setError] = useState("")
// -----useEffect -------
useEffect(() => {
// console.log("I am useEffect (inside <HOME/>)")
getRestaurantData()
}, [])
async function getRestaurantData() {
const data = await fetch(SWIGGY_PUBLIC_API)
const json = await data.json()
const restaurant = json?.data?.cards[2]?.data?.data?.cards
// console.log(restaurant)
setAllRestaurant(restaurant)
setFilteredRestaurant(restaurant)
}
const isOnline = useOnline()
if (!isOnline) {
return <h3 className="flex-container warning">**You are Offline. Check Your internet connection</h3>
}
return (
<section className="section">
<main className="home flex-container">
{/* ***** Search Restaurants***** */}
<div className="search">
<div className="search__box">
<input className="search__input" value={searchText} onChange={e => setSearchText(e.target.value)} placeholder="Search Restaurant ..." />
<button
className="search__btn"
onClick={e => {
const data = filterRestaurant(searchText, allRestaurant)
if (data.length === 0) {
setError("No Match Found")
} else {
setError("")
}
setFilteredRestaurant(data)
}}
>
Search
</button>
</div>
{/* **** Display Search Result ***** */}
<div className="search__result warning">{error}</div>
</div>
{/* ***** Display Restaurant ***** */}
<div className="restaurant">
{!allRestaurant?.length ? (
<Shimmer />
) : (
filteredRestaurant?.map(restaurant => {
return <RestaurantCard {...restaurant.data} key={restaurant.data.id} />
})
)}
</div>
</main>
</section>
)
}
export default Home