Merge branch 'front' of ssh://meowgit.nekoea.red:22222/nihonium/nyanimedb into front
All checks were successful
Build (frontend build only) / build (push) Successful in 2m55s
All checks were successful
Build (frontend build only) / build (push) Successful in 2m55s
This commit is contained in:
commit
542e4b52e1
1 changed files with 23 additions and 16 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import React, { useEffect, useState, useRef } from "react";
|
import React, { useEffect, useState, useRef } from "react";
|
||||||
import { updateUser, getUsersId, postMediaUpload } from "../../api";
|
import { updateUser, getUsersId } from "../../api";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { useCookies } from 'react-cookie';
|
import { useCookies } from 'react-cookie';
|
||||||
|
|
||||||
|
|
@ -48,39 +48,46 @@ export const SettingsPage: React.FC = () => {
|
||||||
}, [userId]);
|
}, [userId]);
|
||||||
|
|
||||||
// Обработка загрузки файла
|
// Обработка загрузки файла
|
||||||
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
const file = e.target.files?.[0];
|
const file = e.target.files?.[0];
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
|
|
||||||
setUploading(true);
|
setUploading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
|
|
||||||
// Создаем FormData строго по спецификации
|
// Создаем стандартный объект FormData
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
|
// Ключ "image" соответствует вашему OpenAPI
|
||||||
formData.append("image", file);
|
formData.append("image", file);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Используем fetch напрямую или убеждаемся, что клиент принимает FormData
|
// Используем нативный fetch вместо сгенерированного postMediaUpload
|
||||||
const res = await postMediaUpload({
|
const response = await fetch("/api/v1/media/upload", {
|
||||||
// @ts-ignore - обходим строгую типизацию тела, если она ожидает объект
|
method: "POST",
|
||||||
body: formData,
|
body: formData,
|
||||||
headers: {
|
headers: {
|
||||||
// УДАЛЯЕМ Content-Type вообще.
|
// Важно: НЕ УКАЗЫВАЕМ Content-Type.
|
||||||
// Если оставить undefined, некоторые клиенты могут подставить 'application/json'.
|
// Браузер сам добавит multipart/form-data и сгенерирует boundary.
|
||||||
// Нам нужно, чтобы заголовок отсутствовал в объекте headers,
|
"X-XSRF-TOKEN": xsrfToken || "",
|
||||||
// тогда fetch сам выставит multipart/form-data с boundary.
|
|
||||||
"X-XSRF-TOKEN": xsrfToken,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res.data && res.data.id) {
|
if (!response.ok) {
|
||||||
setAvatarId(res.data.id);
|
const errorText = await response.text();
|
||||||
setAvatarUrl(res.data.image_path ?? null);
|
throw new Error(errorText || "Upload failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ответ должен соответствовать схеме Image.yaml
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (data && data.id) {
|
||||||
|
setAvatarId(data.id);
|
||||||
|
setAvatarUrl(data.image_path ?? null);
|
||||||
setSuccess("Image uploaded!");
|
setSuccess("Image uploaded!");
|
||||||
}
|
}
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error("Upload error:", err);
|
console.error("Upload error:", err);
|
||||||
setError("Upload failed. Check if the file is an image.");
|
setError(err.message || "Failed to upload image");
|
||||||
} finally {
|
} finally {
|
||||||
setUploading(false);
|
setUploading(false);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue