|
| 1 | +import { useState } from 'react'; |
| 2 | +import Category from './Category'; |
| 3 | +import FormTitle from './FormTitle'; |
| 4 | +import WriteForm from './WriteForm'; |
| 5 | +import CompleteBtn from './CompleteBtn'; |
| 6 | +import ImageSection from './image-upload/ImageSection'; |
| 7 | +import Tag from '../components/tag/Tag'; |
| 8 | +import { getApi } from '@/app/api/config/appConfig'; |
| 9 | + |
| 10 | +export type FormType = { |
| 11 | + categoryName: string; |
| 12 | + title: string; |
| 13 | + content: string; |
| 14 | + imageUrls: string[]; |
| 15 | + tags: string[]; |
| 16 | +}; |
| 17 | + |
| 18 | +type Props = { |
| 19 | + setIsOpen: (value: boolean) => void; |
| 20 | +}; |
| 21 | + |
| 22 | +function WriteSection({ setIsOpen }: Props) { |
| 23 | + const [formData, setFormData] = useState<FormType>({ |
| 24 | + categoryName: '', |
| 25 | + title: '', |
| 26 | + content: '', |
| 27 | + imageUrls: [], |
| 28 | + tags: [], |
| 29 | + }); |
| 30 | + |
| 31 | + const handleSubmit = async (e: React.FormEvent) => { |
| 32 | + e.preventDefault(); |
| 33 | + |
| 34 | + try { |
| 35 | + const res = await fetch(`${getApi}/posts`, { |
| 36 | + method: 'POST', |
| 37 | + headers: { |
| 38 | + 'Content-Type': 'application/json; charset=UTF-8', |
| 39 | + }, |
| 40 | + body: JSON.stringify({ |
| 41 | + title: formData.title, |
| 42 | + content: formData.content, |
| 43 | + categoryName: formData.categoryName, |
| 44 | + }), |
| 45 | + }); |
| 46 | + |
| 47 | + console.log('▶ 요청 보낸 후 status:', res.status); |
| 48 | + const text = await res.text(); |
| 49 | + console.log('▶ 응답 텍스트:', text); |
| 50 | + if (res.ok) { |
| 51 | + console.log('글작성 성공', formData); |
| 52 | + } |
| 53 | + } catch (err) { |
| 54 | + console.error('글작성 폼 작성 에러', err); |
| 55 | + return; |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + return ( |
| 60 | + <form onSubmit={handleSubmit}> |
| 61 | + <CompleteBtn /> |
| 62 | + <section> |
| 63 | + <FormTitle setFormData={setFormData} /> |
| 64 | + <Category setFormData={setFormData} /> |
| 65 | + <WriteForm setFormData={setFormData} /> |
| 66 | + </section> |
| 67 | + <ImageSection /> |
| 68 | + <section className="mt-8"> |
| 69 | + <Tag use="write" onClick={() => setIsOpen(true)} /> |
| 70 | + </section> |
| 71 | + </form> |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +export default WriteSection; |
0 commit comments