11"use client" ;
2- import React , { useContext , useState , useEffect } from "react" ;
2+ import React , { useContext , useState , useEffect , useRef } from "react" ;
33import dynamic from "next/dynamic" ;
44import { Context } from "@context/store" ;
55import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" ;
@@ -16,6 +16,8 @@ import 'react-quill/dist/quill.snow.css';
1616import ReactQuill , { Quill } from 'react-quill' ;
1717import { useRouter } from "next/navigation" ;
1818function CreateForum ( ) {
19+ let [ selectedTags , setSelectedTags ] = useState ( [ ] )
20+
1921 const { theme , finalUser} = useContext ( Context ) ;
2022 const [ activeSection , setActiveSection ] = useState ( "title" ) ;
2123 let router = useRouter ( )
@@ -28,15 +30,33 @@ function CreateForum() {
2830 toolbar . style . background = 'transparent' ;
2931 }
3032 } , [ ] ) ;
33+ const Tag = ( { name } ) => {
34+
35+ return < div onClick = { ( ) => {
36+ if ( selectedTags . includes ( name ) ) { let arr = selectedTags ;
37+ arr = arr . filter ( ( data ) => data !== name )
38+ setSelectedTags ( [ ...arr ] )
39+ } else { setSelectedTags ( prev => [ ...prev , name ] ) } } } className = { ` ${ selectedTags . includes ( name ) ?"bg-gray-400" :"bg-gray-200" } px-4 py-1 text-gray-700 cursor-pointer hover:bg-[#deecf5] hover:text-[#6089a4] transition-all duration-200` } >
40+ { name }
41+ </ div >
42+ } ;
43+ const Tag1 = ( { name } ) => {
44+
45+ return < div className = "bg-gray-200 px-4 py-1 text-gray-700 cursor-pointer hover:bg-[#deecf5] hover:text-[#6089a4] transition-all duration-200" >
46+ { name }
47+ </ div >
48+ } ;
3149 async function postQuestion ( ) {
50+
3251 let payload = {
3352 title :title ,
3453 content :content ,
3554 authorUsername :finalUser . username ,
3655 authorEmail :finalUser . email ,
3756 authorId :finalUser . _id ,
3857 authorName :finalUser . name ,
39- authorImage :finalUser . image1
58+ authorImage :finalUser . image1 ,
59+ tags :selectedTags
4060 }
4161 await fetch ( '/api/createquestion' , {
4262 method :"POST" ,
@@ -45,6 +65,23 @@ function CreateForum() {
4565 router . push ( "/devopsforum" )
4666 }
4767 const renderSection = ( ) => {
68+ const tags = [
69+ "Docker" ,
70+ "Devops" ,
71+ "Azure" ,
72+ "Ubuntu" ,
73+ "NeedHelp" ,
74+ "Dockerization" ,
75+ "CI/CD" ,
76+ "AWS" ,
77+ "Kubernetes" ,
78+ ]
79+ let tagValue = useRef ( )
80+ function handleAddTag ( ) {
81+ let a = tagValue . current . value
82+ setSelectedTags ( ( prev ) => [ ...prev , a ] )
83+ tagValue . current . value = ""
84+ }
4885 switch ( activeSection ) {
4986 case "title" :
5087 return (
@@ -96,7 +133,7 @@ function CreateForum() {
96133 </ div >
97134 < div
98135 className = "w-20 rounded-xl h-12 flex justify-center items-center text-white bg-[#6089a4]"
99- onClick = { ( ) => setActiveSection ( "member " ) }
136+ onClick = { ( ) => setActiveSection ( "tags " ) }
100137 >
101138 Next
102139 </ div >
@@ -143,7 +180,7 @@ function CreateForum() {
143180 < div className = "flex justify-between mt-4" >
144181 < div
145182 className = "w-20 rounded-xl h-12 flex justify-center items-center text-white bg-[#6089a4] cursor-pointer"
146- onClick = { ( ) => setActiveSection ( "content " ) }
183+ onClick = { ( ) => setActiveSection ( "tags " ) }
147184 >
148185 Prev
149186 </ div >
@@ -187,7 +224,45 @@ function CreateForum() {
187224 </ div >
188225 </ div >
189226 ) ;
190- default :
227+ case "tags" :return (
228+ < div className = "my-10 w-[80%] m-auto" >
229+ < div className = "text-xl my-5 px-3 text-center" >
230+ Select Your Tags
231+ </ div >
232+ < div className = "bg-white shadow-md rounded-xl p-4" >
233+ < div className = "mb-4 " >
234+ < div className = "flex justify-center flex-wrap mt-5 gap-4" >
235+ { tags . map ( ( tag , index ) => (
236+ < Tag key = { index } name = { tag } />
237+ ) ) }
238+ </ div >
239+ { selectedTags . length > 0 && < h1 className = "text-xl text-center mt-[20px] " > Selected Tags</ h1 > }
240+ < div className = "w-[100%] mt-[25px] mb-[20px] justify-center flex gap-5" > < input placeholder = "Add Your Tag" className = "border-b-gray-500 border-b-[1px]" ref = { tagValue } /> < button onClick = { handleAddTag } > Add Tag</ button > </ div >
241+ < div className = "flex flex-wrap mt-5 gap-4 justify-center" >
242+ { selectedTags . map ( ( tag , index ) => (
243+ < Tag1 key = { index } name = { tag } />
244+ ) ) }
245+ </ div >
246+ </ div >
247+
248+ </ div >
249+ < div className = "flex justify-between mt-4" >
250+ < div
251+ className = "w-20 rounded-xl h-12 flex justify-center items-center text-white bg-[#6089a4] cursor-pointer"
252+ onClick = { ( ) => setActiveSection ( "content" ) }
253+ >
254+ Prev
255+ </ div >
256+ < div
257+ className = "w-20 rounded-xl h-12 flex justify-center items-center text-white bg-[#6089a4] cursor-pointer"
258+ onClick = { ( ) => setActiveSection ( "member" ) }
259+ >
260+ Next
261+ </ div >
262+ </ div >
263+ </ div >
264+ )
265+ default :
191266 return null ;
192267 }
193268 } ;
0 commit comments