| Conditions | 9 |
| Total Lines | 92 |
| Code Lines | 75 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import Map from '../../components/Map'; |
||
| 9 | |||
| 10 | export default function ShowMap() { |
||
| 11 | const { city } = useParams(); |
||
| 12 | const [zoneData, setZoneData] = useState<Zone[]>([]); |
||
| 13 | const [scooterData, setScooterData] = useState<Scooter[]>([]); |
||
| 14 | const [realTime, setRealTime] = useState(false); |
||
| 15 | const timerRef = useRef<null | number>(null); |
||
| 16 | const [trigger, setTrigger] = useState(0); |
||
| 17 | |||
| 18 | const updateRealTime = () => { |
||
| 19 | setRealTime(!realTime); |
||
| 20 | if (realTime) |
||
| 21 | { |
||
| 22 | stopTimer(); |
||
| 23 | } else { |
||
| 24 | startTimer(); |
||
| 25 | } |
||
| 26 | |||
| 27 | } |
||
| 28 | |||
| 29 | const startTimer = () => { |
||
| 30 | if (!timerRef.current) |
||
| 31 | { |
||
| 32 | timerRef.current = setInterval(() => { |
||
| 33 | setTrigger((prev) => prev + 1); |
||
| 34 | }, 1000); |
||
| 35 | } |
||
| 36 | }; |
||
| 37 | |||
| 38 | const stopTimer = () => { |
||
| 39 | if (timerRef.current) |
||
| 40 | { |
||
| 41 | clearInterval(timerRef.current); |
||
| 42 | timerRef.current = null; |
||
| 43 | } |
||
| 44 | }; |
||
| 45 | |||
| 46 | useEffect(() => { |
||
| 47 | const fetchScooters = async() => { |
||
| 48 | try { |
||
| 49 | const response = await axios.get(`${API_URL}/bike/city/${city}`); |
||
| 50 | setScooterData(response.data); |
||
| 51 | } |
||
| 52 | catch(error) |
||
| 53 | { |
||
| 54 | } |
||
| 55 | } |
||
| 56 | fetchScooters(); |
||
| 57 | },[city, trigger]) |
||
| 58 | |||
| 59 | useEffect(() => { |
||
| 60 | const fetchZones = async() => { |
||
| 61 | try { |
||
| 62 | |||
| 63 | const response = await axios.get(`${API_URL}/zone/city/${city}`); |
||
| 64 | setZoneData(response.data); |
||
| 65 | } |
||
| 66 | catch(error) |
||
| 67 | { |
||
| 68 | } |
||
| 69 | } |
||
| 70 | fetchZones(); |
||
| 71 | },[city, trigger]) |
||
| 72 | |||
| 73 | |||
| 74 | return ( |
||
| 75 | <> |
||
| 76 | <AdminGate/> |
||
| 77 | <div data-testid="show-map"><Map city={city ?? "Göteborg"} zoneData={zoneData} scooterData={scooterData}/></div> |
||
| 78 | <Label htmlFor="realtimetoggle">Vill du uppdatera kartan i realtid?</Label> |
||
| 79 | <ToggleSwitch id="realtimetoggle" checked={realTime} onChange={updateRealTime}>Uppdatera i realtid?</ToggleSwitch> |
||
| 80 | <div id="scooter-list" className="mt-4 bg-gray-600 rounded"> |
||
| 81 | <h2 className="text-xl font-bold mb-2">Cyklar i {city}:</h2> |
||
| 82 | {scooterData.length > 0 ? ( |
||
| 83 | <ul className="list-disc pl-6 list-none"> |
||
| 84 | {scooterData.map((scooter) => ( |
||
| 85 | <li key={scooter.id} className="mb-2"> |
||
| 86 | <div className="mt-4 p-6 mx-auto w-1/2 hover:opacity-5 bg-gray-400 rounded text-center"> |
||
| 87 | <h2><span className="font-semibold">ID:</span> {scooter.id} -{" "}</h2> |
||
| 88 | <span className="font-semibold">Batteri:</span> {scooter.batteryLevel}% -{" "} |
||
| 89 | <span className="font-semibold">Status:</span> {scooter.status} -{" "} |
||
| 90 | <span className="font-semibold">Longitud:</span> {scooter.longitude} -{" "} |
||
| 91 | <span className="font-semibold">Latitud:</span> {scooter.latitude} |
||
| 92 | </div> |
||
| 93 | </li> |
||
| 94 | ))} |
||
| 95 | </ul> |
||
| 96 | ) : ( |
||
| 97 | <p>Inga cyklar tillgängliga i denna stad.</p> |
||
| 98 | )} |
||
| 99 | </div> |
||
| 100 | </> |
||
| 101 | ) |
||
| 103 |