So, the request is returning the JSON file. But when in console it is saying 'Undefined' and I do not know why.
So the button when clicked will send the results from my request from the google Place API; which contains the place_id needed to make the call to the Place Details API to the Info component.
const OnButtonClick = (restaurant) => {
setRestaurant(restaurant)
setOpenPopup(true)
}
<button className="cardButton" onClick={() => OnButtonClick(restaurantData)}>
View Information
</button>
<InfoPopup open={openPopup} restaurant={restaurant} onClose={() => setOpenPopup(false)} />
So, this works the way I think it does (Sorry, I am new to React)
Here's the InfoPopup component
function InfoPopup({ open, onClose, restaurant }) {
const [restaurant1, setRestaurant1] = useState([])
let id = restaurant.place_id
let URL = `/maps/api/place/details/json?place_id=${id}&key=${process.env.REACT_APP_API_KEY}`
const fetchRestaurants1 = async () => {
const res1 = await axios.get(URL)
setRestaurant1(res1.data.results);
}
useEffect(() => {
fetchRestaurants1()
console.log(restaurant1) //This is getting 'Undefined'
}, [id]);
const navigate = useNavigate()
if (!open) {return null}
return ReactDOM.createPortal(
<>
<div>
{restaurant1?.map(restaurant => (
<div key={restaurant.place_id}> {restaurant.formatted_phone_number} </div>
))}
</div>
<div className="popup">
<div className="popup-inner">
<button className="close-btn" onClick={onClose}> Close </button>
<h1 className="title"> {restaurant.name} </h1>
<ul>
{/* <li className="service">
Status: {}
</li> */}
<li className="location">
Address: {restaurant.vicinity}
Phone Number:
</li>
<li className="cost">
Cost: {restaurant.price_level}
</li>
{/* <li className="food">
Food Type:
</li> */}
</ul>
<div className="links">
<Link className="writeButton" to="/write" state={{data: restaurant}}>
Write a review
</Link>
{/* <button className="writeButton" onClick={() => navigate("/write", {data:restaurant})}>
Write a review
</button> */}
<Link className="readButton" to="/read" state={{data: restaurant}}>
Read the reviews
</Link>
{/* <button className="readButton" onClick={() => navigate("/read")}>
Read the reviews
</button> */}
</div>
</div>
</div>
</>,
document.getElementById('portal')
)
}
I think the problem is on the first render, there's no ID being passed. But I do not know how to work around it. Any help would be appreciated.