eb975e5301
Merge branch 'main' into feature/140 #140 Merge remote-tracking branch 'origin/main' into feature/140 #140 #140 #140 #140 #140 Merge remote-tracking branch 'origin/main' into feature/140 #140 #140 #140 #140 #140 #140 #140 #140 #140 #140 #140 Merge remote-tracking branch 'origin/main' into feature/140 Merge remote-tracking branch 'origin/main' into feature/140 #140 ぼちぼち Merge remote-tracking branch 'origin/main' into feature/140 #140 #140 #140 Co-authored-by: miteruzo <miteruzo@naver.com> Reviewed-on: #256
109 lines
2.9 KiB
TypeScript
109 lines
2.9 KiB
TypeScript
import { useQueryClient } from '@tanstack/react-query'
|
|
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 { SITE_TITLE } from '@/config'
|
|
import { apiGet, apiPut } from '@/lib/api'
|
|
import { wikiKeys } from '@/lib/queryKeys'
|
|
import Forbidden from '@/pages/Forbidden'
|
|
|
|
import 'react-markdown-editor-lite/lib/index.css'
|
|
|
|
import type { FC } from 'react'
|
|
|
|
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 <Forbidden/>
|
|
|
|
const { id } = useParams ()
|
|
|
|
const navigate = useNavigate ()
|
|
|
|
const qc = useQueryClient ()
|
|
|
|
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 apiPut (`/wiki/${ id }`, formData,
|
|
{ headers: { 'Content-Type': 'multipart/form-data' } })
|
|
qc.setQueryData (wikiKeys.show (title, { }),
|
|
(prev: WikiPage) => ({ ...prev, title, body }))
|
|
qc.invalidateQueries ({ queryKey: wikiKeys.root })
|
|
toast ({ title: '投稿成功!' })
|
|
navigate (`/wiki/${ title }`)
|
|
}
|
|
catch
|
|
{
|
|
toast ({ title: '投稿失敗', description: '入力を確認してください。' })
|
|
}
|
|
}
|
|
|
|
useEffect (() => {
|
|
void (async () => {
|
|
setLoading (true)
|
|
const data = await apiGet<WikiPage> (`/wiki/${ id }`)
|
|
setTitle (data.title)
|
|
setBody (data.body)
|
|
setLoading (false)
|
|
}) ()
|
|
}, [id])
|
|
|
|
return (
|
|
<MainArea>
|
|
<Helmet>
|
|
<title>{`Wiki ページを編輯 | ${ SITE_TITLE }`}</title>
|
|
</Helmet>
|
|
<div className="max-w-xl mx-auto p-4 space-y-4">
|
|
<h1 className="text-2xl font-bold mb-2">Wiki ページを編輯</h1>
|
|
|
|
{loading ? 'Loading...' : (
|
|
<>
|
|
{/* タイトル */}
|
|
{/* TODO: タグ補完 */}
|
|
<div>
|
|
<label className="block font-semibold mb-1">タイトル</label>
|
|
<input type="text"
|
|
value={title}
|
|
onChange={e => setTitle (e.target.value)}
|
|
className="w-full border p-2 rounded"/>
|
|
</div>
|
|
|
|
{/* 本文 */}
|
|
<div>
|
|
<label className="block font-semibold mb-1">本文</label>
|
|
<MdEditor value={body}
|
|
style={{ height: '500px' }}
|
|
renderHTML={text => mdParser.render (text)}
|
|
onChange={({ text }) => setBody (text)}/>
|
|
</div>
|
|
|
|
{/* 送信 */}
|
|
<button onClick={handleSubmit}
|
|
className="px-4 py-2 bg-blue-600 text-white rounded disabled:bg-gray-400">
|
|
編輯
|
|
</button>
|
|
</>)}
|
|
</div>
|
|
</MainArea>)
|
|
}) satisfies FC<Props>
|