Passed
Pull Request — development (#84)
by Vad
12:05
created

frontend/src/pages/admin/AllZones.tsx   A

Complexity

Total Complexity 10
Complexity/F 10

Size

Lines of Code 93
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 35.7%

Importance

Changes 0
Metric Value
eloc 78
dl 0
loc 93
ccs 10
cts 28
cp 0.357
rs 10
c 0
b 0
f 0
wmc 10
mnd 9
bc 9
fnc 1
bpm 9
cpm 10
noi 1

1 Function

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