Skip to content

Commit

Permalink
Feature/core 4887 fux map (#343)
Browse files Browse the repository at this point in the history
* CORE-4887 docs

* CORE-4887 All points set

* CORE-4887 Clean

* CORE-4887 Add icon style

* CORE-4887 Icon size

* Fix loading group

* Fix loading group

* Fix loading group
  • Loading branch information
valeriibudko authored Jun 14, 2024
1 parent cf69577 commit 87fd986
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 90 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ npm run build # to build all files for next server (stored in .next folder)
npm run start # to start simple development server
```

Fix broken dependencies:
`npm install --legacy-peer-deps`

Server starts listen on `0.0.0.0:3000`

[github-token]: https://github.com/settings/tokens
Expand Down
5 changes: 5 additions & 0 deletions main/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ body,
.leaflet-popup-tip {
margin-top: -10px;
}

.leaflet-container .leaflet-bar a:first-child,
.leaflet-container .leaflet-bar a:last-child {
text-decoration: none;
}
109 changes: 52 additions & 57 deletions modules/map/map.jsx
Original file line number Diff line number Diff line change
@@ -1,86 +1,81 @@
import React, { useRef, useEffect } from 'react'
import React, { useRef, useEffect, useState } from 'react'
import L from 'leaflet'
import { MarkerClusterGroup } from 'leaflet.markercluster'

import styles from './styles.module.css'

import 'leaflet.markercluster'
import { getAssetsPath } from 'utils/helpers'

// somewhere in the middle of North Atlantic ocean
const KEY_MOVE = 'moveend'

const centerPosition = new L.LatLng(26.523257520856546, -43.10211013159716)

const markerIcon = L.icon({
iconUrl: getAssetsPath('/static/images/map/marker.svg'),
iconSize: [32, 32],
iconSize: [18, 18],
iconAnchor: [16, 32],
popupAnchor: [0, -32],
})

const CustomMap = ({ locations }) => {
const mapContainerRef = useRef(null)
const map = useRef(null)
const [visibleMarkers, setVisibleMarkers] = useState([])
// const [mapLoaded, setMapLoaded] = useState(false)

const mapRef = useRef(null)

const loadVisibleMarkers = () => {
const bounds = mapRef.current.getBounds()
const inViewMarkers = locations.filter((loc) =>
bounds.contains(new L.LatLng(loc.latitude, loc.longitude))
)
setVisibleMarkers(inViewMarkers)
}

useEffect(() => {
const coverLayer = L.tileLayer(
'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
{
attribution: `
<a href="https://www.openstreetmap.org">OpenStreetMap</a> under
<a href="https://creativecommons.org/licenses/by-sa/2.0">CC-BY-SA</a>
`,
// Initialize the map
if (locations.length > 0 && !mapRef.current) {
mapRef.current = L.map('map', {
center: centerPosition,
zoom: 2,
minZoom: 1,
maxZoom: 12,
}
)
layers: [
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution:
'&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}),
],
})
// Handle map movements to load new markers
mapRef.current.on(KEY_MOVE, loadVisibleMarkers)
}

map.current = L.map(mapContainerRef.current, {
center:
locations[0]?.latitude && locations[0]?.longitude
? new L.LatLng(locations[0]?.latitude, locations[0]?.longitude)
: centerPosition,
zoom: 1,
maxBounds: [
[70, 180],
[-70, -180],
],
layers: [coverLayer],
scrollWheelZoom: false,
})
return () => map.current.remove()
// Initialize marker cluster group
const markerClusterGroup = new L.MarkerClusterGroup()
mapRef.current.addLayer(markerClusterGroup)

return () => {
// Cleanup the map and markers
mapRef.current.off(KEY_MOVE, loadVisibleMarkers)
mapRef.current.remove()
mapRef.current = null
}
}, [])

useEffect(() => {
const markers = new MarkerClusterGroup({
chunkedLoading: true,
icon: markerIcon,
})

locations.forEach(({ name, href, latitude, longitude }) => {
const marker = L.marker(new L.LatLng(latitude, longitude), {
title: name,
const markerClusterGroup = new L.MarkerClusterGroup()
// Create and add markers
visibleMarkers.forEach(({ name, href, latitude, longitude }) => {
const marker = L.marker([latitude, longitude], {
icon: markerIcon,
})
if (href) {
marker.bindPopup(
`<a
href=${href}
target="_blank"
rel="noopener noreferrer"
>
${name}
</a>`
)
} else marker.bindPopup(name)

markers.addLayer(marker)
}).bindPopup(href ? `<a href=${href} target="_blank">${name}</a>` : name)
markerClusterGroup.addLayer(marker)
})

map.current.addLayer(markers)
mapRef.current.addLayer(markerClusterGroup)

return () => map.current.removeLayer(markers)
}, [locations])
return () => markerClusterGroup.clearLayers()
}, [visibleMarkers])

return <div ref={mapContainerRef} className={styles.map} />
return <div id="map" style={{ height: '26vh', width: '100%' }} />
}

export default CustomMap
7 changes: 1 addition & 6 deletions pages/data-providers/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ export async function getServerSideProps({ query }) {
props: {
initialState: {
dataProviders: {
data:
// TODO: Remove once https://github.com/vercel/next.js/issues/16122 is solved
// or once we migrate to backend search
normalizeDataProviders(
process.env.NODE_ENV === 'production' ? data : data.slice(0, 200)
),
data: normalizeDataProviders(data),
params: {
...Object.fromEntries(
Object.entries(query).filter(([, v]) => v != null)
Expand Down
54 changes: 27 additions & 27 deletions templates/data-providers/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@ import AddDataProviderForm from './form'

import { formatNumber } from 'utils/format-number'
import Search from 'modules/search-layout'
import Map from 'modules/map'

// REMOVE TEMP until data is correct

// const filterAndMapDataProviders = (dataProviders) =>
// dataProviders
// .filter(
// ({ name, dataProviderLocation }) =>
// dataProviderLocation?.latitude != null &&
// dataProviderLocation?.longitude != null &&
// name
// )
// .map(({ id, name, dataProviderLocation }) => ({
// name,
// href: `/data-providers/${id}`,
// latitude: dataProviderLocation?.latitude,
// longitude: dataProviderLocation?.longitude,
// }))
const filterAndMapDataProviders = (dataProviders) =>
dataProviders
.filter(
({ name, dataProviderLocation }) =>
dataProviderLocation?.latitude != null &&
dataProviderLocation?.longitude != null &&
name
)
.map(({ id, name, dataProviderLocation }) => ({
name,
href: `/data-providers/${id}`,
latitude: dataProviderLocation?.latitude,
longitude: dataProviderLocation?.longitude,
}))

const getCountryName = (code) => {
const countryName = countries[String(code).toUpperCase()]
Expand Down Expand Up @@ -151,7 +150,11 @@ const DataProvidersSearchTemplate = React.memo(
results={results}
setDataProvidersOffset={setDataProvidersSize}
>
<div id="add-new-data-provider" className={styles.addDataProvider}>
<div
id="add-new-data-provider"
key="add-new-data-provider"
className={styles.addDataProvider}
>
{showAddDataProviderForm ? (
<AddDataProviderForm
ref={formRef}
Expand All @@ -175,16 +178,13 @@ const DataProvidersSearchTemplate = React.memo(
</div>
</SearchResults>
<Search.Sidebar>
{/* // REMOVE TEMP until data is correct */}

{/* {results.length > 0 && ( */}
{/* <Map */}
{/* className={styles.map} */}
{/* // We have too long load map with full list data-providers */}
{/* eslint-disable-next-line max-len */}
{/* locations={filterAndMapDataProviders(results.slice(0, 200))} */}
{/* /> */}
{/* )} */}
{results.length > 0 && (
<Map
className={styles.map}
locations={filterAndMapDataProviders(results)}
// locations={filterAndMapDataProviders(results.slice(0, 500))}
/>
)}
<p>
We aggregate research papers from data providers all over the world
including institutional and subject repositories and journal
Expand Down

0 comments on commit 87fd986

Please sign in to comment.