11import React , { useState , useEffect } from 'react' ;
2- import { Link } from 'react-router-dom' ;
32import { useNavigate } from 'react-router' ;
43import { useSelector , useDispatch } from 'react-redux' ;
5- import { setForm , setSelected } from '../../store/actions' ;
4+ import { setSelected } from '../../store/actions' ;
65import { useCreateCourseMutation } from '../../generated/graphql' ;
76import { ReactComponent as CloseBtn } from '../../Assets/window-close-regular.svg' ;
87import Course from '../../Types/course' ;
@@ -14,7 +13,6 @@ const lottieStyle = 'invisible md:visible w-0 h-0 md:w-4/12 md:h-auto';
1413
1514const AddForm : React . FC = ( ) => {
1615 const [ title , setTitle ] = useState ( '' ) ;
17- const open = useSelector ( ( state : { form : boolean } ) => state . form ) ;
1816 const selected = useSelector (
1917 ( state : { selectedCourse : Course | null } ) => state . selectedCourse
2018 ) ;
@@ -32,8 +30,8 @@ const AddForm: React.FC = () => {
3230
3331 useEffect ( ( ) => {
3432 // display new Course in ClassView
35- if ( selected && ! open ) navigate ( `/homepage/classes/${ selected . id } ` ) ;
36- } , [ selected , open , navigate ] ) ;
33+ if ( selected ) navigate ( `/homepage/classes/${ selected . id } ` ) ;
34+ } , [ selected , navigate ] ) ;
3735
3836 async function addCourse ( name : string ) {
3937 const response = await createCourse ( { name } ) ;
@@ -45,46 +43,47 @@ const AddForm: React.FC = () => {
4543
4644 function handleSubmit ( e : React . FormEvent < HTMLFormElement > ) {
4745 e . preventDefault ( ) ;
46+ console . log ( 'handling form submit' ) ;
4847 if ( title . length > 1 ) addCourse ( title ) ;
4948 else {
5049 // TODO: display error
5150 }
5251 setTitle ( '' ) ;
53- dispatch ( setForm ( open ) ) ;
5452 }
53+
54+ function handleClose ( ) {
55+ console . log ( 'handling close' ) ;
56+ dispatch ( setSelected ( null ) ) ;
57+ navigate ( '/homepage' ) ;
58+ }
59+
5560 return (
56- < div className = 'h-full w-1/2 flex justify-center flex-row border-4 mx-auto mt-16' >
61+ < div className = "w-2/3 flex justify-center flex-row border-4 mx-auto mt-24" >
5762 < section className = { popUpStyle } >
58- < section className = 'flex flex-row justify-between items-center bg-black p-8 h-20 mb-4 text-3xl' >
59- < h1 className = 'font-bold md:text-3xl text-xl' > Add new class.</ h1 >
60- < Link to = '/homepage' >
61- < button
62- onClick = { ( ) => {
63- dispatch ( setForm ( open ) ) ;
64- } }
65- >
66- < CloseBtn className = 'w-10 h-10' />
67- </ button >
68- </ Link >
63+ < section className = "flex flex-row justify-between items-center bg-black p-8 h-20 mb-4 text-3xl" >
64+ < h1 className = "font-bold md:text-3xl text-xl" > Add new class.</ h1 >
65+ < button onClick = { handleClose } >
66+ < CloseBtn className = "w-10 h-10" />
67+ </ button >
6968 </ section >
70- < article className = ' flex flex-col md:h-3/4 w-full justify-center items-center p-0 h-0' >
69+ < article className = " flex flex-col md:h-3/4 w-full justify-center items-center p-0 h-0" >
7170 < div className = { lottieStyle } >
7271 < Lottie options = { defaultOptions } height = { '100%' } width = { '100%' } />
7372 </ div >
7473 < form
7574 onSubmit = { ( e ) => handleSubmit ( e ) }
76- className = ' flex flex-row items-center w-7/12 border-4 border-green'
75+ className = " flex flex-row items-center w-7/12 border-4 border-green"
7776 >
7877 < input
79- className = ' h-14 w-full sm:w-full text-2xl text-black px-6 focus:outline-none'
80- type = ' text'
78+ className = " h-14 w-full sm:w-full text-2xl text-black px-6 focus:outline-none"
79+ type = " text"
8180 value = { title }
82- placeholder = ' Input class title.'
81+ placeholder = " Input class title."
8382 onChange = { ( e ) => setTitle ( e . target . value ) }
8483 />
8584 < button
86- type = ' submit'
87- className = ' bg-green text-white text-lg lg:text-2xl sm:text-xl w-5/12 lg:w-3/12 h-14'
85+ type = " submit"
86+ className = " bg-green text-white text-lg lg:text-2xl sm:text-xl w-5/12 lg:w-3/12 h-14"
8887 >
8988 Add
9089 </ button >
0 commit comments