Skip to content

Commit 969c59e

Browse files
authored
Merge pull request #59 from plezanje-net/edit-crag
New crag, Edit crag basics
2 parents 72c07ec + 3b00a84 commit 969c59e

File tree

4 files changed

+65
-50
lines changed

4 files changed

+65
-50
lines changed

src/app/sandbox/map/map-event-demo.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import MapMarker from "@/components/map/map-marker";
77
function MapEventDemo() {
88
const [position, setPosition] = useState<[number, number] | undefined>();
99
const map = useMapEvent("click", (e) => {
10-
console.log(e.latlng);
1110
setPosition([e.latlng.lat, e.latlng.lng]);
1211
});
1312

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"use client";
2+
3+
import { Marker, Popup } from "react-leaflet";
4+
import L from "leaflet";
5+
import { ReactNode } from "react";
6+
import { useClientRenderToString } from "@/hooks/useClientRenderToString";
7+
import IconMarkerWall from "../ui/icons/marker-wall";
8+
import IconMarkerParking from "../ui/icons/marker-parking";
9+
import { IconSize } from "../ui/icons/icon-size";
10+
11+
type TLazyMapMarkerProps = {
12+
type: "parking" | "wall";
13+
position: [number, number];
14+
popupContent?: ReactNode;
15+
interactive?: boolean;
16+
hidden?: boolean;
17+
};
18+
19+
function LazyMapMarker({
20+
type,
21+
position,
22+
popupContent,
23+
interactive = true,
24+
hidden,
25+
}: TLazyMapMarkerProps) {
26+
const [icon] = useClientRenderToString(
27+
type === "parking" ? (
28+
<IconMarkerParking size={IconSize.xl} />
29+
) : (
30+
<IconMarkerWall size={IconSize.xl} />
31+
)
32+
);
33+
34+
return (
35+
<Marker
36+
icon={L.divIcon({
37+
className: "",
38+
html: icon,
39+
iconSize: [52, 52],
40+
iconAnchor: [26, 52],
41+
popupAnchor: [0, -46],
42+
})}
43+
position={position}
44+
interactive={interactive}
45+
opacity={hidden ? 0 : 1}
46+
>
47+
{popupContent && <Popup>{popupContent}</Popup>}
48+
</Marker>
49+
);
50+
}
51+
52+
export default LazyMapMarker;
53+
export type { TLazyMapMarkerProps };

src/components/map/map-marker.tsx

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,12 @@
1-
"use client";
1+
import dynamic from "next/dynamic";
2+
import { TLazyMapMarkerProps as TMapMarkerProps } from "./lazy-map-marker";
23

3-
import { Marker, Popup } from "react-leaflet";
4-
import L from "leaflet";
5-
import { ReactNode } from "react";
6-
import { useClientRenderToString } from "@/hooks/useClientRenderToString";
7-
import IconMarkerWall from "../ui/icons/marker-wall";
8-
import IconMarkerParking from "../ui/icons/marker-parking";
9-
import { IconSize } from "../ui/icons/icon-size";
4+
const LazyMapMarker = dynamic(() => import("./lazy-map-marker"), {
5+
ssr: false,
6+
});
107

11-
type TMapMarkerProps = {
12-
type: "parking" | "wall";
13-
position: [number, number];
14-
popupContent?: ReactNode;
15-
interactive?: boolean;
16-
};
17-
18-
function MapMarker({
19-
type,
20-
position,
21-
popupContent,
22-
interactive = true,
23-
}: TMapMarkerProps) {
24-
const [icon] = useClientRenderToString(
25-
type === "parking" ? (
26-
<IconMarkerParking size={IconSize.xl} />
27-
) : (
28-
<IconMarkerWall size={IconSize.xl} />
29-
)
30-
);
31-
32-
return (
33-
<Marker
34-
icon={L.divIcon({
35-
className: "",
36-
html: icon,
37-
iconSize: [52, 52],
38-
iconAnchor: [26, 52],
39-
popupAnchor: [0, -46],
40-
})}
41-
position={position}
42-
interactive={interactive}
43-
>
44-
{popupContent && <Popup>{popupContent}</Popup>}
45-
</Marker>
46-
);
8+
function MapMarker(props: TMapMarkerProps) {
9+
return <LazyMapMarker {...props} />;
4710
}
4811

4912
export default MapMarker;

src/components/ui/coordinates-input.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import Button from "./button";
99
import Dialog, { DialogSize, DialogTitleSize } from "./dialog";
1010
import TextField, { TTextFieldProps } from "./text-field";
1111
import { Dispatch, SetStateAction, useState } from "react";
12+
import IconMarker from "./icons/marker";
1213
import { useMapEvent } from "react-leaflet";
1314
import MapMarker from "../map/map-marker";
14-
import IconMarker from "./icons/marker";
1515

1616
type TCoordinatesInputProps = TTextFieldProps & {
1717
dialogTitle: string;
@@ -120,10 +120,10 @@ function PlacedMarker({
120120
setPosition([e.latlng.lat, e.latlng.lng]);
121121
});
122122

123-
return (
124-
position && (
125-
<MapMarker type={markerType} position={position} interactive={false} />
126-
)
123+
return position ? (
124+
<MapMarker type={markerType} position={position} interactive={false} />
125+
) : (
126+
<MapMarker type={markerType} position={[0, 0]} interactive={false} hidden />
127127
);
128128
}
129129

0 commit comments

Comments
 (0)