1- import { createPortal } from ' react-dom' ;
2- import { CreateBoardModalProps } from ' ../types' ;
3- import { useState } from ' react' ;
4- import ' ../scss/modal.scss' ;
1+ import { createPortal } from " react-dom" ;
2+ import { CreateBoardModalProps } from " ../types" ;
3+ import { useState } from " react" ;
4+ import " ../scss/modal.scss" ;
55
66const CreateBoardModal = ( {
77 setCreatingBoard,
88 setCurrentBoard,
99 user,
10+ boardList,
11+ setBoardList,
12+ handleBoardSelect,
13+ selectedBoard,
1014} : CreateBoardModalProps ) => {
11- const [ boardName , setBoardName ] = useState < string > ( '' ) ;
15+ const [ boardName , setBoardName ] = useState < string > ( "" ) ;
1216
1317 const handleInputChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
1418 const inputValue : string = e . target . value ;
@@ -22,49 +26,64 @@ const CreateBoardModal = ({
2226 boardName : boardName ,
2327 userId : user . id ,
2428 } ;
25- const response : Response = await fetch ( ' /boards/create' , {
26- method : ' POST' ,
29+ const response : Response = await fetch ( " /boards/create" , {
30+ method : " POST" ,
2731 headers : {
28- ' Content-type' : ' application/json; charset=UTF-8' ,
32+ " Content-type" : " application/json; charset=UTF-8" ,
2933 } ,
3034 body : JSON . stringify ( body ) ,
3135 } ) ;
32- // receive username from backend
36+ // receive usernam and id from backend
3337 // if request success, save username to state and route to dashboard
3438 if ( response . status === 200 ) {
3539 const responseData = await response . json ( ) ;
40+ console . log ( "Board created: " , responseData ) ;
41+ const newBoardListItem = (
42+ < button
43+ className = { `board-selector ${
44+ selectedBoard === responseData . _id ? "selected" : ""
45+ } `}
46+ onClick = { handleBoardSelect }
47+ name = { responseData . name }
48+ id = { responseData . _id }
49+ key = { responseData . _id }
50+ >
51+ { responseData . name }
52+ </ button >
53+ ) ;
54+ setBoardList ( [ ...boardList , newBoardListItem ] ) ;
55+ setCurrentBoard ( { name : responseData . name , id : responseData . _id } ) ;
3656 setCreatingBoard ( false ) ;
37- console . log ( 'Board created: ' , responseData ) ;
3857 } else {
39- console . log ( ' Failed To create board.' ) ;
58+ console . log ( " Failed To create board." ) ;
4059 }
4160 } ;
4261
43- const isButtonDisabled : boolean = boardName === '' ; //checking if boardName is empty? using trim in handle input
62+ const isButtonDisabled : boolean = boardName === "" ; //checking if boardName is empty? using trim in handle input
4463
4564 return createPortal (
46- < div className = ' modal-overlay' >
47- < div className = ' modal' >
48- < form className = ' modal-form' onSubmit = { handleFormSubmit } >
49- < h2 className = ' modal-title' > New Board</ h2 >
65+ < div className = " modal-overlay" >
66+ < div className = " modal" >
67+ < form className = " modal-form" onSubmit = { handleFormSubmit } >
68+ < h2 className = " modal-title" > New Board</ h2 >
5069 < input
51- className = ' modal-input'
52- name = ' boardname'
53- type = ' text'
54- placeholder = ' Enter Board Name'
70+ className = " modal-input"
71+ name = " boardname"
72+ type = " text"
73+ placeholder = " Enter Board Name"
5574 onChange = { handleInputChange }
5675 required
5776 />
58- < div className = ' modal-btns' >
77+ < div className = " modal-btns" >
5978 < button
60- className = ' modal-submit'
61- type = ' submit'
79+ className = " modal-submit"
80+ type = " submit"
6281 disabled = { isButtonDisabled }
6382 >
6483 Save
6584 </ button >
6685 < button
67- className = ' modal-cancel'
86+ className = " modal-cancel"
6887 onClick = { ( ) => {
6988 setCreatingBoard ( false ) ;
7089 } }
@@ -75,7 +94,7 @@ const CreateBoardModal = ({
7594 </ form >
7695 </ div >
7796 </ div > ,
78- document . getElementById ( ' portal' ) as Element
97+ document . getElementById ( " portal" ) as Element
7998 ) ;
8099} ;
81100
0 commit comments