-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Community events to map #7522
Community events to map #7522
Changes from all commits
f5fd07b
9e7d6c1
d4d12a7
a383ccb
664dd9c
d7440cb
c676b9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import L from "leaflet"; | ||
import { Marker, Popup } from "react-leaflet"; | ||
import { ReactMarkdown } from "react-markdown/lib/react-markdown"; | ||
import Link from "@components/Link"; | ||
|
||
export default function EventMarker({event}) { | ||
// Custom component for rendering links within ReactMarkdown | ||
const LinkRenderer = ({ href, children }) => ( | ||
<Link href={href}> | ||
{children} | ||
</Link> | ||
); | ||
|
||
return ( | ||
<Marker | ||
icon={L.divIcon({ | ||
className: "rounded-full", | ||
html: `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6"> | ||
<path stroke-linecap="round" stroke-linejoin="round" d="M15 10.5a3 3 0 11-6 0 3 3 0 016 0z" /> | ||
<path stroke-linecap="round" stroke-linejoin="round" d="M19.5 10.5c0 7.142-7.5 11.25-7.5 11.25S4.5 17.642 4.5 10.5a7.5 7.5 0 1115 0z" /> | ||
</svg> | ||
`, | ||
popupAnchor: [0, -10], | ||
iconSize: [40, 40], | ||
iconAnchor: [20, 20], | ||
})} | ||
position={[event.geometry.coordinates[1], event.geometry.coordinates[0]]} | ||
> | ||
<Popup> | ||
<div className="flex flex-col gap-[5px]"> | ||
<h1 className="font-[600]"> | ||
<Link href={event.properties.url}> | ||
{event.properties.name} | ||
</Link> | ||
</h1> | ||
<span> | ||
{[ | ||
event.properties.location.city, | ||
event.properties.location.state, | ||
event.properties.location.country, | ||
] | ||
.filter((x) => x) | ||
.join(", ")} | ||
</span> | ||
<span> | ||
<ReactMarkdown components={{ a: LinkRenderer }}> | ||
{event.properties.description} | ||
</ReactMarkdown> | ||
</span> | ||
<span> | ||
{`${new Date(event.properties.date.start).toLocaleDateString()} - | ||
${new Date(event.properties.date.end).toLocaleDateString()}`} | ||
</span> | ||
</div> | ||
</Popup> | ||
</Marker> | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -234,28 +234,68 @@ export default async function handler(req, res) { | |
} | ||
|
||
// - events | ||
try { | ||
if (profile.events) { | ||
async function getCoordinates(city, state, country) { | ||
let locationDb = {}; | ||
const provided = [city, state, country].filter((x) => x).join(","); | ||
if (locationDb[provided]) { | ||
return locationDb[provided]; | ||
} | ||
try { | ||
const location = await fetch( | ||
eddiejaoude marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`https://nominatim.openstreetmap.org/?addressdetails=1&q= | ||
${encodeURIComponent(provided)}&format=json&limit=1` | ||
); | ||
const coordinates = await location.json(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For events that are added by forms, how do they get their location? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are fetching coordinates with city, state and country details. As soon as we hit the api to load data into MongoDB, it takes the events with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok thanks 👍 Forms will write directly to the db so will skip the |
||
if (coordinates) { | ||
const point = { | ||
lat: coordinates[0].lat, | ||
lon: coordinates[0].lon, | ||
}; | ||
locationDb[provided] = point; | ||
return point; | ||
} | ||
} catch (e) { | ||
return null; | ||
} | ||
return null; | ||
} | ||
|
||
if (profile.events) { | ||
try { | ||
const events = await Promise.all( | ||
profile.events.map(async (event, position) => { | ||
let location = {}; | ||
if (event.location) { | ||
location = { | ||
location: { ...event.location }, | ||
}; | ||
if (new Date(event.date.start) > Date.now() || new Date(event.date.end) > Date.now()) { | ||
const coordinates = await getCoordinates( | ||
event.location.city, | ||
event.location.state, | ||
event.location.country | ||
); | ||
if (coordinates) { | ||
location.location.lat = coordinates.lat; | ||
location.location.lon = coordinates.lon; | ||
} | ||
} | ||
} | ||
return { | ||
order: position, | ||
...event, | ||
...location, | ||
}; | ||
}) | ||
); | ||
|
||
await Profile.findOneAndUpdate( | ||
dhruvilmehta marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ username: profile.username }, | ||
{ | ||
events: profile.events.map((event) => ({ | ||
isVirtual: event.isVirtual, | ||
color: event.color, | ||
name: event.name, | ||
description: event.description, | ||
date: { | ||
start: event.date.start, | ||
end: event.date.end, | ||
}, | ||
url: event.url, | ||
price: event.price, | ||
})), | ||
} | ||
{ events } | ||
); | ||
} catch (e) { | ||
logger.error(e,`failed to update events for ${profile.username}`); | ||
} | ||
} catch (e) { | ||
logger.error(e, `failed to update events for ${profile.username}`); | ||
} | ||
}) | ||
); | ||
|
Uh oh!
There was an error while loading. Please reload this page.