|
1
|
|
|
|
|
2
|
|
|
import { useEffect, useState } from 'react'; |
|
3
|
|
|
|
|
4
|
|
|
import { API_URL, getHeader} from '../../helpers/config'; |
|
5
|
|
|
import axios, {AxiosError} from 'axios'; |
|
6
|
|
|
import { Scooter } from '../../helpers/bike-functions'; |
|
7
|
|
|
import { RootState } from '../../redux/store/store'; |
|
8
|
|
|
import { useSelector } from 'react-redux'; |
|
9
|
|
|
import AdminGate from '../../components/AdminGate'; |
|
10
|
|
|
import BikeList from '../../components/BikeList'; |
|
11
|
|
|
|
|
12
|
|
|
export default function AllBikes() { |
|
13
|
1 |
|
const [scooterData, setScooterData] = useState<Scooter[]>([]); |
|
14
|
2 |
|
const { token } = useSelector((state: RootState) => state.auth); |
|
15
|
|
|
|
|
16
|
1 |
|
useEffect(() => { |
|
17
|
1 |
|
const fetchScooters = async() => { |
|
18
|
1 |
|
try { |
|
19
|
1 |
|
const response = await axios.get(`${API_URL}/bike`, getHeader(token)); |
|
20
|
|
|
setScooterData(response.data); |
|
21
|
|
|
} |
|
22
|
|
|
catch(error) |
|
23
|
|
|
{ |
|
24
|
4 |
|
if (error instanceof AxiosError && error.response?.data?.message) { |
|
25
|
|
|
console.log(error.response?.data?.messag) |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
1 |
|
fetchScooters(); |
|
30
|
|
|
},[token]) |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
1 |
|
return ( |
|
34
|
|
|
<div data-testid="all-scooter-list" className="p-4 flex flex-col justify-center w-full"> |
|
35
|
|
|
<AdminGate/> |
|
36
|
|
|
<div className="mx-auto"> |
|
37
|
|
|
<h2 className="text-4xl font-bold text-gray-900"> Alla cyklar </h2> |
|
38
|
|
|
</div> |
|
39
|
|
|
{scooterData.length > 0 ? ( |
|
40
|
|
|
<> |
|
41
|
|
|
<div className="mx-auto mb-5"> |
|
42
|
|
|
<h2>Antal cyklar: <b>{scooterData.length}</b> </h2> |
|
43
|
|
|
</div> |
|
44
|
|
|
<BikeList scooterData={scooterData} isCityList={true}/> |
|
45
|
|
|
</>) : ( |
|
46
|
|
|
<div className="mx-auto mb-5"> |
|
47
|
|
|
<p>Inga cyklar tillgängliga</p> |
|
48
|
|
|
</div> |
|
49
|
|
|
)} |
|
50
|
|
|
</div> |
|
51
|
|
|
) |
|
52
|
|
|
}; |
|
53
|
|
|
|