@@ -166,22 +166,34 @@ export const SkillFormGithub: React.FunctionComponent<SkillFormProps> = ({ skill
166
166
return ValidatedOptions . success ;
167
167
} ;
168
168
169
- const validateQuestion = ( question : string ) : ValidatedOptions => {
170
- if ( question . length > 0 && question . length < 250 ) {
171
- setDisableAction ( ! checkSkillFormCompletion ( skillFormData ) ) ;
172
- return ValidatedOptions . success ;
169
+ const validateQuestion = ( question : string ) => {
170
+ const questionStr = question . trim ( ) ;
171
+ if ( questionStr . length === 0 ) {
172
+ setDisableAction ( true ) ;
173
+ return { msg : 'Question is required' , status : ValidatedOptions . error } ;
174
+ }
175
+ const tokens = questionStr . split ( / \s + / ) ;
176
+ if ( tokens . length > 0 && tokens . length < 250 ) {
177
+ setDisableAction ( ! checkSkillFormCompletion ( skillFormData , true ) ) ;
178
+ return { msg : 'Valid input' , status : ValidatedOptions . success } ;
173
179
}
174
180
setDisableAction ( true ) ;
175
- return ValidatedOptions . error ;
181
+ return { msg : 'Question must be less than 250 words. Current word count: ' + tokens . length , status : ValidatedOptions . error } ;
176
182
} ;
177
183
178
- const validateAnswer = ( answer : string ) : ValidatedOptions => {
179
- if ( answer . length > 0 && answer . length < 250 ) {
180
- setDisableAction ( ! checkSkillFormCompletion ( skillFormData ) ) ;
181
- return ValidatedOptions . success ;
184
+ const validateAnswer = ( answer : string ) => {
185
+ const answerStr = answer . trim ( ) ;
186
+ if ( answerStr . length === 0 ) {
187
+ setDisableAction ( true ) ;
188
+ return { msg : 'Answer is required' , status : ValidatedOptions . error } ;
189
+ }
190
+ const tokens = answerStr . split ( / \s + / ) ;
191
+ if ( tokens . length > 0 && tokens . length < 250 ) {
192
+ setDisableAction ( ! checkSkillFormCompletion ( skillFormData , true ) ) ;
193
+ return { msg : 'Valid input' , status : ValidatedOptions . success } ;
182
194
}
183
195
setDisableAction ( true ) ;
184
- return ValidatedOptions . error ;
196
+ return { msg : 'Answer must be less than 250 words. Current word count: ' + tokens . length , status : ValidatedOptions . error } ;
185
197
} ;
186
198
187
199
const handleContextInputChange = ( seedExampleIndex : number , contextValue : string ) : void => {
@@ -225,14 +237,17 @@ export const SkillFormGithub: React.FunctionComponent<SkillFormProps> = ({ skill
225
237
226
238
const handleAnswerBlur = ( seedExampleIndex : number ) : void => {
227
239
setSeedExamples (
228
- seedExamples . map ( ( seedExample : SkillSeedExample , index : number ) =>
229
- index === seedExampleIndex
230
- ? {
231
- ...seedExample ,
232
- isAnswerValid : validateAnswer ( seedExample . answer )
233
- }
234
- : seedExample
235
- )
240
+ seedExamples . map ( ( seedExample : SkillSeedExample , index : number ) => {
241
+ if ( index === seedExampleIndex ) {
242
+ const { msg, status } = validateAnswer ( seedExample . answer ) ;
243
+ return {
244
+ ...seedExample ,
245
+ isAnswerValid : status ,
246
+ answerValidationError : msg
247
+ } ;
248
+ }
249
+ return seedExample ;
250
+ } )
236
251
) ;
237
252
} ;
238
253
@@ -251,14 +266,17 @@ export const SkillFormGithub: React.FunctionComponent<SkillFormProps> = ({ skill
251
266
252
267
const handleQuestionBlur = ( seedExampleIndex : number ) : void => {
253
268
setSeedExamples (
254
- seedExamples . map ( ( seedExample : SkillSeedExample , index : number ) =>
255
- index === seedExampleIndex
256
- ? {
257
- ...seedExample ,
258
- isQuestionValid : validateQuestion ( seedExample . question )
259
- }
260
- : seedExample
261
- )
269
+ seedExamples . map ( ( seedExample : SkillSeedExample , index : number ) => {
270
+ if ( index === seedExampleIndex ) {
271
+ const { msg, status } = validateQuestion ( seedExample . question ) ;
272
+ return {
273
+ ...seedExample ,
274
+ isQuestionValid : status ,
275
+ questionValidationError : msg
276
+ } ;
277
+ }
278
+ return seedExample ;
279
+ } )
262
280
) ;
263
281
} ;
264
282
0 commit comments