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

90 lines
2.5 KiB

  1. import axios from 'axios'
  2. import MarkdownIt from 'markdown-it'
  3. import { useState } from 'react'
  4. import { Helmet } from 'react-helmet-async'
  5. import MdEditor from 'react-markdown-editor-lite'
  6. import { useLocation, 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 location = useLocation ()
  19. const navigate = useNavigate ()
  20. const query = new URLSearchParams (location.search)
  21. const titleQuery = query.get ('title') ?? ''
  22. const [title, setTitle] = useState (titleQuery)
  23. const [body, setBody] = useState ('')
  24. const handleSubmit = async () => {
  25. const formData = new FormData
  26. formData.append ('title', title)
  27. formData.append ('body', body)
  28. try
  29. {
  30. const res = await axios.post (`${ API_BASE_URL }/wiki`, formData, { headers: {
  31. 'Content-Type': 'multipart/form-data',
  32. 'X-Transfer-Code': localStorage.getItem ('user_code') || '' } })
  33. const data = res.data as WikiPage
  34. toast ({ title: '投稿成功!' })
  35. navigate (`/wiki/${ data.title }`)
  36. }
  37. catch
  38. {
  39. toast ({ title: '投稿失敗', description: '入力を確認してください。' })
  40. }
  41. }
  42. return (
  43. <MainArea>
  44. <Helmet>
  45. <title>{`新規 Wiki ページ | ${ SITE_TITLE }`}</title>
  46. </Helmet>
  47. <div className="max-w-xl mx-auto p-4 space-y-4">
  48. <h1 className="text-2xl font-bold mb-2">新規 Wiki ページ</h1>
  49. {/* タイトル */}
  50. {/* TODO: タグ補完 */}
  51. <div>
  52. <label className="block font-semibold mb-1">タイトル</label>
  53. <input type="text"
  54. value={title}
  55. onChange={e => setTitle (e.target.value)}
  56. className="w-full border p-2 rounded"/>
  57. </div>
  58. {/* 本文 */}
  59. <div>
  60. <label className="block font-semibold mb-1">本文</label>
  61. <MdEditor value={body}
  62. style={{ height: '500px' }}
  63. renderHTML={text => mdParser.render (text)}
  64. onChange={({ text }) => setBody (text)}/>
  65. </div>
  66. {/* 送信 */}
  67. <button onClick={handleSubmit}
  68. className="px-4 py-2 bg-blue-600 text-white rounded disabled:bg-gray-400">
  69. 追加
  70. </button>
  71. </div>
  72. </MainArea>)
  73. }