import { useState, useEffect } from 'react'; import { useParams, useNavigate } from 'react-router-dom'; import { useSelector } from 'react-redux'; import { useGetArticleQuery, useUpdateArticleMutation, useDeleteArticleMutation } from '/src/api/apiArticle'; import { selectRegions } from '../store/slice/regionSlice'; import { TextEditor } from '../components/Editors/TextEditor'; import { LoadingComponent } from '../components/Placeholders/LoadingComponent'; import { ErrorComponent } from '../components/Placeholders/ErrorComponent'; import { NotFindElement } from '../components/Placeholders/NotFindElement'; import { EditElementForm } from '../components/Forms/EditElementForm'; import Modal from '../components/Modals/Modal'; export function EditArticlePage() { const { id } = useParams(); const navigate = useNavigate(); const navigateBack = () => navigate(`/article`); const regions = useSelector(selectRegions); const { data: item, isFetching, error } = useGetArticleQuery({ articleId: id }); const [updateArticle] = useUpdateArticleMutation(); const [deleteArticle] = useDeleteArticleMutation(); const [isModalSuccess, setModalSuccess] = useState(false); const [errors, setErrors] = useState({ name: '', alias: '', regionId: '' }); const [form, setForm] = useState({ name: '', active: false, regionId: '', alias: '', previewPicture: '', doctors: '', services: '', }); const [anons, setAnons] = useState(''); const [content, setContent] = useState(''); useEffect(() => { if (!item) return; setForm({ name: item.name ?? '', active: Boolean(item.active), regionId: item.regionId ?? '', alias: item.alias ?? '', previewPicture: item.previewPicture ?? '', doctors: item.doctors == null ? '' : JSON.stringify(item.doctors, null, 2), services: item.services == null ? '' : JSON.stringify(item.services, null, 2), }); setAnons(item.anons ?? ''); setContent(item.content ?? ''); }, [item]); const handleChange = (key) => (e) => { const value = e.target.type === 'checkbox' ? e.target.checked : e.target.value; setForm((f) => ({ ...f, [key]: value })); if (key === 'name' || key === 'alias' || key === 'regionId') setErrors((err) => ({ ...err, [key]: '' })); }; const handleDelete = async () => { try { await deleteArticle({ articleId: id }).unwrap(); setModalSuccess(true); window.setTimeout(() => navigateBack(), 2000); } catch (err) { console.error('Ошибка при удалении:', err); } }; const handleSave = async () => { const newErrors = { name: '', alias: '', regionId: '' }; let hasError = false; if (!String(form.name ?? '').trim()) { newErrors.name = 'Название не может быть пустым'; hasError = true; } if (!String(form.alias ?? '').trim()) { newErrors.alias = 'Alias не может быть пустым'; hasError = true; } if (form.regionId === '' || form.regionId == null) { newErrors.regionId = 'Укажите регион'; hasError = true; } if (hasError) { setErrors(newErrors); window.alert('Пожалуйста исправьте ошибки в форме.'); return; } const data = { anons, content }; try { data.name = form.name === '' ? null : form.name; data.active = Boolean(form.active); data.regionId = form.regionId === '' ? null : Number(form.regionId); data.alias = form.alias === '' ? null : form.alias; data.previewPicture = form.previewPicture === '' ? null : form.previewPicture; data.doctors = !form.doctors || !String(form.doctors).trim() ? null : JSON.parse(form.doctors); data.services = !form.services || !String(form.services).trim() ? null : JSON.parse(form.services); } catch (e) { window.alert('Пожалуйста исправьте ошибки в форме.'); return; } try { await updateArticle({ articleId: id, data }).unwrap(); setModalSuccess(true); window.setTimeout(() => window.location.reload(), 2000); } catch (err) { console.error('Ошибка сохранения:', err); } }; if (isFetching) return ; if (error) return ; if (!item) return ; return (
{errors.name && {errors.name}}
{errors.regionId && {errors.regionId}}
{errors.alias && {errors.alias}}