Passed
Push — development ( 4e024d...529d1a )
by Vad
09:33 queued 16s
created

ReturnRentButton.tsx ➔ ReturnRentButton   B

Complexity

Conditions 5

Size

Total Lines 41
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 7.5412

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 41
rs 8.5973
c 0
b 0
f 0
ccs 8
cts 15
cp 0.5333
cc 5
crap 7.5412
1
import { useState } from 'react';
2
import { useSelector } from 'react-redux';
3
import { RootState } from '../redux/store/store';
4
import { toastOptionsError, toastOptionsSuccess } from '../helpers/config';
5
import { returnBike } from '../helpers/bike-functions';
6
import { toast } from 'react-toastify';
7
import { formatTimestamp } from '../helpers/other-functions';
8
9
function ReturnRentButton( {tripID}  : {tripID: number | string}) {
10
11 2
    const { token } = useSelector((state: RootState) =>  state.auth);
12 1
    const [showTime, setShowTime] = useState(false);
13 1
    const [stopTime, setStopTime] = useState('2024-12-28T13:01:50.801Z')
14 1
    const [cost, setCost] = useState(0)
15 1
    const returnBikeId = async (tripID: number | string) => {
16
        const data = await returnBike(tripID, token);
17
        console.log(data);
18 2
        if (data.statusCode === 200)
19
        {
20
          toast.success(`Trip: ${tripID} has been ended.`, toastOptionsSuccess);
21
          setShowTime(true);
22 2
          setStopTime(data.stopTime || 'Unknown stop time');
23
          setCost(data.cost);
24
        } else 
25
        {
26
          toast.error("Bike was not returned", toastOptionsError);
27
        }
28
      }
29
30
31 2
  return showTime ?
32
    (<>
33
          <div className="flex items-center bg-pink-100 p-1 rounded-lg">
34
              <span className="font-semibold text-gray-600 dark:text-gray-300">Sluttid:</span>
35
              <span className="ml-2 text-gray-800 dark:text-white">{formatTimestamp(stopTime) ?? "Still going"}</span>
36
              </div>
37
          <div className="flex items-center bg-blue-100 p-1 rounded-lg">
38
              <span className="font-semibold text-gray-600 dark:text-gray-300"> Kostnad:</span>
39
              <span className="ml-2 text-gray-800 dark:text-white">{cost.toFixed(2).replace('.', ',')} SEK</span>
40
          </div>
41
42
     </>)
43
    :
44
    (
45
      <button data-testid="returnrentbutton" type="button" onClick={async () => await returnBikeId(tripID)} className="text-white bg-blue-700 hover:bg-blue-800
46
      focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5
47
      me-2 mb-2 focus:outline-none">
48
      Ongoing. Return bike</button>
49
    )
50
    
51
}
52
53
export default ReturnRentButton;
54