Added possibility of updating user profile
This commit is contained in:
parent
b904f1ec0f
commit
a6d6522f6c
@ -1,6 +1,6 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { getUser, deleteUser } from "../api/user";
|
import { getUser, updateUser } from "../api/user";
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
|
|
||||||
interface User {
|
interface User {
|
||||||
@ -17,59 +17,115 @@ const Profile = () => {
|
|||||||
navigate("/login")
|
navigate("/login")
|
||||||
}
|
}
|
||||||
const [user, setUser] = useState<User | null>(null);
|
const [user, setUser] = useState<User | null>(null);
|
||||||
|
const [editMode, setEditMode] = useState(false);
|
||||||
|
const [formData, setFormData] = useState({ username: "", email: "" });
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchUser = async () => {
|
const fetchUser = async () => {
|
||||||
try {
|
try {
|
||||||
const userData = await getUser(Number(id));
|
const data = await getUser(Number(id));
|
||||||
setUser(userData);
|
setUser(data);
|
||||||
} catch (error) {
|
setFormData({ username: data.username, email: data.email });
|
||||||
console.error("Błąd podczas pobierania danych użytkownika:", error);
|
} catch (err) {
|
||||||
|
console.error("Błąd podczas pobierania danych użytkownika:", err);
|
||||||
|
navigate("/tasks");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchUser();
|
fetchUser();
|
||||||
}, [id]);
|
}, [id]);
|
||||||
|
|
||||||
const handleDeleteAccount = async () => {
|
const handleUpdate = async () => {
|
||||||
if (!window.confirm("Na pewno chcesz usunąć konto? Tej operacji nie da się cofnąć.")) return;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await deleteUser(Number(id));
|
const updated = await updateUser(Number(id), formData);
|
||||||
Cookies.remove("user_id");
|
setUser(updated);
|
||||||
navigate("/login");
|
setEditMode(false);
|
||||||
} catch (error) {
|
} catch (err) {
|
||||||
console.error("Błąd podczas usuwania konta:", error);
|
console.error("Błąd podczas aktualizacji użytkownika:", err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!user) return <div className="p-6">Ładowanie danych użytkownika...</div>;
|
if (!user) return <p>Ładowanie...</p>;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="p-6 max-w-md mx-auto">
|
<div className="p-6 max-w-xl mx-auto">
|
||||||
<h1 className="text-2xl font-bold mb-4">Twój profil</h1>
|
<h1 className="text-2xl font-bold mb-6">👤 Twój profil</h1>
|
||||||
|
|
||||||
<div className="bg-white shadow rounded p-4 mb-4">
|
<div className="mb-4">
|
||||||
<p><strong>Nazwa użytkownika:</strong> {user.username}</p>
|
<label className="block font-semibold mb-1">Nazwa użytkownika:</label>
|
||||||
<p><strong>Email:</strong> {user.email}</p>
|
{editMode ? (
|
||||||
<p><strong>Rola:</strong> {user.role}</p>
|
<input
|
||||||
|
type="text"
|
||||||
|
value={formData.username}
|
||||||
|
onChange={(e) => setFormData({ ...formData, username: e.target.value })}
|
||||||
|
className="border p-2 rounded w-full"
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<p>{user.username}</p>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex flex-col gap-2">
|
<div className="mb-4">
|
||||||
<button
|
<label className="block font-semibold mb-1">Email:</label>
|
||||||
onClick={() => navigate("/profile/change-password")}
|
{editMode ? (
|
||||||
className="bg-blue-500 text-white p-2 rounded hover:bg-blue-600"
|
<input
|
||||||
>
|
type="email"
|
||||||
🔐 Zmień hasło
|
value={formData.email}
|
||||||
</button>
|
onChange={(e) => setFormData({ ...formData, email: e.target.value })}
|
||||||
|
className="border p-2 rounded w-full"
|
||||||
<button
|
/>
|
||||||
onClick={handleDeleteAccount}
|
) : (
|
||||||
className="bg-red-500 text-white p-2 rounded hover:bg-red-600"
|
<p>{user.email}</p>
|
||||||
>
|
)}
|
||||||
🗑️ Usuń konto
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div className="mb-6">
|
||||||
|
<label className="block font-semibold mb-1">Rola:</label>
|
||||||
|
<p>{user.role}</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{editMode ? (
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<button
|
||||||
|
onClick={handleUpdate}
|
||||||
|
className="bg-green-500 text-white px-4 py-2 rounded hover:bg-green-600"
|
||||||
|
>
|
||||||
|
💾 Zapisz zmiany
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
setEditMode(false);
|
||||||
|
setFormData({ username: user.username, email: user.email });
|
||||||
|
}}
|
||||||
|
className="bg-gray-300 px-4 py-2 rounded hover:bg-gray-400"
|
||||||
|
>
|
||||||
|
Anuluj
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<button
|
||||||
|
onClick={() => setEditMode(true)}
|
||||||
|
className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-600"
|
||||||
|
>
|
||||||
|
✏️ Edytuj profil
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
{!editMode && (
|
||||||
|
<div className="mt-6 flex flex-col gap-2">
|
||||||
|
<button
|
||||||
|
onClick={() => navigate("/profile/change-password")}
|
||||||
|
className="bg-yellow-500 text-white px-4 py-2 rounded hover:bg-yellow-600"
|
||||||
|
>
|
||||||
|
🔒 Zmień hasło
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => navigate("/profile/delete")}
|
||||||
|
className="bg-red-500 text-white px-4 py-2 rounded hover:bg-red-600"
|
||||||
|
>
|
||||||
|
❌ Usuń konto
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user