Test Failed
Pull Request — development (#86)
by Vad
10:46
created

AllBikes.tsx ➔ AllBikes   B

Complexity

Conditions 8

Size

Total Lines 40
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 8.0877

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 40
rs 7.1973
c 0
b 0
f 0
ccs 8
cts 9
cp 0.8889
cc 8
crap 8.0877
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
    const [scooterData, setScooterData] = useState<Scooter[]>([]);
14
    const { token } = useSelector((state: RootState) =>  state.auth);
15 1
16 2
    useEffect(() => {
17
        const fetchScooters = async() => {
18 1
        try {
19 1
                const response = await axios.get(`${API_URL}/bike`, getHeader(token));
20 1
                setScooterData(response.data);
21 1
            }
22
            catch(error)
23
            { 
24
                if (error instanceof AxiosError && error.response?.data?.message) {
25
                    console.log(error.response?.data?.messag)
26
                }
27
            }
28 1
      }
29
      fetchScooters();
30
      },[token])
31
    
32 1
  
33
  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