| Conditions | 4 |
| Total Lines | 37 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 7 |
| CRAP Score | 6 |
| Changes | 0 | ||
| 1 | import { Label, ToggleSwitch } from 'flowbite-react'; |
||
| 9 | |||
| 10 | export default function RealTimeUpdate({timerRef, realTime, setRealTime, setTrigger} : Props) { |
||
| 11 | |||
| 12 | 3 | const updateRealTime = () => { |
|
| 13 | setRealTime(!realTime); |
||
| 14 | 2 | if (realTime) |
|
| 15 | { |
||
| 16 | stopTimer(); |
||
| 17 | } else { |
||
| 18 | startTimer(); |
||
| 19 | } |
||
| 20 | } |
||
| 21 | |||
| 22 | 3 | const startTimer = () => { |
|
| 23 | 2 | if (!timerRef.current) |
|
| 24 | { |
||
| 25 | timerRef.current = setInterval(() => { |
||
| 26 | setTrigger((prev: number) => prev + 1); |
||
| 27 | }, 1000); |
||
| 28 | } |
||
| 29 | }; |
||
| 30 | |||
| 31 | 3 | const stopTimer = () => { |
|
| 32 | 2 | if (timerRef.current) |
|
| 33 | { |
||
| 34 | clearInterval(timerRef.current); |
||
| 35 | timerRef.current = null; |
||
| 36 | } |
||
| 37 | }; |
||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | 3 | return ( |
|
| 42 | <div> |
||
| 43 | <Label htmlFor="realtimetoggle">Vill du uppdatera kartan i realtid?</Label> |
||
| 44 | <ToggleSwitch id="realtimetoggle" data-testid="realtimeupdate" checked={realTime} onChange={updateRealTime}>Uppdatera i realtid?</ToggleSwitch> |
||
| 45 | </div> |
||
| 46 | ) |
||
| 48 |