feat: first settings page version
All checks were successful
Build (frontend build only) / build (push) Successful in 2m50s
All checks were successful
Build (frontend build only) / build (push) Successful in 2m50s
This commit is contained in:
parent
ee15139fdb
commit
56337237ed
2 changed files with 194 additions and 134 deletions
|
|
@ -5,6 +5,7 @@ import UserPage from "./pages/UserPage/UserPage";
|
|||
import TitlesPage from "./pages/TitlesPage/TitlesPage";
|
||||
import TitlePage from "./pages/TitlePage/TitlePage";
|
||||
import { LoginPage } from "./pages/LoginPage/LoginPage";
|
||||
import { SettingsPage } from "./pages/SettingsPage/SettingsPage";
|
||||
import { Header } from "./components/Header/Header";
|
||||
|
||||
// import { OpenAPI } from "./api";
|
||||
|
|
@ -46,6 +47,12 @@ const App: React.FC = () => {
|
|||
element={userId ? <UserPage userId={userId} /> : <LoginPage />}
|
||||
/>
|
||||
|
||||
{/*settings*/}
|
||||
<Route
|
||||
path="/settings"
|
||||
element={userId ? <SettingsPage /> : <LoginPage />}
|
||||
/>
|
||||
|
||||
{/* titles */}
|
||||
<Route path="/titles" element={<TitlesPage />} />
|
||||
<Route path="/titles/:id" element={<TitlePage />} />
|
||||
|
|
|
|||
|
|
@ -1,154 +1,207 @@
|
|||
// import React, { useEffect, useState } from "react";
|
||||
// import { updateUser, getUsersId } from "../../api";
|
||||
// import { useNavigate } from "react-router-dom";
|
||||
import React, { useEffect, useState, useRef } from "react";
|
||||
import { updateUser, getUsersId, postMediaUpload } from "../../api";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
// export const SettingsPage: React.FC = () => {
|
||||
// const navigate = useNavigate();
|
||||
export const SettingsPage: React.FC = () => {
|
||||
const navigate = useNavigate();
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
// const userId = Number(localStorage.getItem("user_id"));
|
||||
// const initialNickname = localStorage.getItem("user_name") || "";
|
||||
// const [mail, setMail] = useState("");
|
||||
// const [nickname, setNickname] = useState(initialNickname);
|
||||
// const [dispName, setDispName] = useState("");
|
||||
// const [userDesc, setUserDesc] = useState("");
|
||||
// const [avatarId, setAvatarId] = useState<number | null>(null);
|
||||
const userId = Number(localStorage.getItem("user_id"));
|
||||
|
||||
// const [loading, setLoading] = useState(false);
|
||||
// const [success, setSuccess] = useState<string | null>(null);
|
||||
// const [error, setError] = useState<string | null>(null);
|
||||
// Состояния для полей формы
|
||||
const [mail, setMail] = useState("");
|
||||
const [nickname, setNickname] = useState("");
|
||||
const [dispName, setDispName] = useState("");
|
||||
const [userDesc, setUserDesc] = useState("");
|
||||
const [avatarId, setAvatarId] = useState<number | null>(null);
|
||||
const [avatarUrl, setAvatarUrl] = useState<string | null>(null);
|
||||
|
||||
// useEffect(() => {
|
||||
// const fetch = async () => {
|
||||
// const res = await getUsersId({
|
||||
// path: { user_id: String(userId) },
|
||||
// });
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [uploading, setUploading] = useState(false);
|
||||
const [success, setSuccess] = useState<string | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
// setProfile(res.data);
|
||||
// };
|
||||
// Загружаем текущие данные пользователя при входе
|
||||
useEffect(() => {
|
||||
const fetchUserData = async () => {
|
||||
try {
|
||||
const res = await getUsersId({
|
||||
path: { user_id: String(userId) },
|
||||
});
|
||||
if (res.data) {
|
||||
setMail(res.data.mail || "");
|
||||
setNickname(res.data.nickname || "");
|
||||
setDispName(res.data.disp_name || "");
|
||||
setUserDesc(res.data.user_desc || "");
|
||||
setAvatarId(res.data.image?.id ?? null);
|
||||
setAvatarUrl(res.data.image?.image_path ?? null);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Failed to fetch user:", err);
|
||||
}
|
||||
};
|
||||
fetchUserData();
|
||||
}, [userId]);
|
||||
|
||||
// fetch();
|
||||
// }, [userId]);
|
||||
// Обработка загрузки файла
|
||||
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (!file) return;
|
||||
|
||||
// const saveSettings = async (e: React.FormEvent) => {
|
||||
// e.preventDefault();
|
||||
// setLoading(true);
|
||||
// setSuccess(null);
|
||||
// setError(null);
|
||||
setUploading(true);
|
||||
setError(null);
|
||||
|
||||
// try {
|
||||
// const res = await updateUser({
|
||||
// path: { user_id: userId },
|
||||
// body: {
|
||||
// ...(mail ? { mail } : {}),
|
||||
// ...(nickname ? { nickname } : {}),
|
||||
// ...(dispName ? { disp_name: dispName } : {}),
|
||||
// ...(userDesc ? { user_desc: userDesc } : {}),
|
||||
// ...(avatarId !== undefined ? { avatar_id: avatarId } : {}),
|
||||
// }
|
||||
// });
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
|
||||
// // Обновляем локальное отображение username
|
||||
// if (nickname) {
|
||||
// localStorage.setItem("user_name", nickname);
|
||||
// window.dispatchEvent(new Event("storage")); // чтобы Header обновился
|
||||
// }
|
||||
try {
|
||||
const res = await postMediaUpload({
|
||||
body: formData,
|
||||
// Сбрасываем заголовок, чтобы браузер сам поставил multipart/form-data + boundary
|
||||
headers: { "Content-Type": undefined as any },
|
||||
});
|
||||
|
||||
// setSuccess("Settings updated!");
|
||||
// setTimeout(() => navigate("/profile"), 800);
|
||||
if (res.data && res.data.id) {
|
||||
setAvatarId(res.data.id);
|
||||
setAvatarUrl(res.data.image_path ?? null); // Для мгновенного превью
|
||||
setSuccess("Image uploaded!");
|
||||
}
|
||||
} catch (err: any) {
|
||||
setError("Failed to upload image");
|
||||
} finally {
|
||||
setUploading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// } catch (err: any) {
|
||||
// console.error(err);
|
||||
// setError(err?.message || "Failed to update settings");
|
||||
// } finally {
|
||||
// setLoading(false);
|
||||
// }
|
||||
// };
|
||||
const saveSettings = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setLoading(true);
|
||||
setSuccess(null);
|
||||
setError(null);
|
||||
|
||||
// return (
|
||||
// <div className="max-w-2xl mx-auto p-6">
|
||||
// <h1 className="text-3xl font-bold mb-6">User Settings</h1>
|
||||
try {
|
||||
await updateUser({
|
||||
path: { user_id: userId },
|
||||
body: {
|
||||
mail: mail || undefined,
|
||||
nickname: nickname || undefined,
|
||||
disp_name: dispName || undefined,
|
||||
user_desc: userDesc || undefined,
|
||||
avatar_id: avatarId, // Может быть числом или null для удаления
|
||||
},
|
||||
});
|
||||
|
||||
// {success && <div className="text-green-600 mb-4">{success}</div>}
|
||||
// {error && <div className="text-red-600 mb-4">{error}</div>}
|
||||
localStorage.setItem("user_name", nickname);
|
||||
window.dispatchEvent(new Event("storage"));
|
||||
|
||||
// <form className="space-y-6" onSubmit={saveSettings}>
|
||||
// {/* Email */}
|
||||
// <div>
|
||||
// <label className="block text-sm font-medium mb-1">Email</label>
|
||||
// <input
|
||||
// type="email"
|
||||
// value={mail}
|
||||
// onChange={(e) => setMail(e.target.value)}
|
||||
// placeholder="example@mail.com"
|
||||
// className="w-full px-4 py-2 border rounded-lg"
|
||||
// />
|
||||
// </div>
|
||||
setSuccess("Settings updated successfully!");
|
||||
setTimeout(() => navigate("/profile"), 1000);
|
||||
} catch (err: any) {
|
||||
setError(err?.message || "Failed to update settings");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// {/* Nickname */}
|
||||
// <div>
|
||||
// <label className="block text-sm font-medium mb-1">Nickname</label>
|
||||
// <input
|
||||
// type="text"
|
||||
// value={nickname}
|
||||
// onChange={(e) => setNickname(e.target.value)}
|
||||
// className="w-full px-4 py-2 border rounded-lg"
|
||||
// />
|
||||
// </div>
|
||||
return (
|
||||
<div className="max-w-2xl mx-auto p-6">
|
||||
<h1 className="text-3xl font-bold mb-6">User Settings</h1>
|
||||
|
||||
// {/* Display name */}
|
||||
// <div>
|
||||
// <label className="block text-sm font-medium mb-1">Display name</label>
|
||||
// <input
|
||||
// type="text"
|
||||
// value={dispName}
|
||||
// onChange={(e) => setDispName(e.target.value)}
|
||||
// placeholder="Shown name"
|
||||
// className="w-full px-4 py-2 border rounded-lg"
|
||||
// />
|
||||
// </div>
|
||||
{success && <div className="p-3 bg-green-100 text-green-700 rounded-lg mb-4">{success}</div>}
|
||||
{error && <div className="p-3 bg-red-100 text-red-700 rounded-lg mb-4">{error}</div>}
|
||||
|
||||
// {/* Bio */}
|
||||
// <div>
|
||||
// <label className="block text-sm font-medium mb-1">Bio</label>
|
||||
// <textarea
|
||||
// value={userDesc}
|
||||
// onChange={(e) => setUserDesc(e.target.value)}
|
||||
// rows={4}
|
||||
// className="w-full px-4 py-2 border rounded-lg resize-none"
|
||||
// />
|
||||
// </div>
|
||||
<div className="mb-8 flex flex-col items-center p-4 border rounded-xl bg-gray-50">
|
||||
<div className="relative w-32 h-32 mb-4 group">
|
||||
<img
|
||||
src={avatarUrl || "https://via.placeholder.com/150"}
|
||||
alt="Avatar"
|
||||
className="w-full h-full object-cover rounded-full border-4 border-white shadow-md"
|
||||
/>
|
||||
{uploading && (
|
||||
<div className="absolute inset-0 flex items-center justify-center bg-black/40 rounded-full text-white text-xs">
|
||||
Uploading...
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
// {/* Avatar ID */}
|
||||
// <div>
|
||||
// <label className="block text-sm font-medium mb-1">Avatar ID</label>
|
||||
// <input
|
||||
// type="number"
|
||||
// value={avatarId ?? ""}
|
||||
// onChange={(e) => {
|
||||
// const v = e.target.value;
|
||||
// setAvatarId(v === "" ? null : Number(v));
|
||||
// }}
|
||||
// placeholder="Image ID"
|
||||
// className="w-full px-4 py-2 border rounded-lg"
|
||||
// />
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => fileInputRef.current?.click()}
|
||||
className="px-4 py-2 bg-white border border-gray-300 rounded-lg text-sm font-medium hover:bg-gray-100"
|
||||
>
|
||||
Change photo
|
||||
</button>
|
||||
{avatarId && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => { setAvatarId(null); setAvatarUrl(null); }}
|
||||
className="px-4 py-2 text-sm text-red-600 hover:text-red-700"
|
||||
>
|
||||
Remove
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
className="hidden"
|
||||
accept="image/*"
|
||||
onChange={handleFileChange}
|
||||
/>
|
||||
</div>
|
||||
|
||||
// <button
|
||||
// type="button"
|
||||
// onClick={() => setAvatarId(null)}
|
||||
// className="text-sm text-blue-600 mt-1 hover:underline"
|
||||
// >
|
||||
// Remove avatar
|
||||
// </button>
|
||||
// </div>
|
||||
<form className="space-y-6" onSubmit={saveSettings}>
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium mb-1">Nickname</label>
|
||||
<input
|
||||
type="text"
|
||||
value={nickname}
|
||||
onChange={(e) => setNickname(e.target.value)}
|
||||
className="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-blue-500 outline-none"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-sm font-medium mb-1">Display Name</label>
|
||||
<input
|
||||
type="text"
|
||||
value={dispName}
|
||||
onChange={(e) => setDispName(e.target.value)}
|
||||
className="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-blue-500 outline-none"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
// <button
|
||||
// type="submit"
|
||||
// disabled={loading}
|
||||
// className="w-full bg-blue-600 text-white py-2 rounded-lg font-semibold hover:bg-blue-700 disabled:opacity-50"
|
||||
// >
|
||||
// {loading ? "Saving..." : "Save changes"}
|
||||
// </button>
|
||||
// </form>
|
||||
// </div>
|
||||
// );
|
||||
// };
|
||||
<div>
|
||||
<label className="block text-sm font-medium mb-1">Email</label>
|
||||
<input
|
||||
type="email"
|
||||
value={mail}
|
||||
onChange={(e) => setMail(e.target.value)}
|
||||
className="w-full px-4 py-2 border rounded-lg focus:ring-2 focus:ring-blue-500 outline-none"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-sm font-medium mb-1">Bio</label>
|
||||
<textarea
|
||||
value={userDesc}
|
||||
onChange={(e) => setUserDesc(e.target.value)}
|
||||
rows={4}
|
||||
className="w-full px-4 py-2 border rounded-lg resize-none focus:ring-2 focus:ring-blue-500 outline-none"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={loading || uploading}
|
||||
className="w-full bg-blue-600 text-white py-3 rounded-lg font-bold hover:bg-blue-700 disabled:opacity-50 transition-colors"
|
||||
>
|
||||
{loading ? "Saving Changes..." : "Save Settings"}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue