ぼざクリ タグ広場 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.
 
 
 
 
 
 

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