Passed
Push — development ( 8e8ffd...1af938 )
by Peter
18:46 queued 06:25
created

CustomerHistory.tsx ➔ CustomerHistory   B

Complexity

Conditions 6

Size

Total Lines 79
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 11.6215

Importance

Changes 0
Metric Value
cc 6
eloc 71
dl 0
loc 79
ccs 12
cts 26
cp 0.4615
crap 11.6215
rs 7.0157
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

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 } from '../../helpers/bike-functions';
6
import UserGate from '../../components/UserGate';
7
import ReturnRentButton from '../../components/ReturnRentButton';
8
9
export default function CustomerHistory() {
10 3
  const { isLoggedIn, user, token } = useSelector((state: RootState) =>  state.auth);
11 2
  const [rentals, setRentals] = useState<any[]>([]);
12 2
  const navigate = useNavigate();
13 2
  const [refreshTrigger, setRefreshTrigger] = useState(0);
14
15 2
  useEffect(() => {
16 2
    if(!isLoggedIn) {
17 1
      navigate('/');
18
    }
19
  },[isLoggedIn, navigate]);
20
21 2
  useEffect(() => {
22 1
    const getRentals = async () => {
23 4
        if (user && token) {
24
25
            setRentals([]);
26
            setTimeout(async () => {
27
              const rentalData = await allRentals(token);
28
              rentalData.reverse()
29
              setRentals(rentalData)}
30
              , 100);
31
32
        }
33
    };
34 1
    getRentals();
35
  }, [user, token, refreshTrigger]);
36
37
  function formatTimestamp(isoString: string) {
38
    const date = new Date(isoString);
39
    const yyyy = date.getFullYear();
40
    const mm = String(date.getMonth() + 1).padStart(2, '0');
41
    const dd = String(date.getDate()).padStart(2, '0');
42
    const hh = String(date.getHours()).padStart(2, '0');
43
    const min = String(date.getMinutes()).padStart(2, '0');
44
    const ss = String(date.getSeconds()).padStart(2, '0');
45
46
    return `${yyyy}-${mm}-${dd} ${hh}:${min}:${ss}`;
47
}
48
49 2
  return (
50
    <div data-testid="my-rentals" className='p-4 flex flex-col justify-center items-center'>
51
      <UserGate/>
52
      <div>
53
        <h2 className="text-2xl font-bold text-gray-900"> Mina resor </h2> 
54
        </div>
55
      <ul className="w-full sm:max-w-xl">
56
          { rentals.map((rental, index) => (
57
          <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">
58
          <div className="flex items-center bg-purple-100 p-1 rounded-lg">
59
              <span className="font-semibold">id:</span>
60
              <span className="ml-2">{rental.id}</span>
61
          </div>
62
          <div className="flex items-center bg-green-100 p-1 rounded-lg">
63
              <span className="font-semibold text-gray-600 dark:text-gray-300">Starttid</span>
64
              <span className="ml-2 text-gray-800 dark:text-white">{formatTimestamp(rental.startTime)}</span>
65
          </div>
66
          {!rental.stopTime && 
67
          <div className="flex items-center">
68
              <ReturnRentButton tripID={rental.id}/>
69
          </div>
70
          } 
71
          {rental.stopTime &&
72
          <>
73
          <div className="flex items-center bg-pink-100 p-1 rounded-lg">
74
              <span className="font-semibold text-gray-600 dark:text-gray-300">Sluttid:</span>
75
              <span className="ml-2 text-gray-800 dark:text-white">{formatTimestamp(rental.stopTime) ?? "Still going"}</span>
76
              </div>
77
          <div className="flex items-center bg-blue-100 p-1 rounded-lg">
78
              <span className="font-semibold text-gray-600 dark:text-gray-300"> Kostnad:</span>
79
              <span className="ml-2 text-gray-800 dark:text-white">{rental.cost.toFixed(2).replace('.', ',')} SEK</span>
80
          </div>
81
          </>
82
          }
83
          </li>
84
          )) }
85
      </ul>
86
    </div>
87
  )
88
}
89