ADD: added new user data model and updated the administration site
This commit is contained in:
@@ -67,6 +67,7 @@ func main() {
|
|||||||
})
|
})
|
||||||
api.DELETE("/players/:id", func(c *gin.Context) {
|
api.DELETE("/players/:id", func(c *gin.Context) {
|
||||||
player.DeletePlayer(c, db.GetDB())
|
player.DeletePlayer(c, db.GetDB())
|
||||||
|
// c.JSON(http.StatusOK, gin.H{"message": "Player deleted successfully"})
|
||||||
})
|
})
|
||||||
|
|
||||||
port := os.Getenv("PORT")
|
port := os.Getenv("PORT")
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ func GetPlayers(c *gin.Context, db *sql.DB) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CreatePlayer(c *gin.Context, db *sql.DB) {
|
func CreatePlayer(c *gin.Context, db *sql.DB) {
|
||||||
|
log.Println("CreatePlayer called")
|
||||||
var newPlayer User
|
var newPlayer User
|
||||||
var err error
|
var err error
|
||||||
if err := c.ShouldBindJSON(&newPlayer); err != nil {
|
if err := c.ShouldBindJSON(&newPlayer); err != nil {
|
||||||
@@ -149,5 +150,6 @@ func DeletePlayer(c *gin.Context, db *sql.DB) {
|
|||||||
|
|
||||||
log.Printf("User %s (%s) deleted player with ID: %s", c.GetString("userId"), c.GetString("email"), playerID)
|
log.Printf("User %s (%s) deleted player with ID: %s", c.GetString("userId"), c.GetString("email"), playerID)
|
||||||
|
|
||||||
common.RespondMessage(c, "Player deleted successfully")
|
c.JSON(http.StatusOK, gin.H{"message": "Player deleted successfully"})
|
||||||
|
// common.RespondSuccess(c, "Player deleted successfully")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,22 +7,23 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
UUID string `db:"uuid" sql:"VARCHAR(255)" index:"true"`
|
UUID string `db:"uuid" sql:"VARCHAR(255)" index:"true"`
|
||||||
Username string `db:"username" sql:"VARCHAR(100)"`
|
Username string `db:"username" sql:"VARCHAR(100)"`
|
||||||
Email string `db:"email" sql:"VARCHAR(255)" index:"true"`
|
Email string `db:"email" sql:"VARCHAR(255)" index:"true"`
|
||||||
lastname string `db:"lastname" sql:"VARCHAR(100)"`
|
lastname sql.NullString `db:"lastname" sql:"VARCHAR(100)"`
|
||||||
fistname string `db:"fistname" sql:"VARCHAR(100)"`
|
firstname sql.NullString `db:"firstname" sql:"VARCHAR(100)"`
|
||||||
password string `db:"password_hash" sql:"VARCHAR(255)"`
|
password string `db:"password_hash" sql:"VARCHAR(255)"`
|
||||||
phone string `db:"phone" sql:"VARCHAR(20)"`
|
phone sql.NullString `db:"phone" sql:"VARCHAR(20)"`
|
||||||
avatarURL string `db:"avatar_url" sql:"VARCHAR(255)"`
|
avatarURL sql.NullString `db:"avatar_url" sql:"VARCHAR(255)"`
|
||||||
IsActive bool `db:"is_active" sql:"BOOLEAN"`
|
IsActive sql.NullBool `db:"is_active" sql:"BOOLEAN"`
|
||||||
birthday time.Time `db:"birthday" sql:"DATE"`
|
birthday *time.Time `db:"birthday" sql:"DATE"`
|
||||||
createdAt time.Time `db:"created_at" sql:"TIMESTAMP"`
|
createdAt *time.Time `db:"created_at" sql:"TIMESTAMP"`
|
||||||
updatedAt time.Time `db:"updated_at" sql:"TIMESTAMP"`
|
updatedAt *time.Time `db:"updated_at" sql:"TIMESTAMP"`
|
||||||
LastLogin time.Time `db:"last_login" sql:"TIMESTAMP"`
|
LastLogin *time.Time `db:"last_login" sql:"TIMESTAMP"`
|
||||||
Role []string `ignore:"true"` // wird NICHT in der DB angelegt
|
Role []string `ignore:"true"` // wird NICHT in der DB angelegt
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -173,7 +174,7 @@ func savePlayer(db *sql.DB, player User) error {
|
|||||||
|
|
||||||
log.Printf("Saving player: ID=%v, Name=%v, Email=%v", player.UUID, player.Username, player.Email)
|
log.Printf("Saving player: ID=%v, Name=%v, Email=%v", player.UUID, player.Username, player.Email)
|
||||||
|
|
||||||
stmt := "INSERT INTO public.users (id, name, email,password_hash) VALUES ($1, $2, $3,$4)"
|
stmt := "INSERT INTO public.users (UUID, username, email,password_hash) VALUES ($1, $2, $3,$4)"
|
||||||
log.Printf("Generated SQL statement: %s", stmt)
|
log.Printf("Generated SQL statement: %s", stmt)
|
||||||
_, err := db.Exec(stmt, player.UUID, player.Username, player.Email, player.password)
|
_, err := db.Exec(stmt, player.UUID, player.Username, player.Email, player.password)
|
||||||
|
|
||||||
@@ -203,7 +204,7 @@ func AddRoleToPlayer(db *sql.DB, playerID string, role []string) error {
|
|||||||
log.Printf("Adding role '%s' to player with ID: %s", role, playerID)
|
log.Printf("Adding role '%s' to player with ID: %s", role, playerID)
|
||||||
|
|
||||||
stmt := "INSERT INTO public.roles (player_id, role) VALUES ($1, $2)"
|
stmt := "INSERT INTO public.roles (player_id, role) VALUES ($1, $2)"
|
||||||
_, err := db.Exec(stmt, playerID, role)
|
_, err := db.Exec(stmt, playerID, pq.Array(role))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error adding role to player with ID %s: %v", playerID, err)
|
log.Printf("Error adding role to player with ID %s: %v", playerID, err)
|
||||||
return err
|
return err
|
||||||
@@ -214,7 +215,11 @@ func AddRoleToPlayer(db *sql.DB, playerID string, role []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetAllPlayers(db *sql.DB) ([]User, error) {
|
func GetAllPlayers(db *sql.DB) ([]User, error) {
|
||||||
rows, err := db.Query("SELECT id, name, email FROM public.users")
|
// rows, err := db.Query("SELECT id, name, email FROM public.users")
|
||||||
|
// rows, err := db.Query("SELECT u.uuid, u.email, u.username, u.firstname,u.lastname,u.birthday, u.is_active, u.created_at, ur.roleFROM public.users u
|
||||||
|
// LEFT JOIN roles ur ON u.uuid::uuid = ur.player_id ORDER BY u.uuid ASC")
|
||||||
|
rows, err := db.Query(getPLayerWithRolesQuery)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error retrieving users: %v", err)
|
log.Printf("Error retrieving users: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -224,7 +229,16 @@ func GetAllPlayers(db *sql.DB) ([]User, error) {
|
|||||||
var users []User
|
var users []User
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var player User
|
var player User
|
||||||
if err := rows.Scan(&player.UUID, &player.Username, &player.Email); err != nil {
|
if err := rows.Scan(&player.UUID,
|
||||||
|
&player.Email,
|
||||||
|
&player.Username,
|
||||||
|
&player.firstname,
|
||||||
|
&player.lastname,
|
||||||
|
&player.birthday,
|
||||||
|
&player.IsActive,
|
||||||
|
&player.createdAt,
|
||||||
|
(*pq.StringArray)(&player.Role),
|
||||||
|
); err != nil {
|
||||||
log.Printf("Error scanning player row: %v", err)
|
log.Printf("Error scanning player row: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -268,15 +282,15 @@ func GetPlayerByName(db *sql.DB, name string) (User, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DeletePlayer deletes a player from the database by ID.
|
// DeletePlayer deletes a player from the database by ID.
|
||||||
func deletePlayer(db *sql.DB, id string) error {
|
func deletePlayer(db *sql.DB, uuid string) error {
|
||||||
// Delete the player from the database
|
// Delete the player from the database
|
||||||
log.Printf("Deleting player with ID: %s", id)
|
log.Printf("Deleting player with ID: %s", uuid)
|
||||||
_, err := db.Exec("DELETE FROM public.users WHERE id = $1", id)
|
_, err := db.Exec("DELETE FROM public.users WHERE uuid = $1", uuid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error deleting player with ID %s: %v", id, err)
|
log.Printf("Error deleting player with ID %s: %v", uuid, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Printf("User with ID %s deleted successfully", id)
|
log.Printf("User with ID %s deleted successfully", uuid)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
19
backend/internal/player/querys.go
Normal file
19
backend/internal/player/querys.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package player
|
||||||
|
|
||||||
|
const getPLayerWithRolesQuery = `
|
||||||
|
SELECT
|
||||||
|
u.uuid,
|
||||||
|
u.email,
|
||||||
|
u.username,
|
||||||
|
u.firstname,
|
||||||
|
u.lastname,
|
||||||
|
u.birthday,
|
||||||
|
u.is_active,
|
||||||
|
u.created_at,
|
||||||
|
ur.role
|
||||||
|
FROM public.users u
|
||||||
|
LEFT JOIN roles ur ON u.uuid = ur.player_id::uuid
|
||||||
|
`
|
||||||
|
|
||||||
|
// GROUP BY u.uuid
|
||||||
|
// ORDER BY u.uuid ASC;
|
||||||
@@ -10,7 +10,7 @@ import TeamManagement from './pages/Teams';
|
|||||||
import ViewEditPlayer from './pages/ViewEditPlayer';
|
import ViewEditPlayer from './pages/ViewEditPlayer';
|
||||||
import Tournaments from './pages/Tournaments';
|
import Tournaments from './pages/Tournaments';
|
||||||
import NewTournament from './pages/NewTournament';
|
import NewTournament from './pages/NewTournament';
|
||||||
import Administration from './pages/Administration';
|
import Administration from './pages/Administration/Administration';
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export interface User {
|
|||||||
FirstName?: string
|
FirstName?: string
|
||||||
LastName?: string
|
LastName?: string
|
||||||
AvatarUrl?: string
|
AvatarUrl?: string
|
||||||
Roles: UserRole[]
|
Role: UserRole[]
|
||||||
IsActive: boolean
|
IsActive: boolean
|
||||||
Phone?: string
|
Phone?: string
|
||||||
TeamId?: string[]
|
TeamId?: string[]
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import React, { JSX, useEffect, useState } from "react";
|
import React, { JSX, useEffect, useState } from "react";
|
||||||
import { fetchPlayers } from "./api";
|
import { fetchPlayers } from "../api";
|
||||||
import { User, UserRole } from "../components/interfaces/users";
|
import { User, UserRole } from "../../components/interfaces/users";
|
||||||
|
import UsersPage from "./Users";
|
||||||
|
import TMP from "./TMP";
|
||||||
|
|
||||||
// type User = {
|
// type User = {
|
||||||
// id: string;
|
// id: string;
|
||||||
@@ -58,10 +60,9 @@ const contentStyle: React.CSSProperties = {
|
|||||||
|
|
||||||
export default function Administration(): JSX.Element {
|
export default function Administration(): JSX.Element {
|
||||||
const [activeTab, setActiveTab] = useState<
|
const [activeTab, setActiveTab] = useState<
|
||||||
"users" | "teams" | "events" | "settings"
|
"users" | "teams" | "events" | "settings" | "TMP"
|
||||||
>("users");
|
>("users");
|
||||||
|
|
||||||
const [users, setUsers] = useState<User[]>([]);
|
|
||||||
const [teams, setTeams] = useState<Team[]>([]);
|
const [teams, setTeams] = useState<Team[]>([]);
|
||||||
const [events, setEvents] = useState<EventItem[]>([]);
|
const [events, setEvents] = useState<EventItem[]>([]);
|
||||||
|
|
||||||
@@ -86,27 +87,22 @@ export default function Administration(): JSX.Element {
|
|||||||
try {
|
try {
|
||||||
// Replace these endpoints with your backend API
|
// Replace these endpoints with your backend API
|
||||||
|
|
||||||
const users = await fetchPlayers(token);
|
|
||||||
setUsers(users);
|
|
||||||
console.log("Fetched users:", users);
|
|
||||||
const [uRes, tRes, eRes] = await Promise.all([
|
const [uRes, tRes, eRes] = await Promise.all([
|
||||||
fetch("/api/admin/players"),
|
fetch("/api/admin/players"),
|
||||||
fetch("/api/admin/teams"),
|
fetch("/api/admin/teams"),
|
||||||
fetch("/api/admin/events"),
|
fetch("/api/admin/events"),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (!uRes.ok || !tRes.ok || !eRes.ok) {
|
if (!tRes.ok || !eRes.ok) {
|
||||||
throw new Error("Failed to fetch admin resources");
|
throw new Error("Failed to fetch admin resources");
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.log(uRes);
|
// console.log(uRes);
|
||||||
const [uJson, tJson, eJson] = await Promise.all([
|
const [tJson, eJson] = await Promise.all([
|
||||||
uRes.json(),
|
|
||||||
tRes.json(),
|
tRes.json(),
|
||||||
eRes.json(),
|
eRes.json(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// setUsers(uRes as User[]);
|
|
||||||
setTeams(tJson as Team[]);
|
setTeams(tJson as Team[]);
|
||||||
setEvents(eJson as EventItem[]);
|
setEvents(eJson as EventItem[]);
|
||||||
|
|
||||||
@@ -118,48 +114,6 @@ export default function Administration(): JSX.Element {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function toggleAdmin(user: User) {
|
|
||||||
const promote = user.Roles.includes(UserRole.Admin) ? false : true;
|
|
||||||
if (
|
|
||||||
!window.confirm(
|
|
||||||
`${promote ? "Promote" : "Demote"} ${user.Username} to ${
|
|
||||||
promote ? "admin" : "user"
|
|
||||||
}?`
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const res = await fetch(`/api/admin/users/${user.UUID}/role`, {
|
|
||||||
method: "PATCH",
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify({ role: promote ? "admin" : "user" }),
|
|
||||||
});
|
|
||||||
if (!res.ok) throw new Error("Failed to change role");
|
|
||||||
setUsers((prev) =>
|
|
||||||
prev.map((u) => (u.UUID === user.UUID ? { ...u, role: promote ? "admin" : "user" } : u))
|
|
||||||
);
|
|
||||||
} catch (err: any) {
|
|
||||||
alert(err?.message ?? "Failed to update role");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// async function toggleBan(user: User) {
|
|
||||||
// const ban = user.isActive;
|
|
||||||
// if (!window.confirm(`${ban ? "Ban" : "Unban"} ${user.username}?`)) return;
|
|
||||||
// try {
|
|
||||||
// const res = await fetch(`/api/admin/users/${user.id}/ban`, {
|
|
||||||
// method: "PATCH",
|
|
||||||
// headers: { "Content-Type": "application/json" },
|
|
||||||
// body: JSON.stringify({ banned: ban }),
|
|
||||||
// });
|
|
||||||
// if (!res.ok) throw new Error("Failed to update ban");
|
|
||||||
// setUsers((prev) => prev.map((u) => (u.id === user.id ? { ...u, banned: ban } : u)));
|
|
||||||
// } catch (err: any) {
|
|
||||||
// alert(err?.message ?? "Failed to update ban");
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
async function deleteTeam(team: Team) {
|
async function deleteTeam(team: Team) {
|
||||||
if (!window.confirm(`Delete team "${team.name}"? This cannot be undone.`)) return;
|
if (!window.confirm(`Delete team "${team.name}"? This cannot be undone.`)) return;
|
||||||
try {
|
try {
|
||||||
@@ -240,6 +194,14 @@ export default function Administration(): JSX.Element {
|
|||||||
>
|
>
|
||||||
Settings
|
Settings
|
||||||
</button>
|
</button>
|
||||||
|
<button
|
||||||
|
style={tabButtonStyle(activeTab === "TMP")}
|
||||||
|
onClick={() => {
|
||||||
|
setActiveTab("TMP");
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
TMP
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div style={{ marginTop: 20 }}>
|
<div style={{ marginTop: 20 }}>
|
||||||
@@ -258,37 +220,7 @@ export default function Administration(): JSX.Element {
|
|||||||
<h2>Users</h2>
|
<h2>Users</h2>
|
||||||
<p>Manage user accounts, roles and bans.</p>
|
<p>Manage user accounts, roles and bans.</p>
|
||||||
<div style={{ marginTop: 12 }}>
|
<div style={{ marginTop: 12 }}>
|
||||||
{users.length === 0 ? (
|
<UsersPage />
|
||||||
<p>No users found.</p>
|
|
||||||
) : (
|
|
||||||
<table style={{ width: "100%", borderCollapse: "collapse" }}>
|
|
||||||
<thead>
|
|
||||||
<tr style={{ textAlign: "left", borderBottom: "1px solid #eee" }}>
|
|
||||||
<th style={{ padding: "8px 6px" }}>Name</th>
|
|
||||||
<th style={{ padding: "8px 6px" }}>Email</th>
|
|
||||||
<th style={{ padding: "8px 6px" }}>Role</th>
|
|
||||||
<th style={{ padding: "8px 6px" }}>Status</th>
|
|
||||||
<th style={{ padding: "8px 6px" }}>Actions</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{users.map((u) => (
|
|
||||||
<tr key={u.UUID} style={{ borderBottom: "1px solid #fafafa" }}>
|
|
||||||
<td style={{ padding: "8px 6px" }}>{u.Username}</td>
|
|
||||||
<td style={{ padding: "8px 6px" }}>{u.Email}</td>
|
|
||||||
<td style={{ padding: "8px 6px" }}>{u.Roles}</td>
|
|
||||||
{/* <td style={{ padding: "8px 6px" }}>{u.banned ? "Banned" : "Active"}</td> */}
|
|
||||||
{/* <td style={{ padding: "8px 6px" }}>
|
|
||||||
<button onClick={() => toggleAdmin(u)} style={{ marginRight: 8 }}>
|
|
||||||
{u.roles.includes(UserRole.Admin) ? "Demote" : "Promote"}
|
|
||||||
</button> */}
|
|
||||||
{/* <button onClick={() => toggleBan(u)}>{u.banned ? "Unban" : "Ban"}</button> */}
|
|
||||||
{/* </td> */}
|
|
||||||
</tr>
|
|
||||||
))}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
)}
|
)}
|
||||||
@@ -415,6 +347,12 @@ export default function Administration(): JSX.Element {
|
|||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
)}
|
)}
|
||||||
|
{activeTab === "TMP" && (
|
||||||
|
<section>
|
||||||
|
<h2>TMP Tab</h2>
|
||||||
|
<TMP/>
|
||||||
|
</section>
|
||||||
|
)}
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
252
frontend/src/pages/Administration/TMP.tsx
Normal file
252
frontend/src/pages/Administration/TMP.tsx
Normal file
@@ -0,0 +1,252 @@
|
|||||||
|
import React, { useState } from "react";
|
||||||
|
import { createPlayer } from "../api";
|
||||||
|
import { User } from "../../components/interfaces/users";
|
||||||
|
|
||||||
|
type UserRow = { [key: string]: string };
|
||||||
|
|
||||||
|
function parseCSV(text: string): UserRow[] {
|
||||||
|
// Basic CSV parser supporting quoted fields and commas inside quotes.
|
||||||
|
// Returns array of objects using the first row as header.
|
||||||
|
const rows: string[][] = [];
|
||||||
|
const regex = /(?:"([^"]*(?:""[^"]*)*)"|([^",\r\n]*))(,|\r?\n|$)/g;
|
||||||
|
|
||||||
|
let row: string[] = [];
|
||||||
|
let match: RegExpExecArray | null;
|
||||||
|
let i = 0;
|
||||||
|
while ((match = regex.exec(text)) !== null) {
|
||||||
|
const quoted = match[1];
|
||||||
|
const plain = match[2];
|
||||||
|
const delim = match[3];
|
||||||
|
|
||||||
|
const value = quoted !== undefined
|
||||||
|
? quoted.replace(/""/g, '"')
|
||||||
|
: (plain ?? "");
|
||||||
|
row.push(value);
|
||||||
|
|
||||||
|
if (delim === "\n" || delim === "\r\n" || delim === "") {
|
||||||
|
rows.push(row);
|
||||||
|
row = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// safety guard to avoid infinite loops
|
||||||
|
if (++i > 10_000_000) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rows.length === 0) return [];
|
||||||
|
|
||||||
|
const header = rows[0].map(h => h.trim());
|
||||||
|
const dataRows = rows.slice(1).filter(r => r.length > 1 || (r.length === 1 && r[0] !== ""));
|
||||||
|
|
||||||
|
return dataRows.map(r => {
|
||||||
|
const obj: UserRow = {};
|
||||||
|
for (let j = 0; j < header.length; j++) {
|
||||||
|
obj[header[j] ?? `col${j}`] = (r[j] ?? "").trim();
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function postUsersBatch(endpoint: string, users: UserRow[]) {
|
||||||
|
// const res = await fetch(endpoint, {
|
||||||
|
// method: "POST",
|
||||||
|
// headers: { "Content-Type": "application/json" },
|
||||||
|
// body: JSON.stringify({ users }),
|
||||||
|
// });
|
||||||
|
|
||||||
|
// if (!res.ok) {
|
||||||
|
// const text = await res.text();
|
||||||
|
// throw new Error(`API error ${res.status}: ${text}`);
|
||||||
|
// }
|
||||||
|
// return res.json();
|
||||||
|
const normalize = (s: string) => s.toLowerCase().replace(/[^a-z0-9]/g, "");
|
||||||
|
const find = (row: UserRow, candidates: string[]) => {
|
||||||
|
const candNorm = candidates.map(c => normalize(c));
|
||||||
|
for (const k of Object.keys(row)) {
|
||||||
|
if (candNorm.includes(normalize(k))) return (row[k] ?? "").trim();
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
const mapped: User[] = users.map(r => {
|
||||||
|
const uui = find(r, ["uuid", "id", "userId"]) || "";
|
||||||
|
const Username = find(r, ["username", "user_name", "user"]) || "";
|
||||||
|
const Email = find(r, ["email", "e-mail"]) || "";
|
||||||
|
const firstName = find(r, ["firstName", "first_name", "firstname", "first name"]) || "";
|
||||||
|
const lastName = find(r, ["lastName", "last_name", "lastname", "last name"]) || "";
|
||||||
|
const phone = find(r, ["phone", "phoneNumber", "phonenumber", "mobile"]) || undefined;
|
||||||
|
const position = find(r, ["position", "role"]) || undefined;
|
||||||
|
const jerseyNumber = find(r, ["jersey", "number", "jerseyNumber"]) || undefined;
|
||||||
|
const team = find(r, ["team", "club"]) || undefined;
|
||||||
|
const dob = find(r, ["dob", "dateOfBirth", "date_of_birth", "birthdate"]) || undefined;
|
||||||
|
|
||||||
|
// Adjust the returned shape to match your actual User interface.
|
||||||
|
return {
|
||||||
|
uui,
|
||||||
|
Username,
|
||||||
|
Email,
|
||||||
|
firstName,
|
||||||
|
lastName,
|
||||||
|
phone,
|
||||||
|
position,
|
||||||
|
jerseyNumber,
|
||||||
|
team,
|
||||||
|
dob,
|
||||||
|
} as unknown as User;
|
||||||
|
});
|
||||||
|
|
||||||
|
mapped.forEach((u, idx) => {
|
||||||
|
console.log("Creating user:", idx, u);
|
||||||
|
createPlayer({
|
||||||
|
Username: u.Username,
|
||||||
|
Email: u.Email,
|
||||||
|
},
|
||||||
|
localStorage.getItem("token") || "");
|
||||||
|
});
|
||||||
|
// return await createPlayer(mapped, localStorage.getItem("token") || "");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function TMP() {
|
||||||
|
const [preview, setPreview] = useState<UserRow[] | null>(null);
|
||||||
|
const [fileName, setFileName] = useState<string | null>(null);
|
||||||
|
const [status, setStatus] = useState<string | null>(null);
|
||||||
|
const [errors, setErrors] = useState<string[] | null>(null);
|
||||||
|
const [isUploading, setIsUploading] = useState(false);
|
||||||
|
|
||||||
|
const onFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
setStatus(null);
|
||||||
|
setErrors(null);
|
||||||
|
const file = e.target.files?.[0];
|
||||||
|
if (!file) return;
|
||||||
|
setFileName(file.name);
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = () => {
|
||||||
|
const text = String(reader.result ?? "");
|
||||||
|
try {
|
||||||
|
const parsed = parseCSV(text);
|
||||||
|
setPreview(parsed.slice(0, 50)); // preview first 50 rows
|
||||||
|
} catch (err) {
|
||||||
|
setErrors([String(err)]);
|
||||||
|
setPreview(null);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
reader.onerror = () => {
|
||||||
|
setErrors(["Failed to read file"]);
|
||||||
|
setPreview(null);
|
||||||
|
};
|
||||||
|
reader.readAsText(file, "utf-8");
|
||||||
|
};
|
||||||
|
|
||||||
|
const validateUsers = (users: UserRow[]) => {
|
||||||
|
const problems: string[] = [];
|
||||||
|
// require "email" column (case-insensitive)
|
||||||
|
const sample = users[0] ?? {};
|
||||||
|
const hasEmailHeader = Object.keys(sample).some(k => k.toLowerCase() === "email");
|
||||||
|
if (!hasEmailHeader) problems.push('CSV must contain an "email" column.');
|
||||||
|
// quick email validation for each row
|
||||||
|
const emailKey = hasEmailHeader ? Object.keys(sample).find(k => k.toLowerCase() === "email")! : null;
|
||||||
|
if (emailKey) {
|
||||||
|
users.forEach((u, idx) => {
|
||||||
|
const val = (u[emailKey] ?? "").trim();
|
||||||
|
if (!val) problems.push(`Row ${idx + 2}: missing email`);
|
||||||
|
else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(val)) problems.push(`Row ${idx + 2}: invalid email "${val}"`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return { problems, emailKey };
|
||||||
|
};
|
||||||
|
|
||||||
|
const onUpload = async () => {
|
||||||
|
setStatus(null);
|
||||||
|
setErrors(null);
|
||||||
|
if (!preview) {
|
||||||
|
setErrors(["No parsed data to upload. Choose a CSV file first."]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// In a real flow you should re-parse the file to get all rows (we only stored preview).
|
||||||
|
// For simplicity here we assume preview contains the whole parsed dataset when small.
|
||||||
|
// If you expect large files, store entire parsed array in state instead of preview.
|
||||||
|
// For this example, we will assume preview contains all rows (adjust as needed).
|
||||||
|
const users = preview;
|
||||||
|
|
||||||
|
const { problems, emailKey } = validateUsers(users);
|
||||||
|
if (problems.length) {
|
||||||
|
setErrors(problems);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setIsUploading(true);
|
||||||
|
setStatus("Uploading...");
|
||||||
|
try {
|
||||||
|
// Adjust endpoint to match your backend API
|
||||||
|
const endpoint = "/api/users/bulk";
|
||||||
|
// send in chunks to avoid huge payloads
|
||||||
|
const chunkSize = 100;
|
||||||
|
for (let i = 0; i < users.length; i += chunkSize) {
|
||||||
|
const chunk = users.slice(i, i + chunkSize).map(u => {
|
||||||
|
// optional: rename fields to backend expected shape
|
||||||
|
// e.g. map "firstName" -> "first_name" etc.
|
||||||
|
return u;
|
||||||
|
});
|
||||||
|
await postUsersBatch(endpoint, chunk);
|
||||||
|
}
|
||||||
|
setStatus(`Uploaded ${users.length} users successfully.`);
|
||||||
|
} catch (err: any) {
|
||||||
|
setErrors([err.message ?? String(err)]);
|
||||||
|
setStatus("Upload failed.");
|
||||||
|
} finally {
|
||||||
|
setIsUploading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{ padding: 16 }}>
|
||||||
|
<h2>Upload users CSV</h2>
|
||||||
|
|
||||||
|
<input type="file" accept=".csv,text/csv" onChange={onFileChange} />
|
||||||
|
{fileName && <div>Selected file: {fileName}</div>}
|
||||||
|
|
||||||
|
{preview && (
|
||||||
|
<>
|
||||||
|
<h4>Preview (first {preview.length} rows)</h4>
|
||||||
|
<div style={{ maxHeight: 300, overflow: "auto", border: "1px solid #ddd", padding: 8 }}>
|
||||||
|
<table style={{ width: "100%", borderCollapse: "collapse" }}>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
{Object.keys(preview[0]).map(k => (
|
||||||
|
<th key={k} style={{ borderBottom: "1px solid #ccc", textAlign: "left", padding: 4 }}>{k}</th>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{preview.map((row, ri) => (
|
||||||
|
<tr key={ri}>
|
||||||
|
{Object.keys(preview[0]).map((k, ci) => (
|
||||||
|
<td key={ci} style={{ padding: 4, borderBottom: "1px solid #f3f3f3" }}>{row[k]}</td>
|
||||||
|
))}
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div style={{ marginTop: 12 }}>
|
||||||
|
<button onClick={onUpload} disabled={isUploading || !preview}>
|
||||||
|
{isUploading ? "Uploading..." : "Upload to API"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{status && <div style={{ marginTop: 8 }}>{status}</div>}
|
||||||
|
{errors && (
|
||||||
|
<div style={{ marginTop: 8, color: "crimson" }}>
|
||||||
|
<strong>Errors:</strong>
|
||||||
|
<ul>
|
||||||
|
{errors.map((e, i) => <li key={i}>{e}</li>)}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
416
frontend/src/pages/Administration/Users.tsx
Normal file
416
frontend/src/pages/Administration/Users.tsx
Normal file
@@ -0,0 +1,416 @@
|
|||||||
|
import React, { JSX, useEffect, useState } from "react";
|
||||||
|
import { createPlayer, deletePlayer, fetchPlayers, updatePlayer } from "../api";
|
||||||
|
import { User, UserRole } from "../../components/interfaces/users";
|
||||||
|
import { sign } from "crypto";
|
||||||
|
import { create } from "domain";
|
||||||
|
|
||||||
|
// /home/henry/code/Volleyball/frontend/src/pages/Administration/Users.tsx
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
type UserForm = {
|
||||||
|
name: string;
|
||||||
|
email: string;
|
||||||
|
role: string;
|
||||||
|
active: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
const emptyForm = (): UserForm => ({
|
||||||
|
name: "",
|
||||||
|
email: "",
|
||||||
|
role: UserRole.Player,
|
||||||
|
active: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default function UsersPage(): JSX.Element {
|
||||||
|
const [users, setUsers] = useState<User[]>([]);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
const [query, setQuery] = useState("");
|
||||||
|
const [page, setPage] = useState(1);
|
||||||
|
const pageSize = 20;
|
||||||
|
|
||||||
|
const [showForm, setShowForm] = useState(false);
|
||||||
|
const [editing, setEditing] = useState<User | null>(null);
|
||||||
|
const [form, setForm] = useState<UserForm>(emptyForm());
|
||||||
|
const [formLoading, setFormLoading] = useState(false);
|
||||||
|
|
||||||
|
const [deletingId, setDeletingId] = useState<string | null>(null);
|
||||||
|
const [confirmDeleteId, setConfirmDeleteId] = useState<string | null>(null);
|
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const ac = new AbortController();
|
||||||
|
async function load() {
|
||||||
|
setLoading(true);
|
||||||
|
setError(null);
|
||||||
|
try {
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
if (query) params.set("q", query);
|
||||||
|
params.set("page", String(page));
|
||||||
|
params.set("limit", String(pageSize));
|
||||||
|
|
||||||
|
const token = localStorage.getItem('token');
|
||||||
|
if (!token) throw new Error("No auth token found");
|
||||||
|
const data = await fetchPlayers(token);
|
||||||
|
console.log("Loaded users:", data);
|
||||||
|
// const res = await fetch(`/api/users?${params.toString()}`, {
|
||||||
|
// signal: ac.signal,
|
||||||
|
// });
|
||||||
|
|
||||||
|
// if (!res.ok) throw new Error(`Failed to load users: ${res.status}`);
|
||||||
|
// const data = await res.json();
|
||||||
|
// Expecting { users: User[] } or User[] — handle both.
|
||||||
|
const list: User[] = Array.isArray(data) ? data : data.users ?? [];
|
||||||
|
setUsers(list);
|
||||||
|
} catch (err: any) {
|
||||||
|
if (err.name !== "AbortError") setError(err.message || String(err));
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
load();
|
||||||
|
return () => ac.abort();
|
||||||
|
}, [query, page]);
|
||||||
|
|
||||||
|
function openCreate() {
|
||||||
|
setEditing(null);
|
||||||
|
setForm(emptyForm());
|
||||||
|
setShowForm(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function openEdit(u: User) {
|
||||||
|
setEditing(u);
|
||||||
|
setForm({
|
||||||
|
name: u.Username ?? "",
|
||||||
|
email: u.Email ?? "",
|
||||||
|
role: Array.isArray(u.Role) ? u.Role[0] : (u.Role ?? UserRole.Player),
|
||||||
|
active: u.IsActive ?? true,
|
||||||
|
});
|
||||||
|
setShowForm(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function submitForm(e?: React.FormEvent) {
|
||||||
|
e?.preventDefault();
|
||||||
|
setFormLoading(true);
|
||||||
|
setError(null);
|
||||||
|
try {
|
||||||
|
const payload = { ...form };
|
||||||
|
let res: Response;
|
||||||
|
if (editing) {
|
||||||
|
res = await updatePlayer(editing.UUID, {
|
||||||
|
Username: form.name,
|
||||||
|
Email: form.email,
|
||||||
|
}, localStorage.getItem('token') || "");
|
||||||
|
// res = await createPlayer({
|
||||||
|
// Username: form.name,
|
||||||
|
// Email: form.email,
|
||||||
|
// }, localStorage.getItem('token') || "");
|
||||||
|
|
||||||
|
// res = await fetch(`/api/users/${editing.UUID}`, {
|
||||||
|
// method: "PUT",
|
||||||
|
// headers: { "Content-Type": "application/json" },
|
||||||
|
// body: JSON.stringify(payload),
|
||||||
|
// });
|
||||||
|
} else {
|
||||||
|
res = await createPlayer({
|
||||||
|
Username: form.name,
|
||||||
|
Email: form.email,
|
||||||
|
}, localStorage.getItem('token') || "");
|
||||||
|
console.log("Create response:", res);
|
||||||
|
// res = await fetch(`/api/users`, {
|
||||||
|
// method: "POST",
|
||||||
|
// headers: { "Content-Type": "application/json" },
|
||||||
|
// body: JSON.stringify(payload),
|
||||||
|
// });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse response body (support both fetch Response and already-parsed results)
|
||||||
|
let saved: User;
|
||||||
|
if (res && typeof (res as any).json === "function") {
|
||||||
|
const body = await (res as Response).json();
|
||||||
|
saved = (body && (body.data ?? body)) as User;
|
||||||
|
} else {
|
||||||
|
// If `res` is already a parsed object (e.g. from axios-like helpers)
|
||||||
|
const obj = res as any;
|
||||||
|
saved = (obj && (obj.data ?? obj)) as User;
|
||||||
|
}
|
||||||
|
// Update local lists
|
||||||
|
setUsers((prev) => {
|
||||||
|
if (editing) return prev.map((p) => (p.UUID === saved.UUID ? saved : p));
|
||||||
|
return [saved, ...prev];
|
||||||
|
});
|
||||||
|
setShowForm(false);
|
||||||
|
setEditing(null);
|
||||||
|
setForm(emptyForm());
|
||||||
|
} catch (err: any) {
|
||||||
|
setError(err.message || String(err));
|
||||||
|
} finally {
|
||||||
|
setFormLoading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function removeUser(id: string) {
|
||||||
|
setDeletingId(id);
|
||||||
|
setError(null);
|
||||||
|
try {
|
||||||
|
const res = await deletePlayer(id, localStorage.getItem('token') || "");
|
||||||
|
|
||||||
|
setUsers((prev) => prev.filter((u) => u.UUID !== id));
|
||||||
|
} catch (err: any) {
|
||||||
|
setError(err.message || String(err));
|
||||||
|
console.error("Error deleting user:", err.message);
|
||||||
|
} finally {
|
||||||
|
setDeletingId(null);
|
||||||
|
setConfirmDeleteId(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{ padding: 20 }}>
|
||||||
|
<h1 style={{ marginBottom: 8 }}>User Management</h1>
|
||||||
|
|
||||||
|
<div style={{ display: "flex", gap: 8, marginBottom: 12, alignItems: "center" }}>
|
||||||
|
<input
|
||||||
|
placeholder="Search by name or email..."
|
||||||
|
value={query}
|
||||||
|
onChange={(e) => {
|
||||||
|
setQuery(e.target.value);
|
||||||
|
setPage(1);
|
||||||
|
}}
|
||||||
|
style={{ padding: 6, flex: 1 }}
|
||||||
|
/>
|
||||||
|
<button onClick={() => { setQuery(""); setPage(1); }}>Clear</button>
|
||||||
|
<button onClick={openCreate} style={{ marginLeft: 8 }}>
|
||||||
|
New User
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<div style={{ color: "crimson", marginBottom: 12 }}>
|
||||||
|
Error: {error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div style={{ border: "1px solid #ddd", borderRadius: 6, overflowX: "auto" }}>
|
||||||
|
<table style={{ width: "100%", borderCollapse: "collapse" }}>
|
||||||
|
<thead>
|
||||||
|
<tr style={{ textAlign: "left", background: "#f7f7f7" }}>
|
||||||
|
<th style={{ padding: 8 }}>Name</th>
|
||||||
|
<th style={{ padding: 8 }}>Email</th>
|
||||||
|
<th style={{ padding: 8 }}>Role</th>
|
||||||
|
<th style={{ padding: 8 }}>Active</th>
|
||||||
|
<th style={{ padding: 8 }}>Created</th>
|
||||||
|
<th style={{ padding: 8 }}>Actions</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{loading ? (
|
||||||
|
<tr>
|
||||||
|
<td colSpan={6} style={{ padding: 12 }}>
|
||||||
|
Loading...
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
) : users.length === 0 ? (
|
||||||
|
<tr>
|
||||||
|
<td colSpan={6} style={{ padding: 12 }}>
|
||||||
|
No users found.
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
) : (
|
||||||
|
users.map((u) => (
|
||||||
|
<tr key={u.UUID} style={{ borderTop: "1px solid #eee" }}>
|
||||||
|
<td style={{ padding: 8 }}>{u.Username}</td>
|
||||||
|
<td style={{ padding: 8 }}>{u.Email}</td>
|
||||||
|
<td style={{ padding: 8 }}>
|
||||||
|
{u.Role &&
|
||||||
|
Array.isArray(u.Role) ? u.Role.join(", ") : (u.Role ?? "—")}
|
||||||
|
</td>
|
||||||
|
<td style={{ padding: 8 }}>{u.IsActive ? "Yes" : "No"}</td>
|
||||||
|
<td style={{ padding: 8 }}>
|
||||||
|
{u.CreatedAt ? new Date(u.CreatedAt).toLocaleString() : "—"}
|
||||||
|
</td>
|
||||||
|
<td style={{ padding: 8 }}>
|
||||||
|
<button onClick={() => openEdit(u)} style={{ marginRight: 8 }}>
|
||||||
|
Edit
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => setConfirmDeleteId(u.UUID)}
|
||||||
|
disabled={deletingId === u.UUID}
|
||||||
|
style={{ color: "crimson" }}
|
||||||
|
>
|
||||||
|
{deletingId === u.UUID ? "Deleting..." : "Delete"}
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style={{ marginTop: 12, display: "flex", gap: 8, alignItems: "center" }}>
|
||||||
|
<button onClick={() => setPage((p) => Math.max(1, p - 1))} disabled={page === 1}>
|
||||||
|
Prev
|
||||||
|
</button>
|
||||||
|
<div>Page {page}</div>
|
||||||
|
<button onClick={() => setPage((p) => p + 1)} disabled={users.length < pageSize}>
|
||||||
|
Next
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{showForm && (
|
||||||
|
<div
|
||||||
|
role="dialog"
|
||||||
|
aria-modal
|
||||||
|
style={{
|
||||||
|
position: "fixed",
|
||||||
|
inset: 0,
|
||||||
|
display: "flex",
|
||||||
|
alignItems: "center",
|
||||||
|
justifyContent: "center",
|
||||||
|
background: "rgba(0,0,0,0.35)",
|
||||||
|
zIndex: 9999,
|
||||||
|
}}
|
||||||
|
onClick={() => {
|
||||||
|
// click outside to close
|
||||||
|
setShowForm(false);
|
||||||
|
setEditing(null);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<form
|
||||||
|
onSubmit={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
submitForm(e);
|
||||||
|
}}
|
||||||
|
onClick={(e) => e.stopPropagation()}
|
||||||
|
style={{
|
||||||
|
width: 520,
|
||||||
|
maxWidth: "95%",
|
||||||
|
background: "white",
|
||||||
|
padding: 18,
|
||||||
|
borderRadius: 8,
|
||||||
|
boxShadow: "0 6px 20px rgba(0,0,0,0.12)",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<h2 style={{ marginTop: 0 }}>{editing ? "Edit User" : "New User"}</h2>
|
||||||
|
|
||||||
|
<div style={{ marginBottom: 8 }}>
|
||||||
|
<label style={{ display: "block", fontSize: 13, marginBottom: 4 }}>Name</label>
|
||||||
|
<input
|
||||||
|
required
|
||||||
|
value={form.name}
|
||||||
|
onChange={(e) => setForm((f) => ({ ...f, name: e.target.value }))}
|
||||||
|
style={{ width: "100%", padding: 8 }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style={{ marginBottom: 8 }}>
|
||||||
|
<label style={{ display: "block", fontSize: 13, marginBottom: 4 }}>Email</label>
|
||||||
|
<input
|
||||||
|
required
|
||||||
|
type="email"
|
||||||
|
value={form.email}
|
||||||
|
onChange={(e) => setForm((f) => ({ ...f, email: e.target.value }))}
|
||||||
|
style={{ width: "100%", padding: 8 }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style={{ display: "flex", gap: 8, marginBottom: 12 }}>
|
||||||
|
<div style={{ flex: 1 }}>
|
||||||
|
<label style={{ display: "block", fontSize: 13, marginBottom: 4 }}>Role</label>
|
||||||
|
<select
|
||||||
|
value={form.role}
|
||||||
|
onChange={(e) => setForm((f) => ({ ...f, role: e.target.value }))}
|
||||||
|
style={{ width: "100%", padding: 8 }}
|
||||||
|
>
|
||||||
|
<option value="user">User</option>
|
||||||
|
<option value="admin">Admin</option>
|
||||||
|
<option value="coach">Coach</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style={{ minWidth: 120 }}>
|
||||||
|
<label style={{ display: "block", fontSize: 13, marginBottom: 4 }}>Active</label>
|
||||||
|
<select
|
||||||
|
value={String(form.active)}
|
||||||
|
onChange={(e) =>
|
||||||
|
setForm((f) => ({ ...f, active: e.target.value === "true" }))
|
||||||
|
}
|
||||||
|
style={{ width: "100%", padding: 8 }}
|
||||||
|
>
|
||||||
|
<option value="true">Yes</option>
|
||||||
|
<option value="false">No</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style={{ display: "flex", gap: 8, justifyContent: "flex-end" }}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => {
|
||||||
|
setShowForm(false);
|
||||||
|
setEditing(null);
|
||||||
|
}}
|
||||||
|
disabled={formLoading}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</button>
|
||||||
|
<button type="submit" disabled={formLoading}>
|
||||||
|
{formLoading ? "Saving..." : editing ? "Save changes" : "Create user"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{confirmDeleteId && (
|
||||||
|
<div
|
||||||
|
role="dialog"
|
||||||
|
aria-modal
|
||||||
|
style={{
|
||||||
|
position: "fixed",
|
||||||
|
inset: 0,
|
||||||
|
display: "flex",
|
||||||
|
alignItems: "center",
|
||||||
|
justifyContent: "center",
|
||||||
|
background: "rgba(0,0,0,0.35)",
|
||||||
|
zIndex: 10000,
|
||||||
|
}}
|
||||||
|
onClick={() => setConfirmDeleteId(null)}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
onClick={(e) => e.stopPropagation()}
|
||||||
|
style={{
|
||||||
|
background: "white",
|
||||||
|
padding: 24,
|
||||||
|
borderRadius: 8,
|
||||||
|
boxShadow: "0 6px 20px rgba(0,0,0,0.12)",
|
||||||
|
minWidth: 320,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<h3 style={{ marginTop: 0 }}>Confirm Delete</h3>
|
||||||
|
<p>Are you sure you want to delete this user? This action cannot be undone.</p>
|
||||||
|
<div style={{ display: "flex", gap: 8, justifyContent: "flex-end" }}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setConfirmDeleteId(null)}
|
||||||
|
disabled={deletingId === confirmDeleteId}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
style={{ color: "crimson" }}
|
||||||
|
onClick={() => removeUser(confirmDeleteId)}
|
||||||
|
disabled={deletingId === confirmDeleteId}
|
||||||
|
>
|
||||||
|
{deletingId === confirmDeleteId ? "Deleting..." : "Delete"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -11,8 +11,8 @@ import { User, UserRole } from '../components/interfaces/users';
|
|||||||
|
|
||||||
export default function PlayerManagement() {
|
export default function PlayerManagement() {
|
||||||
const [players, setPlayers] = useState<User[]>([]);
|
const [players, setPlayers] = useState<User[]>([]);
|
||||||
const [name, setName] = useState("");
|
const [Username, setName] = useState("");
|
||||||
const [email, setEmail] = useState("");
|
const [Email, setEmail] = useState("");
|
||||||
const [password, setPassword] = useState("");
|
const [password, setPassword] = useState("");
|
||||||
const [editingId, setEditingId] = useState<string | null>(null);
|
const [editingId, setEditingId] = useState<string | null>(null);
|
||||||
const token = localStorage.getItem('token');
|
const token = localStorage.getItem('token');
|
||||||
@@ -35,14 +35,14 @@ export default function PlayerManagement() {
|
|||||||
|
|
||||||
|
|
||||||
const handleAddOrUpdate = () => {
|
const handleAddOrUpdate = () => {
|
||||||
if (!name || !email) return;
|
if (!Username || !Email) return;
|
||||||
|
|
||||||
if (editingId !== null) {
|
if (editingId !== null) {
|
||||||
setPlayers(players.map(p =>
|
setPlayers(players.map(p =>
|
||||||
p.UUID === editingId ? { ...p, name, email } : p
|
p.UUID === editingId ? { ...p, Username, Email } : p
|
||||||
));
|
));
|
||||||
if (token) {
|
if (token) {
|
||||||
updatePlayer(editingId, { name, email }, token);
|
updatePlayer(editingId, { Username, Email }, token);
|
||||||
}
|
}
|
||||||
setEditingId(null);
|
setEditingId(null);
|
||||||
} else {
|
} else {
|
||||||
@@ -50,7 +50,7 @@ export default function PlayerManagement() {
|
|||||||
UUID: "",
|
UUID: "",
|
||||||
Username:"",
|
Username:"",
|
||||||
Email: "",
|
Email: "",
|
||||||
Roles: [UserRole.Player],
|
Role: [UserRole.Player],
|
||||||
IsActive: true,
|
IsActive: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -89,14 +89,14 @@ export default function PlayerManagement() {
|
|||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="Spielername"
|
placeholder="Spielername"
|
||||||
value={name}
|
value={Username}
|
||||||
onChange={(e) => setName(e.target.value)}
|
onChange={(e) => setName(e.target.value)}
|
||||||
className="border p-2 rounded"
|
className="border p-2 rounded"
|
||||||
/>
|
/>
|
||||||
<input
|
<input
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="Email"
|
placeholder="Email"
|
||||||
value={email}
|
value={Email}
|
||||||
onChange={(e) => setEmail(e.target.value)}
|
onChange={(e) => setEmail(e.target.value)}
|
||||||
className="border p-2 rounded"
|
className="border p-2 rounded"
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ const ViewEditPlayer = () => {
|
|||||||
<option value="admin">Admin</option>
|
<option value="admin">Admin</option>
|
||||||
</select>
|
</select>
|
||||||
) : (
|
) : (
|
||||||
<p>{player.Roles}</p>
|
<p>{player.Role}</p>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ export async function createPlayer(player: { Username: string, Email: string },
|
|||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updatePlayer(id: string, player: { name?: string, email?: string }, token: string) {
|
export async function updatePlayer(id: string, player: { Username?: string, Email?: string }, token: string) {
|
||||||
const res = await fetch(`${API_URL}/players/${id}`, {
|
const res = await fetch(`${API_URL}/players/${id}`, {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
|
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
|
||||||
@@ -90,11 +90,12 @@ export async function updatePlayer(id: string, player: { name?: string, email?:
|
|||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deletePlayer(id: string, token: string) {
|
export async function deletePlayer(uuid: string, token: string) {
|
||||||
const res = await fetch(`${API_URL}/players/${id}`, {
|
const res = await fetch(`${API_URL}/players/${uuid}`, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
headers: { Authorization: `Bearer ${token}` },
|
headers: { Authorization: `Bearer ${token}` },
|
||||||
});
|
});
|
||||||
|
console.log("Delete response:", res);
|
||||||
if (!res.ok) throw new Error('Spieler-Löschung fehlgeschlagen');
|
if (!res.ok) throw new Error('Spieler-Löschung fehlgeschlagen');
|
||||||
return res.json();
|
return res.json();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user