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

ReturnRentButton.tsx ➔ ReturnRentButton   A

Complexity

Conditions 3

Size

Total Lines 37
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 7.608

Importance

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