Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 77 additions & 6 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const ERROR_INVALID_EVENT_ID = "INVALID_EVENT_ID";
const ERROR_BAD_UUID = "BAD_UUID";
const ERROR_GEOCODING_FAILED = "GEOCODING_FAILED";
const ERROR_BAD_PLACES_API_INTERACTION = "BAD_PLACES_API";
const ERROR_PLACE_DETAILS_FAILED = "PLACE_DETAILS_FAILED";

app.use(express.static("static/absolute"));

Expand Down Expand Up @@ -279,14 +280,45 @@ app.get(
console.error("Places API error. Status: " + status);
response.status(500).json({
status: 500,
error: { type: ERROR_BAD_PLACES_API_INTERACTION },
error: { type: status },
});
} else {
const restaurants = placesApiResponse.results;
const placeDetailsPromises = [];

//Used to get additional information about the restaurant results.
const fields = encodeForURL(
"url,formatted_phone_number,website,review"
);

restaurants.forEach((restaurant) => {
const placeId = encodeForURL(restaurant.place_id);
const newPromise =
"https://maps.googleapis.com/maps/api/place/details/json?place_id=" +
placeId +
"&fields=" +
fields +
"&key=" +
env.API_KEY_PLACES;

placeDetailsPromises.push(
fetch(newPromise).then((response) => response.json())
);
});

await Promise.all(placeDetailsPromises).then((values) => {
for (let i = 0; i < values.length; i++) {
if (values[i].status === "OK") {
restaurants[i].additional_details = values[i].result;
}
}
});

// Normalize undefined or null to an empty array
response.json({
status: 200,
data: {
places: placesApiResponse.results || [],
places: restaurants || [],
attributions: placesApiResponse.html_attributions || [],
center: { latitude, longitude },
},
Expand All @@ -313,6 +345,45 @@ app.get(
}
);

app.get(`${PREFIX_API}/placedetails`, async (request, response) => {
const placeId = encodeForURL(request.query.id);
const fields = encodeForURL(request.query.fields);

const placeDetailsRequest =
"https://maps.googleapis.com/maps/api/place/details/json?place_id=" +
placeId +
"&fields=" +
fields +
"&key=" +
env.API_KEY_PLACES;

try {
const placeDetailsResponse = await (
await fetch(placeDetailsRequest)
).json();
const responseStatus = placeDetailsResponse.status;

if (responseStatus !== "OK") {
console.error(
"Place Details error occured. Api response status: " + responseStatus
);
response
.status(500)
.json({ status: 500, error: { type: responseStatus } });
} else {
response.json({
status: 200,
data: placeDetailsResponse,
});
}
} catch (err) {
console.error(err);
response
.status(500)
.json({ status: 500, error: { type: ERROR_PLACE_DETAILS_FAILED } });
}
});

app.get(
`${PREFIX_API}/:${URL_PARAM_EVENT_ID}/participants`,
getEvent,
Expand All @@ -330,7 +401,7 @@ app.get(
);

app.get(`${PREFIX_API}/geocode`, async (request, response) => {
const address = encodeAddress(request.query.address);
const address = encodeForURL(request.query.address);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between encodeAddress and encodeForURL? Is it just a function you created to format the address?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the name to be a bit broader. It encodes any string to be used in URLs, not just addresses. I use this function in more than one api now, so I made the name fit its purpose better.


const geocodeRequest =
"https://maps.googleapis.com/maps/api/geocode/json?address=" +
Expand Down Expand Up @@ -364,14 +435,14 @@ app.get(`${PREFIX_API}/geocode`, async (request, response) => {
}
});

function encodeAddress(address) {
const formattedAddress = encodeURIComponent(address)
function encodeForURL(string) {
const formattedString = encodeURIComponent(string)
.replace("!", "%21")
.replace("*", "%2A")
.replace("'", "%27")
.replace("(", "%28")
.replace(")", "%29");
return formattedAddress;
return formattedString;
}

// This number should be kept in sync with the port number in nodemon.json
Expand Down
33 changes: 31 additions & 2 deletions static/absolute/css/searchPage.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ body {
margin: 0px;
}

hr {
height: 1px;
background-color: #d4ede0;
border: none;
}

/* margin left small */
.ml-s {
margin-left: 15px;
Expand Down Expand Up @@ -107,12 +113,16 @@ body {
font-size: 1.5em;
}

.restaurant-card {
.info-div {
display: grid;
grid-template-columns: 50% auto;
grid-template-columns: 25% auto auto;
}

.restaurant-card {
border: 1px solid #d4ede0;
border-radius: 0.5rem;
margin-top: 25px;
margin-bottom: 25px;
padding: 25px;
}

Expand All @@ -134,6 +144,25 @@ body {
color: #444;
}

.review-header {
color: #fe5f55;
font-weight: bold;
}

.individual-review {
padding: 25px;
}

.show-more-link {
color: #fe5f55;
margin-bottom: 0px;
}

.show-more-link:hover {
cursor: pointer;
color: rgb(226, 55, 43);
}

.banner-btn {
padding: 12px 25px;
text-decoration: none;
Expand Down
2 changes: 1 addition & 1 deletion static/absolute/js/participants.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async function showParticipants() {
);
participantContainer.innerHTML = "";

const participants = response.data;
const participants = response.data.participants;
participants
.map((p) => ({ ...p, location: `${p.lat},${p.long}` }))
.forEach((participant) => {
Expand Down
Loading