Iamogeee / BusinessConnect

0 stars 0 forks source link

Business details feature #7

Closed Iamogeee closed 3 months ago

Iamogeee commented 3 months ago

Users can view business details such as reviews, overview, photos, business hours, on a different page when a business is clicked.

Loom Message - 5 July 2024 - Watch Video

sinap-fb commented 3 months ago

Very nice work! The code is quite well-organized and easy to follow.

Iamogeee commented 3 months ago
  • instead of fetching e.g. "http://localhost:3000/api/businesses" what if we fetched "/api/businesses"? I think this is necessary in order for your app to work when deployed.

Hi @sinap-fb ! Thank you for your feedback regarding the use of relative paths for API requests. I implemented the suggested changes, but I ran into some issues. Specifically, when fetching data from the /api/businesses endpoint, I received the following error: Error fetching businesses: SyntaxError: Unexpected token '<', "<!doctype "... is not valid JSON

This error indicates that the response is an HTML document, not JSON, suggesting the endpoint might not be correctly configured or accessible. Could you please advise on how to proceed with debugging this issue?

Iamogeee commented 2 months ago
  • For my understanding, what is the import object in const apiKey = import.meta.env.VITE_API_KEY;?

@sinap-fb Regarding your question, the import.meta.env object is used to access environment variables in JavaScript modules. In the line const apiKey = import.meta.env.VITE_API_KEY;, import.meta.env.VITE_API_KEY is accessing the VITE_API_KEY environment variable that has been set up in my project.

Iamogeee commented 2 months ago
  • In BusinessDetails.jsx, we render "Loading..." when there is !business, but I believe this will mean we show "Loading..." indefinitely if the server has an error handling the request and consequently sends a 500 HTTP status code, which causes the promise returned by fetch() to fail and thus never call setBusinessData(). Would love to see if we can find a way to avoid this!

@sinap-fb I have updated the component to include proper error handling by adding a try-catch block to handle fetch errors and update the state with an error message. Then I retained the if (!business) block to display the "Loading..." message while the data is being fetched.

Iamogeee commented 2 months ago
  • This is another advanced topic; I'd be surprised if they will cover this in the course, so I'm mainly sharing this for educational purposes. I think there is a really sneaky race-condition in the SearchBar component. On input change, we issue an asynchronous request and then update state (setResults()) when it's complete. However, we have no guarantee that, if you type two keys quickly (e.g. "ab"), the second request will complete after the first, which means that we could end up clobbering the results for "ab" with the results for "a". Something to keep in the back of your mind.

@sinap-fb Thanks for pointing this out! I can track the latest request and ensure that only the results from the most recent request are used to update the state. One common approach I found is to use an abort controller to cancel previous requests. I have updated the component to address this issue by using an AbortController to cancel previous fetch requests when a new input change occurs.