11import { useState } from "react" ;
2+ import TagCard from "./TagCard" ;
3+ import type { Tag } from "../types/lp" ;
4+ import usePostLp from "../hooks/mutations/usePostLp" ;
25
36interface LpModalProps {
47 isOpen : boolean ;
@@ -9,6 +12,31 @@ interface LpModalProps {
912const LpModal = ( { isOpen, onClose, toggle } : LpModalProps ) => {
1013 const [ name , setName ] = useState ( "" ) ;
1114 const [ content , setContent ] = useState ( "" ) ;
15+ const [ thumnailFile , setThumnailFile ] = useState < File | null > ( null ) ;
16+ const [ tagName , setTagName ] = useState < string > ( "" ) ;
17+ const [ tags , setTags ] = useState < Tag [ ] > ( [ ] ) ;
18+
19+ const { mutate } = usePostLp ( ) ;
20+
21+ const handlePostLp = ( ) => {
22+ mutate ( {
23+ title : name ,
24+ content : content ,
25+ thumbnail : thumnailFile ?. name , // 파일 경로를 파일 이름으로 대체
26+ tags : tags . map ( ( tag ) => tag . name ) ,
27+ published : true ,
28+ } ) ;
29+ onClose ( ) ;
30+ } ;
31+
32+ const handleFileChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
33+ const file = e . target . files ?. [ 0 ] || null ;
34+ setThumnailFile ( file ) ;
35+ } ;
36+
37+ const deleteTag = ( idToDelete : number ) => {
38+ setTags ( ( prev ) => prev . filter ( ( tag ) => tag . id !== idToDelete ) ) ;
39+ } ;
1240
1341 return (
1442 < >
@@ -19,7 +47,7 @@ const LpModal = ({ isOpen, onClose, toggle }: LpModalProps) => {
1947 onClick = { onClose }
2048 > </ div >
2149 < div
22- className = { `fixed inset-x-1/4 inset-y-1/6 rounded-2xl flex flex-col items-center space-y-4 box-border px-4 py-2 bg-gray-50 shadow-2xl z-40 ${
50+ className = { `fixed inset-x-1/4 inset-y-24 rounded-2xl flex flex-col items-center space-y-4 box-border px-4 py-2 bg-gray-50 shadow-2xl z-40 ${
2351 isOpen ? "opacity-100" : "opacity-0 pointer-events-none"
2452 } `}
2553 >
@@ -36,24 +64,60 @@ const LpModal = ({ isOpen, onClose, toggle }: LpModalProps) => {
3664 htmlFor = "upload-lp-image"
3765 className = "w-40 h-40 rounded-full bg-gray-500"
3866 > </ label >
39- < input type = "file" className = "hidden" id = "upload-lp-image" />
67+ < input
68+ type = "file"
69+ className = "hidden"
70+ id = "upload-lp-image"
71+ onChange = { handleFileChange }
72+ accept = "image/*"
73+ />
4074 < input
4175 type = "text"
4276 onChange = { ( e ) => setName ( e . target . value ) }
4377 value = { name }
4478 placeholder = "Lp Name"
45- className = "pl-4 py-2 w-full border border-gray-950 rounded-xl "
79+ className = "pl-4 py-2 w-full border border-gray-950 rounded-lg "
4680 />
4781 < input
4882 type = "text"
4983 value = { content }
5084 onChange = { ( e ) => setContent ( e . target . value ) }
5185 placeholder = "Lp Content"
52- className = "pl-4 py-2 w-full border border-gray-950 rounded-xl "
86+ className = "pl-4 py-2 w-full border border-gray-950 rounded-lg "
5387 />
88+ < div className = "flex w-full space-x-2" >
89+ < input
90+ type = "text"
91+ value = { tagName }
92+ onChange = { ( e ) => setTagName ( e . target . value ) }
93+ placeholder = "Lp Tag"
94+ className = "pl-4 py-2 w-full border border-gray-950 rounded-lg"
95+ />
96+ < button
97+ disabled = { tagName . length === 0 }
98+ className = "cursor-pointer py-2 px-4 bg-gray-950 text-gray-50 rounded-lg disabled:bg-gray-400 disabled:cursor-not-allowed"
99+ onClick = { ( ) => {
100+ setTags ( ( prev ) => [ ...prev , { name : tagName , id : Date . now ( ) } ] ) ;
101+ setTagName ( "" ) ;
102+ } }
103+ >
104+ Add
105+ </ button >
106+ </ div >
107+ < div className = "flex w-full space-x-2 overflow-x-scroll" >
108+ { tags . map ( ( tag ) => (
109+ < TagCard
110+ name = { tag . name }
111+ id = { tag . id }
112+ onDelete = { deleteTag }
113+ key = { tag . id }
114+ />
115+ ) ) }
116+ </ div >
54117 < button
55118 disabled = { name . length === 0 || content . length === 0 }
56- className = "cursor-pointer py-2 w-full bg-gray-950 text-gray-50 rounded-xl disabled:bg-gray-400 disabled:cursor-not-allowed"
119+ className = "cursor-pointer py-2 w-full bg-gray-950 text-gray-50 rounded-lg disabled:bg-gray-400 disabled:cursor-not-allowed"
120+ onClick = { handlePostLp }
57121 >
58122 Add Lp
59123 </ button >
0 commit comments