11import React , { useEffect , useState } from "react" ;
22import RestaurantCard from "./RestaurantCard" ;
33import Shimmer from "./Shimmer" ;
4- import { Link , Outlet } from "react-router-dom" ;
5- import useOnline from "../utils/useOnline" ;
4+ import { Link } from "react-router-dom" ;
65import { useDispatch , useSelector } from "react-redux" ;
76import { addData } from "../utils/restaurantSlice" ;
87import { addItems } from "../utils/carousalSlice" ;
98import Carousal from "./Carousal" ;
10- import { keywords } from "./Categories" ;
9+ import { CATEGORIES_URL } from "./Constants" ;
10+ import { redirect } from "react-router-dom" ;
11+ import { useNavigate } from "react-router-dom" ;
1112
1213
1314const Body = ( ) => {
1415 const dispatch = useDispatch ( ) ;
1516 const restaurants = useSelector ( ( store ) => store . restaurant . allRestaurant ) ;
16- const [ activeElement , setActiveElement ] = useState ( "Relevance" )
17- const [ restrau , setRestrau ] = useState ( [ ] ) ;
18- const [ dTRestaurants , setDTRestaurants ] = useState ( [ ] )
19- const [ costL2H , setCostL2H ] = useState ( [ ] )
20- const [ costH2L , setCostH2L ] = useState ( [ ] )
21-
17+ const [ activeElement , setActiveElement ] = useState ( "" )
18+ const [ sort , setSorts ] = useState ( [ ] ) ;
19+ const [ categoryRestaurants , setcategoryRestaurants ] = useState ( [ ] )
20+ const [ showShimmer , setShowShimmer ] = useState ( false )
21+ const navigate = useNavigate ( ) ;
2222
23- const handleClick = ( value ) => {
24- setActiveElement ( value ) ;
23+ const handleClick = ( value , param ) => {
24+ setShowShimmer ( true )
25+ setActiveElement ( value )
26+ if ( param === "RELEVANCE" ) {
27+ setShowShimmer ( false )
28+ navigate ( "/" )
29+ return ;
30+ }
31+ getByCategoryData ( param )
2532 }
26- // console.log(restaurants[0])
33+
34+
2735 useEffect ( ( ) => {
2836 getRestaurant ( ) ;
29- getByRating ( ) ;
30- getByDeliveryTime ( ) ;
31- getByCostL2H ( )
32- getByCostH2L ( )
3337 } , [ ] ) ;
3438
39+
40+
41+
42+
43+ async function getByCategoryData ( param ) {
44+ const data = await fetch ( CATEGORIES_URL + param ) ;
45+ const response = await data . json ( ) ;
46+ setcategoryRestaurants ( response ?. data ?. cards [ 0 ] ?. data ?. data ?. cards ) ;
47+ setShowShimmer ( false ) ;
48+ console . log ( categoryRestaurants )
49+ }
3550
3651 async function getRestaurant ( ) {
3752 const data = await fetch (
@@ -40,46 +55,11 @@ const Body = () => {
4055 const json = await data . json ( ) ;
4156 dispatch ( addData ( json ?. data ?. cards [ 2 ] ?. data ?. data ?. cards ) )
4257 dispatch ( addItems ( json ?. data ?. cards [ 0 ] ?. data ?. data ?. cards ) )
43-
44- }
45- console . log ( "RESTAURANTS---->" + restaurants )
46- async function getByRating ( ) {
47- const data = await fetch (
48- "https://www.swiggy.com/dapi/restaurants/list/v5?lat=30.7095&lng=76.7744&sortBy=RATING&page_type=DESKTOP_WEB_LISTING"
49- ) ;
50- const json = await data . json ( ) ;
51- setRestrau ( json ?. data ?. cards [ 0 ] ?. data ?. data ?. cards ) ;
52- console . log ( "BY RATING---->" + restrau )
53- }
54- async function getByDeliveryTime ( ) {
55- const data = await fetch (
56- "https://www.swiggy.com/dapi/restaurants/list/v5?lat=30.7095&lng=76.7744&sortBy=DELIVERY_TIME&page_type=DESKTOP_WEB_LISTING"
57- ) ;
58- const response = await data . json ( ) ;
59- setDTRestaurants ( response ?. data ?. cards [ 0 ] ?. data ?. data ?. cards ) ;
60- console . log ( "BY DT---->" + dTRestaurants ) ;
58+ setSorts ( json . data . sorts ) ;
6159 }
62- async function getByCostL2H ( ) {
63- const data = await fetch (
64- "https://www.swiggy.com/dapi/restaurants/list/v5?lat=30.7095&lng=76.7744&sortBy=COST_FOR_TWO&page_type=DESKTOP_WEB_LISTING"
65- ) ;
66- const response = await data . json ( ) ;
67- setCostL2H ( response ?. data ?. cards [ 0 ] ?. data ?. data ?. cards ) ;
68- console . log ( "BY L2H---->" + costL2H ) ;
69- }
70- async function getByCostH2L ( ) {
71- const data = await fetch (
72- "https://www.swiggy.com/dapi/restaurants/list/v5?lat=30.7095&lng=76.7744&sortBy=COST_FOR_TWO_H2L&page_type=DESKTOP_WEB_LISTING"
73- ) ;
74- const response = await data . json ( ) ;
75- setCostH2L ( response ?. data ?. cards [ 0 ] ?. data ?. data ?. cards ) ;
76- }
77-
78-
79- // const online = useOnline();
80- // if (!online) {
81- // return <h1>Offline!! Please check your internet connection</h1>;
82- // }
60+
61+
62+
8363 if ( ! restaurants ) return null ;
8464 return restaurants ?. length === 0 ? (
8565 < Shimmer />
@@ -93,44 +73,20 @@ const Body = () => {
9373 { restaurants [ 0 ] . length } restaurants
9474 </ div >
9575 < div className = 'flex gap-x-9 text-center items-center cursor-pointer' >
96- {
97- keywords . map ( ( value , i ) => (
98- < p onClick = { ( ) => handleClick ( value ) } key = { i } className = { activeElement === value ? 'border-b-[1px] border-b-[#161824] cursor-pointer' :'cursor-pointer hover:text-[#161824]' } > { value } </ p >
99- ) )
100- }
76+ { Object . values ( sort ) . map ( ( category , i ) => (
77+ < p onClick = { ( ) => handleClick ( category . title , category . key ) } key = { category . key } className = { activeElement === category . title ? 'border-b-[1px] border-b-[#161824] cursor-pointer' :'cursor-pointer hover:text-[#161824]' } > { category . title } </ p >
78+ ) ) }
10179 </ div >
10280 </ div >
10381
104- { activeElement === "Rating" ? < div className = "flex flex-wrap pl-10 gap-x-10 text-sm gap-y-8 mb-24" >
105- { restrau . map ( ( card ) => (
106- < Link to = { "/restaurant/" + card . data . id } key = { card . data . id } >
107- < RestaurantCard { ...card . data } />
108- </ Link >
109- ) ) }
110-
111-
112- </ div > : activeElement === "Delivery Time" ? < div className = "flex flex-wrap pl-10 gap-x-10 text-sm gap-y-8 mb-24" >
113- { dTRestaurants . map ( ( card ) => (
114- < Link to = { "/restaurant/" + card . data . id } key = { card . data . id } >
115- < RestaurantCard { ...card . data } />
116- </ Link >
117- ) ) }
118-
119-
120- </ div > : activeElement === "Cost: Low To High" ? < div className = "flex flex-wrap pl-10 gap-x-10 text-sm gap-y-8 mb-24" >
121- { costL2H . map ( ( card ) => (
122- < Link to = { "/restaurant/" + card . data . id } key = { card . data . id } >
123- < RestaurantCard { ...card . data } />
124- </ Link >
125- ) ) }
126- </ div > : activeElement === "Cost: High To Low" ? < div className = "flex flex-wrap pl-10 gap-x-10 text-sm gap-y-8 mb-24" >
127- { costH2L . map ( ( card ) => (
128- < Link to = { "/restaurant/" + card . data . id } key = { card . data . id } >
129- < RestaurantCard { ...card . data } />
130- </ Link >
131- ) ) }
132- </ div > :
133-
82+ { /***** CARDS ****/ }
83+ { activeElement ? < div className = "flex flex-wrap pl-10 gap-x-10 text-sm gap-y-8 mb-24" >
84+ { categoryRestaurants . length === 0 || showShimmer ?< Shimmer /> :categoryRestaurants . map ( ( card ) => (
85+ < Link to = { "/restaurant/" + card . data . id } key = { card . data . id } >
86+ < RestaurantCard { ...card . data } />
87+ </ Link >
88+ ) ) }
89+ </ div > :
13490 < div className = "flex flex-wrap pl-10 gap-x-10 text-sm gap-y-8 mb-24" >
13591 { Object . values ( restaurants [ 0 ] ) . map ( ( card ) => (
13692 < Link to = { "/restaurant/" + card . data . id } key = { card . data . id } >
@@ -142,3 +98,6 @@ const Body = () => {
14298 ) ;
14399 }
144100export default Body ;
101+
102+
103+
0 commit comments