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