import axios from 'axios' import MarkdownIt from 'markdown-it' import { useEffect, useState } from 'react' import { Helmet } from 'react-helmet-async' import MdEditor from 'react-markdown-editor-lite' import { useParams, useNavigate } from 'react-router-dom' import MainArea from '@/components/layout/MainArea' import { toast } from '@/components/ui/use-toast' import { API_BASE_URL, SITE_TITLE } from '@/config' import Forbidden from '@/pages/Forbidden' import 'react-markdown-editor-lite/lib/index.css' import type { User, WikiPage } from '@/types' const mdParser = new MarkdownIt type Props = { user: User | null } export default ({ user }: Props) => { if (!(['admin', 'member'].some (r => user?.role === r))) return const { id } = useParams () const navigate = useNavigate () const [body, setBody] = useState ('') const [loading, setLoading] = useState (true) const [title, setTitle] = useState ('') const handleSubmit = async () => { const formData = new FormData () formData.append ('title', title) formData.append ('body', body) try { await axios.put (`${ API_BASE_URL }/wiki/${ id }`, formData, { headers: { 'Content-Type': 'multipart/form-data', 'X-Transfer-Code': localStorage.getItem ('user_code') ?? '' } }) toast ({ title: '投稿成功!' }) navigate (`/wiki/${ title }`) } catch { toast ({ title: '投稿失敗', description: '入力を確認してください。' }) } } useEffect (() => { void (async () => { setLoading (true) const res = await axios.get (`${ API_BASE_URL }/wiki/${ id }`) const data = res.data as WikiPage setTitle (data.title) setBody (data.body) setLoading (false) }) () }, [id]) return ( {`Wiki ページを編輯 | ${ SITE_TITLE }`} Wiki ページを編輯 {loading ? 'Loading...' : ( <> {/* タイトル */} {/* TODO: タグ補完 */} タイトル setTitle (e.target.value)} className="w-full border p-2 rounded" /> {/* 本文 */} 本文 mdParser.render (text)} onChange={({ text }) => setBody (text)} /> {/* 送信 */} 追加 >)} ) }