|
1
|
|
|
import Map from '../../components/Map'; |
|
2
|
|
|
import { useParams } from "react-router-dom"; |
|
3
|
|
|
import axios from 'axios'; |
|
4
|
|
|
import { API_URL} from '../../helpers/config'; |
|
5
|
|
|
import { useEffect, useState, useRef } from 'react'; |
|
6
|
|
|
import { Scooter, Zone } from '../../helpers/map/leaflet-types' |
|
7
|
|
|
import { Label, ToggleSwitch } from 'flowbite-react'; |
|
8
|
|
|
import AdminGate from '../../components/AdminGate'; |
|
9
|
|
|
import { Badge } from 'flowbite-react'; |
|
10
|
|
|
|
|
11
|
|
|
import BikeList from '../../components/BikeList'; |
|
12
|
|
|
|
|
13
|
|
|
export default function ShowMap() { |
|
14
|
1 |
|
const { city } = useParams(); |
|
15
|
1 |
|
const [zoneData, setZoneData] = useState<Zone[]>([]); |
|
16
|
1 |
|
const [scooterData, setScooterData] = useState<Scooter[]>([]); |
|
17
|
1 |
|
const [realTime, setRealTime] = useState(false); |
|
18
|
1 |
|
const timerRef = useRef<null | number>(null); |
|
19
|
1 |
|
const [trigger, setTrigger] = useState(0); |
|
20
|
|
|
|
|
21
|
1 |
|
const updateRealTime = () => { |
|
22
|
|
|
setRealTime(!realTime); |
|
23
|
2 |
|
if (realTime) |
|
24
|
|
|
{ |
|
25
|
|
|
stopTimer(); |
|
26
|
|
|
} else { |
|
27
|
|
|
startTimer(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
1 |
|
const startTimer = () => { |
|
33
|
2 |
|
if (!timerRef.current) |
|
34
|
|
|
{ |
|
35
|
|
|
timerRef.current = setInterval(() => { |
|
36
|
|
|
setTrigger((prev) => prev + 1); |
|
37
|
|
|
}, 1000); |
|
38
|
|
|
} |
|
39
|
|
|
}; |
|
40
|
|
|
|
|
41
|
1 |
|
const stopTimer = () => { |
|
42
|
2 |
|
if (timerRef.current) |
|
43
|
|
|
{ |
|
44
|
|
|
clearInterval(timerRef.current); |
|
45
|
|
|
timerRef.current = null; |
|
46
|
|
|
} |
|
47
|
|
|
}; |
|
48
|
|
|
|
|
49
|
1 |
|
useEffect(() => { |
|
50
|
1 |
|
const fetchScooters = async() => { |
|
51
|
1 |
|
try { |
|
52
|
1 |
|
const response = await axios.get(`${API_URL}/bike/city/${city}`); |
|
53
|
|
|
setScooterData(response.data); |
|
54
|
|
|
} |
|
55
|
|
|
catch(error) |
|
56
|
|
|
{ |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
1 |
|
fetchScooters(); |
|
60
|
|
|
},[city, trigger]) |
|
61
|
|
|
|
|
62
|
1 |
|
useEffect(() => { |
|
63
|
1 |
|
const fetchZones = async() => { |
|
64
|
1 |
|
try { |
|
65
|
|
|
|
|
66
|
1 |
|
const response = await axios.get(`${API_URL}/zone/city/${city}`); |
|
67
|
|
|
setZoneData(response.data); |
|
68
|
|
|
} |
|
69
|
|
|
catch(error) |
|
70
|
|
|
{ |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
1 |
|
fetchZones(); |
|
74
|
|
|
},[city, trigger]) |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
1 |
|
return ( |
|
78
|
|
|
<> |
|
79
|
|
|
<AdminGate/> |
|
80
|
|
|
<div data-testid="show-map" className="mx-auto sm:max-w-4xl"><Map city={city ?? "Göteborg"} zoneData={zoneData} scooterData={scooterData}/></div> |
|
81
|
|
|
<div className="flex flex-col items-center justify-center my-2 py-2 bg-red-100 rounded-md w-full sm:max-w-xl mx-auto"> |
|
82
|
|
|
<Label htmlFor="realtimetoggle">Vill du uppdatera kartan i realtid?</Label> |
|
83
|
|
|
<ToggleSwitch id="realtimetoggle" checked={realTime} onChange={updateRealTime}>Uppdatera i realtid?</ToggleSwitch> |
|
84
|
|
|
</div> |
|
85
|
|
|
<div id="scooter-list" className="p-4 flex flex-col justify-center w-full"> |
|
86
|
|
|
<div className="mx-auto"> |
|
87
|
|
|
<h2 className="text-4xl font-bold text-gray-900"> Cyklar i {city}: </h2> |
|
88
|
|
|
</div> |
|
89
|
|
|
{scooterData.length > 0 ? ( |
|
90
|
|
|
<> |
|
91
|
|
|
<div className="mx-auto mb-5"> |
|
92
|
|
|
<h2>Antal cyklar: <b>{scooterData.length}</b> </h2> |
|
93
|
|
|
</div> |
|
94
|
|
|
<BikeList scooterData={scooterData} isCityList={false}/> |
|
95
|
|
|
</>) : ( |
|
96
|
|
|
<div className="mx-auto mb-5"> |
|
97
|
|
|
<p>Inga cyklar tillgängliga</p> |
|
98
|
|
|
</div> |
|
99
|
|
|
)} |
|
100
|
|
|
</div> |
|
101
|
|
|
</> |
|
102
|
|
|
) |
|
103
|
|
|
}; |
|
104
|
|
|
|