Passed
Push — development ( 2f9cbb...cdc56b )
by Peter
09:24 queued 15s
created

frontend/src/pages/admin/UserListPage.tsx   A

Complexity

Total Complexity 3
Complexity/F 0

Size

Lines of Code 76
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 68.75%

Importance

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