Passed
Push — development ( cdc56b...8e8ffd )
by Vad
18:54 queued 08:30
created

CustomerHistory.tsx ➔ MyRentals   B

Complexity

Conditions 5

Size

Total Lines 69
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 22.5589

Importance

Changes 0
Metric Value
cc 5
eloc 59
dl 0
loc 69
ccs 2
cts 18
cp 0.1111
crap 22.5589
rs 7.8751
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 ReturnRentButton from '../../components/ReturnRentButton';
7
8
export default function MyRentals() {
9
  const { isLoggedIn, user, token } = useSelector((state: RootState) =>  state.auth);
10
  const [rentals, setRentals] = useState<any[]>([]);
11
  const navigate = useNavigate();
12
  const [refreshTrigger, setRefreshTrigger] = useState(0);
13
14
  useEffect(() => {
15 2
    if(!isLoggedIn) {
16
      navigate('/');
17
    }
18
  },[isLoggedIn, navigate]);
19
20
21
  useEffect(() => {
22
    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
    getRentals();
35
  }, [user, token, refreshTrigger]);
36
37
38
39
  return (
40
    <div className='p-1/2 flex flex-col items-center'>
41
      <div data-testid="my-rentals">
42
        <h2 className="text-2xl font-bold text-gray-900"> Mina resor </h2> 
43
        </div>
44
      <ul>
45
          { rentals.map((rental, index) => (
46
          <li key={index} className="flex flex-col items-center gap-4 p-4 mb-6 bg-gray-100 rounded-lg shadow-2xl dark:bg-gray-700">
47
          <div className="flex items-center bg-blue-300 p-2 rounded-lg">
48
              <span className="font-semibold">Bike ID:</span>
49
              <span className="ml-2">{rental.id}</span>
50
          </div>
51
          <div className="flex items-center flex-col">
52
              <span className="font-semibold text-gray-600 dark:text-gray-300">Start time:</span>
53
              <span className="text-gray-800 dark:text-white">{rental.startTime}</span>
54
          </div>
55
          {!rental.stopTime && 
56
          <div className="flex items-center flex-col">
57
              <ReturnRentButton tripID={rental.id}/>
58
              </div>
59
          } 
60
          {rental.stopTime && 
61
          <div className="flex items-center flex-col">
62
              <span className="font-semibold text-gray-600 dark:text-gray-300">Stop time:</span>
63
              <span className="text-gray-800 dark:text-white">{rental.stopTime ?? "Still going"}</span>
64
              <span className="mt-2 font-semibold text-gray-600 dark:text-gray-300"> Kostnad:</span>
65
              {/* <span className="ml-2 text-gray-800 dark:text-white">{rental.cost} krosek</span> */}
66
              <span className="text-gray-800 dark:text-white">
67
              {parseFloat(rental.cost).toFixed(2)} kr
68
          </span>
69
70
          </div>
71
          }
72
          </li>
73
          )) }
74
      </ul>
75
    </div>
76
  )
77
}
78