Skip to content

Commit

Permalink
fix(web): client side map fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed Dec 8, 2024
1 parent b520402 commit e7fcbfb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 74 deletions.
58 changes: 0 additions & 58 deletions apps/web/src/app/map/client-page.tsx

This file was deleted.

88 changes: 72 additions & 16 deletions apps/web/src/app/map/page.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,83 @@
"use server";
"use client";

import { MapClientPage } from "./client-page";
import { useEffect, useState } from "react";
import MapGL, { Source, Layer } from "react-map-gl";
import { pragueCoorinates } from "@/constants/coordinates";
import { pointsLayer } from "@/app/map/map-layers/point-layer";
import { heatmapLayer } from "@/app/map/map-layers/heatmap-layer";
import { PidLogo } from "@/components/PidLogo/PidLogo";

type Datapoint = {
latitude: number;
longitude: number;
name: string;
};

export default async function MapPage() {
const res = await fetch("https://api.metronow.dev/v1/platform/all");
const data: Datapoint[] = await res.json();

const geojson = {
type: "FeatureCollection",
features: data.map((stop) => ({
type: "Feature",
geometry: {
type: "Point",
coordinates: [stop.longitude, stop.latitude],
},
})),
export default function MapPage() {
const [geojson, setGeojson] = useState<any>(null);
const [allowZoom, setAllowZoom] = useState(false);

const udpateGeojson = async () => {
const res = await fetch("https://api.metronow.dev/v1/platform/all");
const data: Datapoint[] = await res.json();

setGeojson({
type: "FeatureCollection",
features: data.map((stop) => ({
type: "Feature",
geometry: {
type: "Point",
coordinates: [stop.longitude, stop.latitude],
},
})),
});
};

return <MapClientPage stopsGeojson={geojson} />;
useEffect(() => {
udpateGeojson();
}, []);

return (
<div
className="flex mt-24 items-center flex-col w-full gap-4 justify-center bg-white dark:bg-black"
onMouseLeave={() => setAllowZoom(false)}
onClick={() => setAllowZoom(true)}
>
<div className="max-w-screen-lg w-full flex items-center justify-around dark:text-neutral-300 text-neutral-800">
<span className="flex flex-col gap-2 items-center text-lg">
<PidLogo className="h-18" />
Prague integrated transport
</span>
</div>

<div className="h-[80vh] w-full relative">
<MapGL
initialViewState={{
...pragueCoorinates,
zoom: 6,
}}
dragRotate={false}
maxZoom={18}
minZoom={5}
style={{
position: "relative",
display: "block",
}}
boxZoom={false}
keyboard={false}
scrollZoom={allowZoom}
mapStyle="mapbox://styles/mapbox/dark-v9"
mapboxAccessToken={process.env.NEXT_PUBLIC_MAPBOX_TOKEN}
attributionControl={false}
>
{geojson && (
<Source type="geojson" data={geojson as any}>
<Layer {...heatmapLayer} />
<Layer {...pointsLayer} />
</Source>
)}
</MapGL>
</div>
</div>
);
}

1 comment on commit e7fcbfb

@vercel
Copy link

@vercel vercel bot commented on e7fcbfb Dec 8, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.