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

frontend/src/pages/UserListPage.tsx   A

Complexity

Total Complexity 3
Complexity/F 0

Size

Lines of Code 74
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 64
dl 0
loc 74
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, useEffect } from 'react';
2
import { useDispatch, useSelector } from 'react-redux';
3
import { RootState, AppDispatch } from '../redux/store/store';
4
import { API_URL, getHeader } from '../helpers/config';
5
import axios from 'axios';
6
import { Link } from 'react-router-dom';
7
8
interface User {
9
  githubId: string;
10
  username: string;
11
  email: string;
12
  roles: string[];
13
  hasAcceptedTerms: boolean;
14
  avatarUrl?: string;
15
}
16
17
const UserListPage: React.FC = () => {
18
  const { token } = useSelector((state: RootState) => state.auth);
19
  const [users, setUsers] = useState<User[]>([]);
20
  const [loading, setLoading] = useState(true);
21
22
  useEffect(() => {
23
    const usersFromBackend = async () => {
24
      try {
25
        const response = await axios.get(`${API_URL}/users`, getHeader(token));
26
        setUsers(response.data);
27
      } catch (error) {
28
        console.error('Failed to fetch users:', error);
29
      } finally {
30
        setLoading(false);
31
      }
32
    };
33
    usersFromBackend();
34
  }, [token]);
35
36
  if (loading) {
37
    return <div>Laddar användarlistan...</div>;
38
  }
39
40
  return (
41
    <div className="w-full max-w-4xl mx-auto p-6 bg-white border border-gray-200 rounded-lg shadow dark:bg-gray-800 dark:border-gray-700">
42
      <h1 className="text-2xl font-bold text-center mb-6 text-gray-900 dark:text-white">Användarlista</h1>
43
      <div className="space-y-4">
44
        {users.map((user) => (
45
          <div
46
            key={user.githubId}
47
            className="p-4 bg-gray-50 border border-gray-300 rounded-lg shadow-sm dark:bg-gray-900 dark:border-gray-700">
48
            <h2 className="text-lg font-semibold text-gray-800 dark:text-white">
49
              <Link to={`/user/${user.githubId}`} className="text-blue-500 hover:underline">
50
                {user.username}
51
              </Link>
52
              </h2>
53
            <p className="text-gray-600 dark:text-gray-400">Email: {user.email}</p>
54
            <p className="text-gray-600 dark:text-gray-400">Roller: {user.roles.join(', ')}</p>
55
            <p className="text-gray-600 dark:text-gray-400">
56
              {user.hasAcceptedTerms
57
                ? 'Godkänt användarvillkor'
58
                : 'Ej godkänt användarvillkor'}
59
            </p>
60
            {user.avatarUrl && (
61
              <img
62
                src={user.avatarUrl}
63
                alt={`${user.username}'s avatar`}
64
                className="w-16 h-16 rounded-full mt-4"
65
              />
66
            )}
67
          </div>
68
        ))}
69
      </div>
70
    </div>
71
  );
72
};
73
74
export default UserListPage;