import { type FormEvent, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Screen } from '../components/Shell'; import { BackLink, Notice } from '../components/ui'; import { api, ApiError } from '../lib/api'; import { useAuth } from '../lib/auth'; import { useNav } from '../lib/tree'; import '../styles/editor.css'; // Новая статья: спрашиваем название и раздел, создаём черновик и открываем редактор. export default function NewArticlePage() { const nav = useNav(); const { isEditor } = useAuth(); const navigate = useNavigate(); const [title, setTitle] = useState(''); const [sectionId, setSectionId] = useState(''); const [error, setError] = useState(''); const [busy, setBusy] = useState(false); const section = sectionId || nav.allSections[0]?.id || ''; const submit = async (e: FormEvent) => { e.preventDefault(); setBusy(true); setError(''); try { const r = await api<{ slug: string }>('/edit/articles', { method: 'POST', json: { title, sectionId: section } }); navigate(`/edit/${r.slug}`, { replace: true }); } catch (err) { setError(err instanceof ApiError ? err.message : 'Не удалось создать статью'); setBusy(false); } }; return (
Главная

Новая статья

{!isEditor ? ( Создавать статьи могут редакторы и администраторы. ) : (
{error ? (
{error}
) : null}
)}
); }