1
1
import React from "react" ;
2
2
import PT from "prop-types" ;
3
+ import { trim , findIndex } from "lodash" ;
3
4
4
5
import Button from "../Button" ;
5
6
import Input from "../Input" ;
@@ -87,6 +88,38 @@ export default function EditProfileModal({
87
88
setLocalUser ( { ...localUser , skills } ) ;
88
89
} ;
89
90
91
+ /**
92
+ * Checks if all required fields are field
93
+ * @param {Object } localUser user's new data
94
+ */
95
+ const isValid = ( localUser ) => {
96
+ let fieldName ;
97
+ if ( ! trim ( localUser . firstName ) ) {
98
+ fieldName = "First name" ;
99
+ } else if ( ! trim ( localUser . lastName ) ) {
100
+ fieldName = "Last name" ;
101
+ } else if ( ! trim ( localUser . title . value ) ) {
102
+ fieldName = "Current role" ;
103
+ } else if ( ! trim ( localUser . company . value ) ) {
104
+ fieldName = "Company" ;
105
+ } else if ( ! trim ( localUser . location . value ) ) {
106
+ fieldName = "Location" ;
107
+ } else {
108
+ let fieldIndex = findIndex (
109
+ localUser . companyAttributes ,
110
+ ( a ) => ! trim ( a . value )
111
+ ) ;
112
+ if ( fieldIndex !== - 1 ) {
113
+ fieldName = localUser . companyAttributes [ fieldIndex ] . name ;
114
+ }
115
+ }
116
+ if ( fieldName ) {
117
+ alert ( `Please enter a value for the "${ fieldName } " field` ) ;
118
+ return false ;
119
+ }
120
+ return true ;
121
+ } ;
122
+
90
123
return (
91
124
< Modal className = { style . container } onCancel = { onCancel } >
92
125
< ProfileCard
@@ -111,8 +144,10 @@ export default function EditProfileModal({
111
144
isSavingChanges ? style . disabledButton : style . saveButton
112
145
}
113
146
onClick = { ( ) => {
114
- setIsSavingChanges ( true ) ;
115
- updateUser ( localUser ) ;
147
+ if ( isValid ( localUser ) ) {
148
+ setIsSavingChanges ( true ) ;
149
+ updateUser ( localUser ) ;
150
+ }
116
151
} }
117
152
disabled = { isSavingChanges || isDeactivatingUser }
118
153
>
@@ -132,6 +167,7 @@ export default function EditProfileModal({
132
167
setImmediate ( ( ) => target . focus ( ) ) ;
133
168
} }
134
169
value = { localUser . firstName }
170
+ required
135
171
/>
136
172
< Input
137
173
label = "Last name"
@@ -143,6 +179,7 @@ export default function EditProfileModal({
143
179
setImmediate ( ( ) => target . focus ( ) ) ;
144
180
} }
145
181
value = { localUser . lastName }
182
+ required
146
183
/>
147
184
< Input
148
185
label = "Current role"
@@ -157,6 +194,7 @@ export default function EditProfileModal({
157
194
setImmediate ( ( ) => target . focus ( ) ) ;
158
195
} }
159
196
value = { localUser . title . value }
197
+ required
160
198
/>
161
199
< Input
162
200
label = "Company"
@@ -171,6 +209,7 @@ export default function EditProfileModal({
171
209
setImmediate ( ( ) => target . focus ( ) ) ;
172
210
} }
173
211
value = { localUser . company . value }
212
+ required
174
213
/>
175
214
< Input
176
215
label = "Location"
@@ -185,6 +224,7 @@ export default function EditProfileModal({
185
224
setImmediate ( ( ) => target . focus ( ) ) ;
186
225
} }
187
226
value = { localUser . location . value }
227
+ required
188
228
/>
189
229
</ div >
190
230
< h3 > Skills</ h3 >
@@ -234,6 +274,7 @@ export default function EditProfileModal({
234
274
setImmediate ( ( ) => target . focus ( ) ) ;
235
275
} }
236
276
value = { localUser . companyAttributes [ key ] . value }
277
+ required
237
278
/>
238
279
) ) }
239
280
</ div >
0 commit comments