import React, { useEffect, useState } from 'react' import axios from 'axios' import { API_BASE_URL } from '@/config' import { Button } from '@/components/ui/button' import type { Post, Tag } from '@/types' type Props = { post: Post onSave: (newPost: Post) => void } export default ({ post, onSave }: Props) => { const [title, setTitle] = useState (post.title) const [tags, setTags] = useState ([]) const [tagIds, setTagIds] = useState (post.tags.map (t => t.id)) const handleSubmit = () => { void (axios.put (`${ API_BASE_URL }/posts/${ post.id }`, { title, tags: JSON.stringify (tagIds) }, { headers: { 'Content-Type': 'multipart/form-data', 'X-Transfer-Code': localStorage.getItem ('user_code') || '' } } ) .then (res => { const newPost: Post = res.data onSave ({ ...post, title: newPost.title, tags: newPost.tags } as Post) })) } useEffect (() => { void (axios.get ('/api/tags') .then (res => setTags (res.data)) .catch (() => toast ({ title: 'タグ一覧の取得失敗' }))) }, []) return (
{/* タイトル */}
setTitle (e.target.value)} />
{/* タグ */}
{/* 送信 */}
) }