frontend/src/pages/user/CustomerHistory.tsx   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 5

Size

Lines of Code 77
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
eloc 66
dl 0
loc 77
ccs 12
cts 18
cp 0.6667
rs 10
c 0
b 0
f 0
wmc 5
mnd 4
bc 4
fnc 1
bpm 4
cpm 5
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B CustomerHistory.tsx ➔ CustomerHistory 0 66 5
1
import { useState, useEffect } from 'react';
2
import { RootState } from '../../redux/store/store';
3
import { useNavigate } from 'react-router-dom';
4
import { useSelector } from 'react-redux';
5
import { allRentals, Rental } from '../../helpers/bike-functions';
6
import UserGate from '../../components/UserGate';
7
import ReturnRentButton from '../../components/ReturnRentButton';
8
import { formatTimestamp } from '../../helpers/other-functions';
9
10
export default function CustomerHistory() {
11 3
  const { isLoggedIn, user, token } = useSelector((state: RootState) =>  state.auth);
12 2
  const [rentals, setRentals] = useState<Rental[]>([]);
13 2
  const navigate = useNavigate();
14 2
  const [refreshTrigger] = useState(0);
15
16 2
  useEffect(() => {
17 2
    if(!isLoggedIn) {
18 1
      navigate('/');
19
    }
20
  },[isLoggedIn, navigate]);
21
22 2
  useEffect(() => {
23 1
    const getRentals = async () => {
24 4
        if (user && token) {
25
26
            setRentals([]);
27
            setTimeout(async () => {
28
              const rentalData = await allRentals(token);
29
              rentalData.reverse()
30
              setRentals(rentalData)}
31
              , 100);
32
33
        }
34
    };
35 1
    getRentals();
36
  }, [user, token, refreshTrigger]);
37
38
39 2
  return (
40
    <div data-testid="my-rentals" className='p-4 flex flex-col justify-center items-center'>
41
      <UserGate/>
42
      <div>
43
        <h2 className="text-2xl font-bold text-gray-900"> Mina resor </h2> 
44
        </div>
45
      <ul className="w-full sm:max-w-xl">
46
          { rentals.map((rental, index) => (
47
          <li key={index} className="flex flex-col flex-nowrap items-center justify-center gap-4 p-4 mb-6 bg-gray-100 rounded-lg shadow-md dark:bg-gray-700 sm:flex-row">
48
          <div className="flex items-center bg-purple-100 p-1 rounded-lg">
49
              <span className="font-semibold">id:</span>
50
              <span className="ml-2">{rental.id}</span>
51
          </div>
52
          <div className="flex items-center bg-green-100 p-1 rounded-lg">
53
              <span className="font-semibold text-gray-600 dark:text-gray-300">Starttid</span>
54
              <span className="ml-2 text-gray-800 dark:text-white">{formatTimestamp(rental.startTime)}</span>
55
          </div>
56
          {!rental.stopTime && 
57
              <ReturnRentButton tripID={rental.id}/>
58
          } 
59
          {rental.stopTime &&
60
          <>
61
          <div className="flex items-center bg-pink-100 p-1 rounded-lg">
62
              <span className="font-semibold text-gray-600 dark:text-gray-300">Sluttid:</span>
63
              <span className="ml-2 text-gray-800 dark:text-white">{formatTimestamp(rental.stopTime) ?? "Still going"}</span>
64
              </div>
65
          <div className="flex items-center bg-blue-100 p-1 rounded-lg">
66
              <span className="font-semibold text-gray-600 dark:text-gray-300"> Kostnad:</span>
67
              <span className="ml-2 text-gray-800 dark:text-white">{rental.cost.toFixed(2).replace('.', ',')} SEK</span>
68
          </div>
69
          </>
70
          }
71
          </li>
72
          )) }
73
      </ul>
74
    </div>
75
  )
76
}
77