1
- import React , { FC , useState , useRef , useEffect } from 'react' ;
1
+ import React , { FC , useState , useEffect } from 'react' ;
2
2
import './AddYearPopup.scss' ;
3
3
import { PlusCircleFill } from 'react-bootstrap-icons' ;
4
- import { Button , Form , Popover , Overlay } from 'react-bootstrap' ;
4
+ import { Button , Form , Popover , OverlayTrigger } from 'react-bootstrap' ;
5
5
import { addYear } from '../../store/slices/roadmapSlice' ;
6
6
import { useAppDispatch } from '../../store/hooks' ;
7
7
@@ -16,99 +16,95 @@ const AddYearPopup: FC<AddYearPopupProps> = ({ placeholderName, placeholderYear
16
16
const [ year , setYear ] = useState ( placeholderYear ) ;
17
17
const [ validated , setValidated ] = useState ( false ) ;
18
18
const [ show , setShow ] = useState ( false ) ;
19
- const target = useRef ( null ) ;
20
19
21
20
useEffect ( ( ) => {
22
21
setYear ( placeholderYear ) ;
23
22
setName ( placeholderName ) ;
24
23
} , [ placeholderYear , placeholderName ] ) ;
25
24
26
- const handleClick = ( ) => {
27
- setShow ( ! show ) ;
28
- } ;
25
+ const overlay = < Popover id = "add-year-popover" >
26
+ < Popover . Content >
27
+ < Form noValidate validated = { validated } >
28
+ < Form . Group >
29
+ < Form . Label className = "add-year-form-label" > Name</ Form . Label >
30
+ < Form . Control
31
+ required
32
+ type = "text"
33
+ name = "name"
34
+ value = { name }
35
+ onChange = { ( e ) => {
36
+ setName ( e . target . value ) ;
37
+ } }
38
+ onKeyDown = { ( e : React . KeyboardEvent ) => {
39
+ // prevent submitting form (reloads the page)
40
+ if ( e . key === 'Enter' ) {
41
+ e . preventDefault ( ) ;
42
+ }
43
+ } }
44
+ maxLength = { 35 }
45
+ placeholder = { placeholderName }
46
+ > </ Form . Control >
47
+ </ Form . Group >
48
+ < Form . Group >
49
+ < Form . Label className = "add-year-form-label" > Start Year</ Form . Label >
50
+ < Form . Control
51
+ required
52
+ type = "number"
53
+ name = "year"
54
+ value = { year }
55
+ onChange = { ( e ) => {
56
+ setYear ( parseInt ( e . target . value ) ) ;
57
+ } }
58
+ onKeyDown = { ( e : React . KeyboardEvent ) => {
59
+ // prevent submitting form (reloads the page)
60
+ if ( e . key === 'Enter' ) {
61
+ e . preventDefault ( ) ;
62
+ }
63
+ } }
64
+ min = { 1000 }
65
+ max = { 9999 }
66
+ placeholder = { placeholderYear . toString ( ) }
67
+ > </ Form . Control >
68
+ </ Form . Group >
69
+ < Button
70
+ className = "popup-btn"
71
+ onClick = { ( ) => {
72
+ if ( name === '' || year < 1000 || year > 9999 || Number . isNaN ( year ) ) {
73
+ setValidated ( true ) ;
74
+ return ;
75
+ }
76
+
77
+ setValidated ( false ) ;
78
+ setShow ( ! show ) ;
79
+ dispatch (
80
+ addYear ( {
81
+ yearData : {
82
+ startYear : year ,
83
+ name : name . trim ( ) ,
84
+ quarters : [ 'fall' , 'winter' , 'spring' ] . map ( ( quarter ) => {
85
+ return { name : quarter , courses : [ ] } ;
86
+ } ) ,
87
+ } ,
88
+ } ) ,
89
+ ) ;
90
+ setYear ( placeholderYear ) ;
91
+ setName ( placeholderName ) ;
92
+ } }
93
+ >
94
+ Add Year
95
+ </ Button >
96
+ </ Form >
97
+ </ Popover . Content >
98
+ </ Popover >
29
99
30
100
return (
31
101
< div >
32
- < Button variant = "light" ref = { target } className = "add-year-btn" onClick = { handleClick } >
33
- < PlusCircleFill className = "add-year-icon" />
34
- < div className = "add-year-text" > Add year</ div >
35
- </ Button >
36
- < Overlay show = { show } target = { target } placement = "top" >
37
- < Popover id = "" >
38
- < Popover . Content >
39
- < Form noValidate validated = { validated } >
40
- < Form . Group >
41
- < Form . Label className = "add-year-form-label" > Name</ Form . Label >
42
- < Form . Control
43
- required
44
- type = "text"
45
- name = "name"
46
- value = { name }
47
- onChange = { ( e ) => {
48
- setName ( e . target . value ) ;
49
- } }
50
- onKeyDown = { ( e : React . KeyboardEvent ) => {
51
- // prevent submitting form (reloads the page)
52
- if ( e . key === 'Enter' ) {
53
- e . preventDefault ( ) ;
54
- }
55
- } }
56
- maxLength = { 35 }
57
- placeholder = { placeholderName }
58
- > </ Form . Control >
59
- </ Form . Group >
60
- < Form . Group >
61
- < Form . Label className = "add-year-form-label" > Start Year</ Form . Label >
62
- < Form . Control
63
- required
64
- type = "number"
65
- name = "year"
66
- value = { year }
67
- onChange = { ( e ) => {
68
- setYear ( parseInt ( e . target . value ) ) ;
69
- } }
70
- onKeyDown = { ( e : React . KeyboardEvent ) => {
71
- // prevent submitting form (reloads the page)
72
- if ( e . key === 'Enter' ) {
73
- e . preventDefault ( ) ;
74
- }
75
- } }
76
- min = { 1000 }
77
- max = { 9999 }
78
- placeholder = { placeholderYear . toString ( ) }
79
- > </ Form . Control >
80
- </ Form . Group >
81
- < Button
82
- className = "popup-btn"
83
- onClick = { ( ) => {
84
- if ( name === '' || year < 1000 || year > 9999 || Number . isNaN ( year ) ) {
85
- setValidated ( true ) ;
86
- return ;
87
- }
88
-
89
- setValidated ( false ) ;
90
- setShow ( ! show ) ;
91
- dispatch (
92
- addYear ( {
93
- yearData : {
94
- startYear : year ,
95
- name : name . trim ( ) ,
96
- quarters : [ 'fall' , 'winter' , 'spring' ] . map ( ( quarter ) => {
97
- return { name : quarter , courses : [ ] } ;
98
- } ) ,
99
- } ,
100
- } ) ,
101
- ) ;
102
- setYear ( placeholderYear ) ;
103
- setName ( placeholderName ) ;
104
- } }
105
- >
106
- Add Year
107
- </ Button >
108
- </ Form >
109
- </ Popover . Content >
110
- </ Popover >
111
- </ Overlay >
102
+ < OverlayTrigger placement = "top" overlay = { overlay } trigger = { [ 'click' ] } rootClose >
103
+ < Button variant = "light" className = "add-year-btn" >
104
+ < PlusCircleFill className = "add-year-icon" />
105
+ < div className = "add-year-text" > Add year</ div >
106
+ </ Button >
107
+ </ OverlayTrigger >
112
108
</ div >
113
109
) ;
114
110
} ;
0 commit comments