Passed
Push — development ( e131c7...d0a4d7 )
by Peter
05:27 queued 13s
created

frontend/src/pages/HomePage.tsx   A

Complexity

Total Complexity 3
Complexity/F 0

Size

Lines of Code 48
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 3
mnd 3
bc 3
fnc 0
bpm 0
cpm 0
noi 0
1
import React, {useState} from 'react';
2
import { useDispatch, useSelector } from 'react-redux';
3
import { RootState, AppDispatch } from '../redux/store/store';
4
import { GITHUB_URL } from '../helpers/config';
5
import { Link } from 'react-router-dom';
6
import { setLoggedInOut, setCurrentUser, setToken, setRole } from '../redux/slices/authLogin';
7
8
const HomePage: React.FC = () => {
9
  const dispatch: AppDispatch = useDispatch();
10
  const { role } = useSelector((state: RootState) =>  state.auth);
11
  const { isLoggedIn } = useSelector((state: RootState) =>  state.auth);
12
13
  const handleSwitchRole = () => {
14
      const newRole = role === 'customer' ? 'admin' : 'customer';
15
      dispatch(setRole(newRole));
16
  };
17
18
  const loginUser = (e: React.MouseEvent<HTMLButtonElement>) => {
19
      e.preventDefault();
20
      window.location.href = GITHUB_URL;
21
  };
22
23
  return (
24
    <div className="flex flex-col items-center justify-center h-screen" data-testid="home-page">
25
        { isLoggedIn 
26
        ?
27
        (
28
        <div className="block max-w-sm p-6 bg-white border border-gray-200 rounded-lg shadow hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700">
29
            <h5 className="mb-2 text-2xl font-bold tracking-tight text-gray-800 dark:text-blue">You are logged in</h5>
30
          </div>
31
        )
32
        :
33
          (<>
34
            <h1 className="text-2xl font-bold mb-4 text-green-500">Logga in som {role}</h1>
35
            <button type="submit" className="px-4 py-2 bg-blue-500 text-blue dark:text-white rounded" onClick={(e) => loginUser(e)}>
36
            Logga in
37
            </button>
38
            <button onClick={handleSwitchRole} className="mt-4 text-sm text-blue-500">
39
            Växla till {role === 'customer' ? 'admin' : 'kund'}
40
            </button>
41
          </>
42
          )
43
      }
44
    </div>
45
  );
46
};
47
48
export default HomePage;