3
3
* Popup form to enter details about the
4
4
* team request before submitting.
5
5
*/
6
- import React , { useState } from "react" ;
6
+ import React , { useEffect , useState } from "react" ;
7
7
import PT from "prop-types" ;
8
8
import { Form , Field , useField } from "react-final-form" ;
9
9
import { useDispatch } from "react-redux" ;
@@ -18,6 +18,7 @@ import { deleteSearchedRole } from "../../actions";
18
18
import IconCrossLight from "../../../../assets/images/icon-cross-light.svg" ;
19
19
import "./styles.module.scss" ;
20
20
import NumberInput from "components/NumberInput" ;
21
+ import validator from "./utils/validator" ;
21
22
22
23
const Error = ( { name } ) => {
23
24
const {
@@ -28,80 +29,30 @@ const Error = ({ name }) => {
28
29
29
30
function TeamDetailsModal ( { open, onClose, submitForm, addedRoles } ) {
30
31
const [ showDescription , setShowDescription ] = useState ( false ) ;
31
- const [ startMonthVisible , setStartMonthVisible ] = useState ( ( ) => {
32
- const roles = { } ;
33
- addedRoles . forEach ( ( { searchId } ) => {
34
- roles [ searchId ] = false ;
35
- } ) ;
36
- return roles ;
37
- } ) ;
32
+ const [ startMonthVisible , setStartMonthVisible ] = useState ( { } ) ;
33
+
34
+ // Ensure role is removed from form state when it is removed from redux store
35
+ let getFormState ;
36
+ let clearFormField ;
37
+ useEffect ( ( ) => {
38
+ const values = getFormState ( ) . values ;
39
+ for ( let fieldName of Object . keys ( values ) ) {
40
+ if ( fieldName === "teamName" || fieldName === "teamDescription" ) {
41
+ continue ;
42
+ }
43
+ if ( addedRoles . findIndex ( ( role ) => role . searchId === fieldName ) ) {
44
+ clearFormField ( fieldName ) ;
45
+ setStartMonthVisible ( ( state ) => ( { ...state , [ fieldName ] : false } ) ) ;
46
+ }
47
+ }
48
+ } , [ getFormState , addedRoles , clearFormField ] ) ;
38
49
39
50
const dispatch = useDispatch ( ) ;
40
51
41
52
const toggleDescription = ( ) => {
42
53
setShowDescription ( ( prevState ) => ! prevState ) ;
43
54
} ;
44
55
45
- const validateName = ( name ) => {
46
- if ( ! name || name . trim ( ) . length === 0 ) {
47
- return "Please enter a team name." ;
48
- }
49
- return undefined ;
50
- } ;
51
-
52
- const validateNumber = ( number ) => {
53
- const converted = Number ( number ) ;
54
-
55
- if (
56
- Number . isNaN ( converted ) ||
57
- converted !== Math . floor ( converted ) ||
58
- converted < 1
59
- ) {
60
- return "Please enter a positive integer" ;
61
- }
62
- return undefined ;
63
- } ;
64
-
65
- const validateMonth = ( monthString ) => {
66
- const then = new Date ( monthString ) ;
67
- const now = new Date ( ) ;
68
- const thenYear = then . getFullYear ( ) ;
69
- const nowYear = now . getFullYear ( ) ;
70
- const thenMonth = then . getMonth ( ) ;
71
- const nowMonth = now . getMonth ( ) ;
72
-
73
- if ( thenYear < nowYear || ( thenYear === nowYear && thenMonth < nowMonth ) ) {
74
- return "Start month may not be before current month" ;
75
- }
76
- return undefined ;
77
- } ;
78
-
79
- const validateRole = ( role ) => {
80
- const roleErrors = { } ;
81
- roleErrors . numberOfResources = validateNumber ( role . numberOfResources ) ;
82
- roleErrors . durationWeeks = validateNumber ( role . durationWeeks ) ;
83
- if ( role . startMonth ) {
84
- roleErrors . startMonth = validateMonth ( role . startMonth ) ;
85
- }
86
-
87
- return roleErrors ;
88
- } ;
89
-
90
- const validator = ( values ) => {
91
- const errors = { } ;
92
-
93
- errors . teamName = validateName ( values . teamName ) ;
94
-
95
- for ( const key of Object . keys ( values ) ) {
96
- if ( key === "teamDescription" || key === "teamName" ) continue ;
97
- errors [ key ] = validateRole ( values [ key ] ) ;
98
- }
99
-
100
- return errors ;
101
- } ;
102
-
103
- const validateRequired = value => ( value ? undefined : 'Please enter a positive integer' )
104
-
105
56
return (
106
57
< Form
107
58
onSubmit = { submitForm }
@@ -110,16 +61,18 @@ function TeamDetailsModal({ open, onClose, submitForm, addedRoles }) {
110
61
changeValue ( state , fieldName , ( ) => undefined ) ;
111
62
} ,
112
63
} }
113
- initialValues = { { teamName : "" } }
114
64
validate = { validator }
115
65
>
116
66
{ ( {
117
67
handleSubmit,
118
68
hasValidationErrors,
119
69
form : {
120
70
mutators : { clearField } ,
71
+ getState,
121
72
} ,
122
73
} ) => {
74
+ getFormState = getState ;
75
+ clearFormField = clearField ;
123
76
return (
124
77
< BaseCreateModal
125
78
open = { open }
@@ -136,6 +89,7 @@ function TeamDetailsModal({ open, onClose, submitForm, addedRoles }) {
136
89
Submit
137
90
</ Button >
138
91
}
92
+ disableFocusTrap
139
93
>
140
94
< div styleName = "modal-body" >
141
95
< FormField
@@ -183,7 +137,7 @@ function TeamDetailsModal({ open, onClose, submitForm, addedRoles }) {
183
137
< tr styleName = "role-row" key = { id } >
184
138
< td > { name } </ td >
185
139
< td >
186
- < Field validate = { validateRequired } name = { `${ id } .numberOfResources` } initialValue = "3" >
140
+ < Field name = { `${ id } .numberOfResources` } initialValue = "3" >
187
141
{ ( { input, meta } ) => (
188
142
< NumberInput
189
143
name = { input . name }
@@ -199,7 +153,7 @@ function TeamDetailsModal({ open, onClose, submitForm, addedRoles }) {
199
153
< Error name = { `${ id } .numberOfResources` } />
200
154
</ td >
201
155
< td >
202
- < Field validate = { validateRequired } name = { `${ id } .durationWeeks` } initialValue = "20" >
156
+ < Field name = { `${ id } .durationWeeks` } initialValue = "20" >
203
157
{ ( { input, meta } ) => (
204
158
< NumberInput
205
159
name = { input . name }
@@ -257,7 +211,6 @@ function TeamDetailsModal({ open, onClose, submitForm, addedRoles }) {
257
211
< button
258
212
styleName = "delete-role"
259
213
onClick = { ( ) => {
260
- clearField ( id ) ;
261
214
dispatch ( deleteSearchedRole ( id ) ) ;
262
215
} }
263
216
>
0 commit comments