fix
This commit is contained in:
parent
ffede271a0
commit
fd45657304
1 changed files with 26 additions and 19 deletions
|
|
@ -38,7 +38,8 @@ export const SettingsPage: React.FC = () => {
|
|||
setDispName(res.data.disp_name || "");
|
||||
setUserDesc(res.data.user_desc || "");
|
||||
setAvatarId(res.data.image?.id ?? null);
|
||||
setAvatarUrl(res.data.image?.image_path ?? null);
|
||||
const path = res.data.image?.image_path;
|
||||
setAvatarUrl(path ? (path.startsWith('http') ? path : `/media/${path}`) : null);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Failed to fetch user:", err);
|
||||
|
|
@ -54,40 +55,46 @@ const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
|||
|
||||
setUploading(true);
|
||||
setError(null);
|
||||
setSuccess(null);
|
||||
|
||||
// Создаем стандартный объект FormData
|
||||
const formData = new FormData();
|
||||
// Ключ "image" соответствует вашему OpenAPI
|
||||
formData.append("image", file);
|
||||
|
||||
try {
|
||||
// Используем нативный fetch вместо сгенерированного postMediaUpload
|
||||
const response = await fetch("/api/v1/media/upload", {
|
||||
// 1. Загружаем файл на сервер (POST)
|
||||
const uploadRes = await fetch("/api/v1/media/upload", {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
headers: {
|
||||
// Важно: НЕ УКАЗЫВАЕМ Content-Type.
|
||||
// Браузер сам добавит multipart/form-data и сгенерирует boundary.
|
||||
"X-XSRF-TOKEN": xsrfToken || "",
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
throw new Error(errorText || "Upload failed");
|
||||
}
|
||||
if (!uploadRes.ok) throw new Error("Failed to upload image to storage");
|
||||
|
||||
// Ответ должен соответствовать схеме Image.yaml
|
||||
const data = await response.json();
|
||||
const uploadData = await uploadRes.json();
|
||||
const newAvatarId = uploadData.id;
|
||||
|
||||
if (data && data.id) {
|
||||
setAvatarId(data.id);
|
||||
setAvatarUrl(data.image_path ?? null);
|
||||
setSuccess("Image uploaded!");
|
||||
if (newAvatarId) {
|
||||
// 2. СРАЗУ отправляем PATCH запрос для обновления профиля пользователя
|
||||
await updateUser({
|
||||
path: { user_id: userId },
|
||||
body: {
|
||||
avatar_id: newAvatarId, // Привязываем новый ID к юзеру
|
||||
},
|
||||
headers: { "X-XSRF-TOKEN": xsrfToken },
|
||||
});
|
||||
|
||||
// 3. Обновляем локальный стейт для отображения
|
||||
setAvatarId(newAvatarId);
|
||||
const path = uploadData.image_path;
|
||||
setAvatarUrl(path ? (path.startsWith("/") ? path : `/media/${path}`) : null);
|
||||
|
||||
setSuccess("Avatar updated successfully!");
|
||||
}
|
||||
} catch (err: any) {
|
||||
console.error("Upload error:", err);
|
||||
setError(err.message || "Failed to upload image");
|
||||
console.error("Upload & Patch error:", err);
|
||||
setError(err.message || "Failed to update avatar");
|
||||
} finally {
|
||||
setUploading(false);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue