|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { data } from "../data/data.js"; |
| 3 | + |
| 4 | +const Food = () => { |
| 5 | + |
| 6 | + //State Hook |
| 7 | + const [foods, setFoods] = useState(data); |
| 8 | + |
| 9 | + //Filter by Type burgers/pizza/etc |
| 10 | + const filterType = (type) => { |
| 11 | + setFoods( |
| 12 | + data.filter((item) => { |
| 13 | + return item.category === type; |
| 14 | + }) |
| 15 | + ); |
| 16 | + }; |
| 17 | + |
| 18 | + //Filter by Price |
| 19 | + const filterPrice = (price) => { |
| 20 | + setFoods( |
| 21 | + data.filter((item) => { |
| 22 | + return item.price === price; |
| 23 | + }) |
| 24 | + ); |
| 25 | + }; |
| 26 | + |
| 27 | + |
| 28 | + return ( |
| 29 | + <div className="max-w-[1640px] m-auto px-4 py-12 "> |
| 30 | + <h1 className="text-black-600 font-bold text-4xl text-center"> |
| 31 | + Top Rated Menu Items{" "} |
| 32 | + </h1> |
| 33 | + |
| 34 | + {/* Filter Row */} |
| 35 | + <div className="flex flex-col lg:flex-row justify-between"> |
| 36 | + |
| 37 | + {/* Filter Type */} |
| 38 | + <div> |
| 39 | + <p className="font-bold text-gray-700 pl-2 pt-4">Filter Type</p> |
| 40 | + {/* This div take care of buttons and all functions */} |
| 41 | + <div className="flex justify-between flex-wrap w-full "> |
| 42 | + {/* when click button(All) then setFoods function run and show all data */} |
| 43 | + <button |
| 44 | + onClick={() => setFoods(data)} |
| 45 | + className="m-1 border-green-500 text-green-500 focus:bg-yellow-500 focus:text-white " |
| 46 | + > |
| 47 | + All |
| 48 | + </button> |
| 49 | + {/* when click rest of 4 buttons then filterType function run and filter the data */} |
| 50 | + <button |
| 51 | + onClick={() => filterType("burger")} |
| 52 | + className="m-1 border-green-500 text-green-500 focus:bg-yellow-500 focus:text-white" |
| 53 | + > |
| 54 | + Burger |
| 55 | + </button> |
| 56 | + <button |
| 57 | + onClick={() => filterType("pizza")} |
| 58 | + className="m-1 border-green-500 text-green-500 focus:bg-yellow-500 focus:text-white " |
| 59 | + > |
| 60 | + Pizza |
| 61 | + </button> |
| 62 | + <button |
| 63 | + onClick={() => filterType("salad")} |
| 64 | + className="m-1 border-green-500 text-green-500 focus:bg-yellow-500 focus:text-white " |
| 65 | + > |
| 66 | + Salad |
| 67 | + </button> |
| 68 | + <button |
| 69 | + onClick={() => filterType("chicken")} |
| 70 | + className="m-1 border-green-500 text-green-500 focus:bg-yellow-500 focus:text-white " |
| 71 | + > |
| 72 | + Chicken |
| 73 | + </button> |
| 74 | + <button |
| 75 | + onClick={() => filterType("dessert")} |
| 76 | + className="m-1 border-green-500 text-green-500 focus:bg-yellow-500 focus:text-white " |
| 77 | + > |
| 78 | + Dessert |
| 79 | + </button> |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + |
| 83 | + {/* Filter Price */} |
| 84 | + <div> |
| 85 | + <p className="font-bold text-gray-700 flex-wrap pl-2 pt-4">Filter Price</p> |
| 86 | + <div className="flex justify-between flex-wrap w-full"> |
| 87 | + <button |
| 88 | + onClick={() => filterPrice("Rs 199")} |
| 89 | + className="m-1 border-green-500 text-green-500 focus:bg-yellow-500 focus:text-white" |
| 90 | + > |
| 91 | + Rs 199 |
| 92 | + </button> |
| 93 | + <button |
| 94 | + onClick={() => filterPrice("Rs 299")} |
| 95 | + className="m-1 border-green-500 text-green-500 focus:bg-yellow-500 focus:text-white" |
| 96 | + > |
| 97 | + Rs 299 |
| 98 | + </button> |
| 99 | + <button |
| 100 | + onClick={() => filterPrice("Rs 399")} |
| 101 | + className="m-1 border-green-500 text-green-500 focus:bg-yellow-500 focus:text-white" |
| 102 | + > |
| 103 | + Rs 399 |
| 104 | + </button> |
| 105 | + <button |
| 106 | + onClick={() => filterPrice("Rs 499")} |
| 107 | + className="m-1 border-green-500 text-green-500 focus:bg-yellow-500 focus:text-white" |
| 108 | + > |
| 109 | + Rs 499 |
| 110 | + </button> |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + |
| 115 | + {/* Display food images, All images control by this div */} |
| 116 | + <div className="grid grid-cols-2 lg:grid-cols-4 gap-6 pt-8"> |
| 117 | + {foods.map((item, index) => ( |
| 118 | + |
| 119 | + <div |
| 120 | + // hover:scale-105 and duration-300 tells when we hover on image then images react after 0.3s or 300milliseconds duration. |
| 121 | + key={index} |
| 122 | + className="border shadow-lg cursor-pointer rounded-lg hover:scale-105 duration-300" |
| 123 | + > |
| 124 | + <img |
| 125 | + src={item.image} |
| 126 | + alt={item.name} |
| 127 | + className="w-full h-[300px] object-cover rounded-t-lg" |
| 128 | + /> |
| 129 | + {/* Inside This div name and price are take */} |
| 130 | + <div className="flex justify-between px-2 py-4"> |
| 131 | + <p className="font-bold">{item.name}</p> |
| 132 | + <p> |
| 133 | + <span className="bg-orange-500 text-white p-1 rounded-full flex text-center"> |
| 134 | + {item.price} |
| 135 | + </span> |
| 136 | + </p> |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + ))} |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + ); |
| 143 | +}; |
| 144 | + |
| 145 | +export default Food; |
0 commit comments