fix bug editor block
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="stylesheet" href="https://cdn.quilljs.com/1.3.6/quill.snow.css" />
|
||||
<title>Admin Frontend</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import React, { useState, useEffect, useRef, useCallback } from 'react';
|
||||
import 'quill/dist/quill.snow.css';
|
||||
import '../App.css';
|
||||
import DOMPurify from 'dompurify';
|
||||
|
||||
@@ -14,7 +15,7 @@ function Editor() {
|
||||
const [editContent, setEditContent] = useState('');
|
||||
const [editPreviewContent, setEditPreviewContent] = useState('');
|
||||
const [showConfirmModal, setShowConfirmModal] = useState(false);
|
||||
const [csrfToken, setCsrfToken] = useState('');
|
||||
//const [csrfToken, setCsrfToken] = useState('');
|
||||
const [notify, setNotify] = useState({ message: '', type: '', show: false });
|
||||
|
||||
const editorRef = useRef(null);
|
||||
@@ -30,14 +31,14 @@ function Editor() {
|
||||
}, []);
|
||||
|
||||
// Fetch CSRF token
|
||||
useEffect(() => {
|
||||
fetch('/moders/csrf-token')
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
setCsrfToken(data.csrfToken);
|
||||
})
|
||||
.catch(() => {});
|
||||
}, []);
|
||||
//useEffect(() => {
|
||||
// fetch('/moders/csrf-token')
|
||||
// .then(res => res.json())
|
||||
// .then(data => {
|
||||
// setCsrfToken(data.csrfToken);
|
||||
//})
|
||||
//.catch(() => {});
|
||||
//}, []);
|
||||
|
||||
// Check session on mount
|
||||
useEffect(() => {
|
||||
@@ -53,13 +54,9 @@ function Editor() {
|
||||
.catch(() => setIsLoggedIn(false));
|
||||
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
const authenticatedFetch = useCallback((url, options = {}) => {
|
||||
if (!options.headers) options.headers = {};
|
||||
if (options.method && ['POST', 'PUT', 'DELETE'].includes(options.method.toUpperCase())) {
|
||||
options.headers['x-csrf-token'] = csrfToken;
|
||||
}
|
||||
return fetch(url, options);
|
||||
}, [csrfToken]);
|
||||
const authenticatedFetch = useCallback((url, options = {}) => {
|
||||
return fetch(url, options);
|
||||
}, []);
|
||||
|
||||
const loadBlocks = useCallback(async (tab) => {
|
||||
try {
|
||||
@@ -140,32 +137,41 @@ function Editor() {
|
||||
};
|
||||
|
||||
const handleSaveBlock = async (e) => {
|
||||
e.preventDefault();
|
||||
const content = DOMPurify.sanitize(editContent.trim());
|
||||
if (!content || content === '<p><br></p>') return;
|
||||
e.preventDefault();
|
||||
const content = DOMPurify.sanitize(editContent.trim());
|
||||
if (!content || content === '<p><br></p>') return;
|
||||
|
||||
try {
|
||||
if (editingBlockId) {
|
||||
await authenticatedFetch(`/moders/content/${activeTab}/${editingBlockId}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ type: 'text', content })
|
||||
});
|
||||
showNotify('Блок обновлён', 'success');
|
||||
} else {
|
||||
await authenticatedFetch(`/moders/content/${activeTab}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ type: 'text', content })
|
||||
});
|
||||
showNotify('Блок добавлен', 'success');
|
||||
}
|
||||
const url = editingBlockId
|
||||
? `/moders/content/${activeTab}/${editingBlockId}`
|
||||
: `/moders/content/${activeTab}`;
|
||||
|
||||
const method = editingBlockId ? 'PUT' : 'POST';
|
||||
|
||||
console.log('➡️ Отправка:', method, url, { type: 'text', content });
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ type: 'text', content })
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
console.log('⬅️ Ответ сервера:', response.status, data);
|
||||
|
||||
if (response.ok) {
|
||||
showNotify(editingBlockId ? 'Блок обновлён' : 'Блок добавлен', 'success');
|
||||
setShowEditModal(false);
|
||||
loadBlocks(activeTab);
|
||||
} catch (e) {
|
||||
showNotify('Ошибка сохранения', 'error');
|
||||
} else {
|
||||
showNotify(`Ошибка: ${data.message || response.status}`, 'error');
|
||||
}
|
||||
};
|
||||
} catch (e) {
|
||||
console.error('❌ Ошибка при сохранении:', e);
|
||||
showNotify('Ошибка сохранения', 'error');
|
||||
}
|
||||
};
|
||||
|
||||
const handleTryCloseEditModal = () => {
|
||||
if (editContent !== originalContentRef.current) {
|
||||
|
||||
@@ -3,15 +3,15 @@ import '../App.css';
|
||||
|
||||
// CSRF token management
|
||||
let csrfToken = null;
|
||||
async function fetchCsrfToken() {
|
||||
try {
|
||||
const res = await fetch('/moders/csrf-token', { credentials: 'include' });
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
csrfToken = data.csrfToken;
|
||||
}
|
||||
} catch (e) { csrfToken = null; }
|
||||
}
|
||||
//async function fetchCsrfToken() {
|
||||
// try {
|
||||
// const res = await fetch('/moders/csrf-token', { credentials: 'include' });
|
||||
// if (res.ok) {
|
||||
// const data = await res.json();
|
||||
// csrfToken = data.csrfToken;
|
||||
// }
|
||||
//} catch (e) { csrfToken = null; }
|
||||
//}
|
||||
|
||||
async function apiFetch(url, options = {}) {
|
||||
if (!options.headers) options.headers = {};
|
||||
|
||||
Reference in New Issue
Block a user