Skip to content

Commit 3050562

Browse files
committed
display current place on map
1 parent ef2d19d commit 3050562

File tree

10 files changed

+181
-85
lines changed

10 files changed

+181
-85
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// src/components/map/MapLayer.tsx
2+
import { TileLayer, Marker } from "react-leaflet";
3+
4+
interface MapLayerProps {
5+
initialPosition: [number, number];
6+
}
7+
8+
const BaseMapLayers = ({ initialPosition }: MapLayerProps) => {
9+
return (
10+
<>
11+
<TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
12+
<Marker position={initialPosition} />
13+
</>
14+
);
15+
};
16+
17+
export default BaseMapLayers;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { useEffect, useRef } from "react";
2+
import { useMap } from "react-leaflet";
3+
import L from "leaflet";
4+
import { useGeolocationContext } from "../../providers/GeolocationContext";
5+
6+
const CurrentLocationMarker = () => {
7+
const map = useMap();
8+
const { location, error } = useGeolocationContext();
9+
const markerRef = useRef<L.Marker>();
10+
11+
useEffect(() => {
12+
if (location) {
13+
const lat = location.coords.latitude;
14+
const lng = location.coords.longitude;
15+
16+
if (markerRef.current) {
17+
markerRef.current.setLatLng([lat, lng]);
18+
} else {
19+
markerRef.current = L.marker([lat, lng]).addTo(map);
20+
}
21+
}
22+
}, [location, map]);
23+
24+
if (error) {
25+
console.error(`Geolocation error (${error.code}): ${error.message}`);
26+
}
27+
28+
return null;
29+
};
30+
31+
export default CurrentLocationMarker;

frontend/src/components/Map/Map.tsx

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// src/components/map/MapComponent.tsx
2+
import React from "react";
3+
import { MapContainer } from "react-leaflet";
4+
import { useGeolocationContext } from "../../providers/GeolocationContext";
5+
import ChangeView from "../../utils/ChangeView";
6+
import CurrentLocationMarker from "./CurrentLocationMarker";
7+
import BaseMapLayers from "./BaseMapLayers";
8+
import { getMapZoom, getInitialCoords } from "./functions";
9+
import "./style.css";
10+
11+
const MapContainerWrapper = () => {
12+
const params = new URLSearchParams(window.location.search);
13+
const paramLat = params.get("lat");
14+
const paramLng = params.get("lng");
15+
16+
const { location } = useGeolocationContext();
17+
18+
const coords = getInitialCoords({
19+
paramLat,
20+
paramLng,
21+
userLocation: location,
22+
});
23+
const zoom = getMapZoom(paramLat, paramLng);
24+
25+
return (
26+
<MapContainer
27+
center={coords}
28+
zoom={zoom}
29+
style={{ height: "100vh", width: "100%" }}
30+
>
31+
<BaseMapLayers initialPosition={coords} />
32+
<ChangeView coords={coords} zoom={zoom} />
33+
<CurrentLocationMarker />
34+
</MapContainer>
35+
);
36+
};
37+
38+
export default MapContainerWrapper;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export const getMapZoom = (
2+
paramLat: string | null,
3+
paramLng: string | null
4+
): number => {
5+
if (paramLat === null && paramLng === null) {
6+
return 20;
7+
}
8+
return 16;
9+
};
10+
11+
interface InitialCoordsParams {
12+
paramLat: string | null;
13+
paramLng: string | null;
14+
userLocation: GeolocationPosition | null;
15+
}
16+
17+
export const getInitialCoords = ({
18+
paramLat,
19+
paramLng,
20+
userLocation,
21+
}: InitialCoordsParams): [number, number] => {
22+
const defaultLat = 50.740717; // Coordonnées par défaut
23+
const defaultLng = 2.258634;
24+
25+
const lat = paramLat
26+
? parseFloat(paramLat)
27+
: userLocation?.coords.latitude || defaultLat;
28+
const lng = paramLng
29+
? parseFloat(paramLng)
30+
: userLocation?.coords.longitude || defaultLng;
31+
32+
return [lat, lng];
33+
};

frontend/src/components/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import MapComponent from "./Map/Map";
1+
import MapContainerWrapper from "./Map/MapContainerWrapper";
22
import Navbar from "./NavBar/NavBar";
33
import Card from "./Card/Card";
44
import ListCards from "./ListCards/ListCards";
55

6-
export { Navbar, Card, ListCards, MapComponent };
6+
export { Navbar, Card, ListCards, MapContainerWrapper };

frontend/src/hooks/useGeolocation.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import { useEffect, useState } from "react";
22

33
const useGeolocation = () => {
4-
interface LocationPosition {
5-
coords: {
6-
latitude: number;
7-
longitude: number;
8-
accuracy: number;
9-
};
10-
}
4+
// interface LocationPosition {
5+
// coords: {
6+
// latitude: number;
7+
// longitude: number;
8+
// accuracy: number;
9+
// };
10+
// }
1111

12-
interface LocationPositionError {
13-
code: number;
14-
message: string;
15-
}
12+
// interface LocationPositionError {
13+
// code: number;
14+
// message: string;
15+
// }
1616

17-
const [location, setLocation] = useState<LocationPosition | null>(null);
18-
const [error, setError] = useState<LocationPositionError | null>(null);
17+
const [location, setLocation] = useState<GeolocationPosition | null>(null);
18+
const [error, setError] = useState<GeolocationPositionError | null>(null);
1919

2020
useEffect(() => {
2121
const watchPosition = navigator.geolocation.watchPosition(
22-
(position: LocationPosition) => {
22+
(position: GeolocationPosition) => {
2323
setLocation(position);
2424
console.log("Geolocation position:", position);
2525
},
26-
(error: LocationPositionError) => {
26+
(error: GeolocationPositionError) => {
2727
setError(error);
2828
console.error(`Geolocation error (${error.code}): ${error.message}`);
2929
}

frontend/src/pages/Homepage/HomePage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from "react";
2-
import { MapComponent } from "src/components";
2+
import { MapContainerWrapper } from "src/components";
33

44
const HomePage = () => {
55
return (
66
<div>
7-
<MapComponent />
7+
<MapContainerWrapper />
88
</div>
99
);
1010
};

frontend/src/pages/LogInPage/LogInForm.tsx

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import React, { useState, useContext } from "react";
22
import axios from "axios";
3-
import { Box, Button, Input, Stack, Heading, Container, Text, useToast } from "@chakra-ui/react";
3+
import {
4+
Box,
5+
Button,
6+
Input,
7+
Stack,
8+
Heading,
9+
Container,
10+
Text,
11+
useToast,
12+
} from "@chakra-ui/react";
413
import { useNavigate } from "react-router-dom";
514
import { AuthContext } from "src/contexts/AuthContext";
615
import PORT from "src/utils/constant";
@@ -27,7 +36,7 @@ const LoginForm = () => {
2736

2837
toast({
2938
title: "Connexion réussie",
30-
description: "Vous allez être redirigé vers votre profil.",
39+
description: `Bienvenue ${username} !`,
3140
status: "success",
3241
duration: 3000,
3342
isClosable: true,
@@ -37,7 +46,6 @@ const LoginForm = () => {
3746
setTimeout(() => {
3847
navigate("/");
3948
}, 3000);
40-
4149
} catch (error) {
4250
setErrorMessage("Nom d'utilisateur ou mot de passe invalide");
4351
toast({
@@ -53,9 +61,17 @@ const LoginForm = () => {
5361
};
5462

5563
return (
56-
<Container p={4} minH="100vh" display="flex" alignItems="center" justifyContent="center">
64+
<Container
65+
p={4}
66+
minH="100vh"
67+
display="flex"
68+
alignItems="center"
69+
justifyContent="center"
70+
>
5771
<Box w="full" maxW="md" p={6} borderWidth={1} borderRadius="md">
58-
<Heading textAlign="center" mb={6}>Connexion</Heading>
72+
<Heading textAlign="center" mb={6}>
73+
Connexion
74+
</Heading>
5975
<Stack as="form" spacing={4} onSubmit={handleSubmit}>
6076
<Input
6177
placeholder="Email ou Nom d'utilisateur"
@@ -71,7 +87,9 @@ const LoginForm = () => {
7187
required
7288
/>
7389
{errorMessage && <Text color="red.500">{errorMessage}</Text>}
74-
<Button colorScheme="teal" type="submit">Se connecter</Button>
90+
<Button colorScheme="teal" type="submit">
91+
Se connecter
92+
</Button>
7593
</Stack>
7694
</Box>
7795
</Container>

frontend/src/utils/ChangeView.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { useEffect } from "react";
2+
import { useMap } from "react-leaflet";
3+
4+
interface ChangeViewProps {
5+
coords: [number, number];
6+
zoom: number;
7+
}
8+
9+
const ChangeView = ({ coords, zoom }: ChangeViewProps) => {
10+
const map = useMap();
11+
12+
useEffect(() => {
13+
map.setView(coords, zoom);
14+
}, [coords, zoom, map]);
15+
16+
return null;
17+
};
18+
19+
export default ChangeView;

0 commit comments

Comments
 (0)