1
|
|
|
|
2
|
|
|
import { useState } from 'react'; |
3
|
|
|
|
4
|
|
|
import { API_URL } from '../../helpers/config'; |
5
|
|
|
import axios from 'axios'; |
6
|
|
|
import { Zone } from '../../helpers/map/leaflet-types' |
7
|
|
|
import { Scooter } from '../../helpers/bike-functions'; |
8
|
|
|
import { Label, Select, Button } from "flowbite-react"; |
9
|
|
|
import Map from '../../components/Map'; |
10
|
|
|
import ZoneTables from '../../components/ZoneTables'; |
11
|
|
|
import AdminGate from '../../components/AdminGate'; |
12
|
|
|
|
13
|
|
|
export default function AllZones() { |
14
|
1 |
|
const [city, setCity] = useState("Välj stad"); |
15
|
1 |
|
const [zoneDataParking, setZoneDataParking] = useState<Zone[]>(); |
16
|
1 |
|
const [zoneDataLoading, setZoneDataLoading] = useState<Zone[]>(); |
17
|
1 |
|
const [zoneDataTotal, setZoneDataTotal] = useState<Zone[]>(); |
18
|
1 |
|
const [bikeTotal, setBikeTotal] = useState<Scooter[]>(); |
19
|
1 |
|
const [stadTitel, setStadTitel] = useState(""); |
20
|
|
|
|
21
|
1 |
|
const changeCity = (e: React.ChangeEvent<HTMLSelectElement>)=> { |
22
|
|
|
const selectedCity = e.target.value as "Göteborg" | "Jönköping" | "Karlshamn"; |
23
|
|
|
|
24
|
|
|
setCity(selectedCity); |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
const loadZoneData = async (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => { |
28
|
|
|
e.preventDefault(); |
29
|
|
|
try |
30
|
|
|
{ |
31
|
|
|
const responseParking = await axios.get(`${API_URL}/zone?type=parking&includes=bikes&city=${city}`); |
32
|
|
|
const responseLoading = await axios.get(`${API_URL}/zone?type=charging&includes=bikes&city=${city}`); |
33
|
|
|
setZoneDataLoading(responseLoading.data.zones); |
34
|
|
|
setZoneDataParking(responseParking.data.zones); |
35
|
|
|
const totalZones = responseLoading.data?.zones?.concat(responseParking.data?.zones); |
36
|
|
|
setZoneDataTotal(totalZones); |
37
|
|
|
let bikes: Scooter[] = []; |
38
|
|
|
totalZones.map((zone: Zone) => { |
39
|
2 |
|
if (zone.bikes) { |
40
|
|
|
bikes = bikes.concat(zone.bikes); // Lägg till bikes om de finns |
41
|
|
|
} |
42
|
|
|
}); |
43
|
|
|
setBikeTotal(bikes); |
44
|
|
|
setStadTitel(city); |
45
|
|
|
|
46
|
|
|
} catch (error) |
47
|
|
|
{ |
48
|
|
|
console.log(error); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
return ( |
54
|
|
|
<div data-testid="allzones"> |
55
|
|
|
<AdminGate/> |
56
|
|
|
<div className="flex justify-center items-center space-x-4"> |
57
|
|
|
<div className="mb-2 block"> |
58
|
|
|
<Label htmlFor="stad" value="Välj stad" /> |
59
|
|
|
</div> |
60
|
|
|
<div className="mb-2 block"> |
61
|
|
|
<Select id="stad" value={city} onChange={(e) => changeCity(e)} required> |
62
|
|
|
{city === "Välj stad" && <option value="Välj stad">Välj stad</option>} |
63
|
|
|
<option>Göteborg</option> |
64
|
|
|
<option>Jönköping</option> |
65
|
|
|
<option>Karlshamn</option> |
66
|
|
|
</Select> |
67
|
|
|
</div> |
68
|
|
|
|
69
|
|
|
<div className="mb-2 block"> |
70
|
|
|
<Button disabled={city === "Välj stad"} onClick={(e) => loadZoneData(e)}> |
71
|
|
|
Tryck för att ladda zoner/cyklar |
72
|
|
|
</Button> |
73
|
|
|
</div> |
74
|
|
|
</div> |
75
|
|
|
<Map city={city} zoneData={zoneDataTotal ?? []} scooterData={bikeTotal ?? []}/> |
76
|
|
|
<div className="w-full text-center my-4"> |
77
|
|
|
<h1 className="text-3xl font-bold">Parkeringszoner i {stadTitel}</h1> |
78
|
|
|
<ZoneTables zoneData={zoneDataParking}/> |
79
|
|
|
</div> |
80
|
|
|
<div className="w-full text-center my-4"> |
81
|
|
|
<h1 className="text-3xl font-bold">Laddzoner i {stadTitel}</h1> |
82
|
|
|
<ZoneTables zoneData={zoneDataLoading}/> |
83
|
|
|
</div> |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
</div> |
87
|
|
|
); |
88
|
|
|
}; |
89
|
|
|
|