import React, { useEffect, useState } from 'react' import { Link, useLocation, useParams, useNavigate } from 'react-router-dom' import axios from 'axios' import { API_BASE_URL, SITE_TITLE } from '../config' import NicoViewer from '../components/NicoViewer' import { Button } from '@/components/ui/button' import { toast } from '@/components/ui/use-toast' import { cn } from '@/lib/utils' type Tag = { id: number name: string category: string } type Post = { id: number url: string title: string thumbnail: string tags: Tag[] viewed: boolean } type Props = { posts: Post[] setPosts: (posts: Post[]) => void } const PostNewPage = () => { const location = useLocation () const navigate = useNavigate () const [title, setTitle] = useState ('') const [titleAutoFlg, setTitleAutoFlg] = useState (true) const [url, setURL] = useState ('') const [thumbnailFile, setThumbnailFile] = useState (null) const [thumbnailPreview, setThumbnailPreview] = useState ('') const [thumbnailAutoFlg, setThumbnailAutoFlg] = useState (true) const [tags, setTags] = useState ([]) const [tagIds, setTagIds] = useState ([]) const handleSubmit = () => { const formData = new FormData () formData.append ('title', title || null) formData.append ('url', url) formData.append ('tags', JSON.stringify (tagIds)) formData.append ('thumbnail', thumbnailAutoFlg ? null : thumbnailFile) void (axios.post (`${ API_BASE_URL }/posts`, formData, { headers: { 'Content-Type': 'multipart/form-data', 'X-Transfer-Code': localStorage.getItem ('user_code') || '' } }) .then (() => { toast ({ title: '投稿成功!' }) navigate ('/posts') }) .catch (e => toast ({ title: '投稿失敗', description: '入力を確認してください。' }))) } document.title = `広場に投稿を追加 | ${ SITE_TITLE }` useEffect (() => { void (axios.get ('/api/tags') .then (res => setTags (res.data)) .catch (() => toast ({ title: 'タグ一覧の取得失敗' }))) }, []) return (

広場に投稿を追加する

{/* URL */}
setURL (e.target.value)} className="w-full border p-2 rounded" />
{/* タイトル */}
setTitle (e.target.value)} disabled={titleAutoFlg} />
{/* サムネール */}
{thumbnailAutoFlg ? (

URL から自動取得されます。

) : ( <> { const file = e.target.files?.[0] if (file) { setThumbnailFile (file) setThumbnailPreview (URL.createObjectURL (file)) } }} /> {thumbnailPreview && ( preview)} )}
{/* タグ */}
{/* 送信 */}
) } export default PostNewPage