ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

104 lines
2.8 KiB

  1. import axios from 'axios'
  2. import MarkdownIt from 'markdown-it'
  3. import { useEffect, useState } from 'react'
  4. import { Helmet } from 'react-helmet-async'
  5. import MdEditor from 'react-markdown-editor-lite'
  6. import { useParams, useNavigate } from 'react-router-dom'
  7. import MainArea from '@/components/layout/MainArea'
  8. import { toast } from '@/components/ui/use-toast'
  9. import { API_BASE_URL, SITE_TITLE } from '@/config'
  10. import Forbidden from '@/pages/Forbidden'
  11. import 'react-markdown-editor-lite/lib/index.css'
  12. import type { FC } from 'react'
  13. import type { User, WikiPage } from '@/types'
  14. const mdParser = new MarkdownIt
  15. type Props = { user: User | null }
  16. export default (({ user }: Props) => {
  17. if (!(['admin', 'member'].some (r => user?.role === r)))
  18. return <Forbidden/>
  19. const { id } = useParams ()
  20. const navigate = useNavigate ()
  21. const [body, setBody] = useState ('')
  22. const [loading, setLoading] = useState (true)
  23. const [title, setTitle] = useState ('')
  24. const handleSubmit = async () => {
  25. const formData = new FormData ()
  26. formData.append ('title', title)
  27. formData.append ('body', body)
  28. try
  29. {
  30. await axios.put (`${ API_BASE_URL }/wiki/${ id }`, formData, { headers: {
  31. 'Content-Type': 'multipart/form-data',
  32. 'X-Transfer-Code': localStorage.getItem ('user_code') ?? '' } })
  33. toast ({ title: '投稿成功!' })
  34. navigate (`/wiki/${ title }`)
  35. }
  36. catch
  37. {
  38. toast ({ title: '投稿失敗', description: '入力を確認してください。' })
  39. }
  40. }
  41. useEffect (() => {
  42. void (async () => {
  43. setLoading (true)
  44. const res = await axios.get (`${ API_BASE_URL }/wiki/${ id }`)
  45. const data = res.data as WikiPage
  46. setTitle (data.title)
  47. setBody (data.body)
  48. setLoading (false)
  49. }) ()
  50. }, [id])
  51. return (
  52. <MainArea>
  53. <Helmet>
  54. <title>{`Wiki ページを編輯 | ${ SITE_TITLE }`}</title>
  55. </Helmet>
  56. <div className="max-w-xl mx-auto p-4 space-y-4">
  57. <h1 className="text-2xl font-bold mb-2">Wiki ページを編輯</h1>
  58. {loading ? 'Loading...' : (
  59. <>
  60. {/* タイトル */}
  61. {/* TODO: タグ補完 */}
  62. <div>
  63. <label className="block font-semibold mb-1">タイトル</label>
  64. <input type="text"
  65. value={title}
  66. onChange={e => setTitle (e.target.value)}
  67. className="w-full border p-2 rounded"/>
  68. </div>
  69. {/* 本文 */}
  70. <div>
  71. <label className="block font-semibold mb-1">本文</label>
  72. <MdEditor value={body}
  73. style={{ height: '500px' }}
  74. renderHTML={text => mdParser.render (text)}
  75. onChange={({ text }) => setBody (text)}/>
  76. </div>
  77. {/* 送信 */}
  78. <button onClick={handleSubmit}
  79. className="px-4 py-2 bg-blue-600 text-white rounded disabled:bg-gray-400">
  80. 編輯
  81. </button>
  82. </>)}
  83. </div>
  84. </MainArea>)
  85. }) satisfies FC<Props>