11import * as React from "react" ;
22import * as Immutable from "immutable" ;
3- import { recordify } from "typed-immutable-record" ;
3+ import { recordify , TypedRecord } from "typed-immutable-record" ;
44import { ActionEmitter } from "action-emitter" ;
55
66import * as Actions from "../actions/form-store" ;
@@ -25,7 +25,7 @@ import {
2525} from "../contracts/form-store" ;
2626import { FieldsGroupStateRecord } from "../contracts/fields-group" ;
2727import { ConstructFormError } from "../utils/form-error-helpers" ;
28- import { FormError , FormErrorRecord } from "../contracts/error" ;
28+ import { FormError , FormErrorRecord , FormErrorOrigin } from "../contracts/error" ;
2929
3030export class FormStore extends ActionEmitter {
3131 constructor ( formId : string ) {
@@ -194,7 +194,12 @@ export class FormStore extends ActionEmitter {
194194 Touched : true
195195 } as FieldState ) ) ;
196196
197- return this . RecalculateDependentFormState ( state ) ;
197+ state . Form = state . Form . merge ( {
198+ SuccessfullySubmitted : false ,
199+ Error : undefined
200+ } as FormState ) ;
201+
202+ return this . RecalculateDependentFormStatuses ( state ) ;
198203 } ) ;
199204
200205 this . emit ( new Actions . ValueChanged ( fieldId , newValue ) ) ;
@@ -235,7 +240,7 @@ export class FormStore extends ActionEmitter {
235240 Validating : false
236241 } as FieldState ) ) ;
237242
238- return this . RecalculateDependentFormState ( state ) ;
243+ return this . RecalculateDependentFormStatuses ( state ) ;
239244 } ) ;
240245 } catch ( error ) {
241246 // Skip validation if the value has changed again
@@ -244,7 +249,7 @@ export class FormStore extends ActionEmitter {
244249 return ;
245250 }
246251
247- const formError = ConstructFormError ( error ) ;
252+ const formError = ConstructFormError ( error , FormErrorOrigin . Validation ) ;
248253 if ( formError == null ) {
249254 throw Error ( error ) ;
250255 }
@@ -256,11 +261,42 @@ export class FormStore extends ActionEmitter {
256261 Error : recordify < FormError , FormErrorRecord > ( formError ! )
257262 } as FieldState ) ) ;
258263
259- return this . RecalculateDependentFormState ( state ) ;
264+ return this . RecalculateDependentFormStatuses ( state ) ;
260265 } ) ;
261266 }
262267 }
263268
269+ public SetActiveField ( fieldId : string | undefined ) : void {
270+ this . State = this . State . withMutations ( state => {
271+ if ( fieldId == null ) {
272+ return state . Form . merge ( {
273+ ActiveFieldId : undefined
274+ } as FormState ) ;
275+ }
276+
277+ const fieldState = this . State . Fields . get ( fieldId ) ;
278+ if ( fieldState == null ) {
279+ console . warn ( `simplr-forms: Given field '${ fieldId } ' does not exist in form '${ this . FormId } ', `
280+ + `therefore field cannot be focused. Form.ActiveFieldId was reset to an undefined.` ) ;
281+ return state . Form . merge ( {
282+ ActiveFieldId : undefined
283+ } as FormState ) ;
284+ }
285+
286+ state . Form = state . Form . merge ( {
287+ ActiveFieldId : fieldId
288+ } as FormState ) ;
289+
290+ state . Fields = state . Fields . withMutations ( fields => {
291+ fields . set ( fieldId , fieldState . merge ( {
292+ Touched : true
293+ } as FieldState ) ) ;
294+ } ) ;
295+
296+ return this . RecalculateDependentFormStatuses ( state ) ;
297+ } ) ;
298+ }
299+
264300 public async ValidateForm ( validationPromise : Promise < never > ) : Promise < void > {
265301 const form = this . State . Form ;
266302
@@ -288,10 +324,10 @@ export class FormStore extends ActionEmitter {
288324 Validating : false
289325 } as FormState ) ;
290326
291- return this . RecalculateDependentFormState ( state ) ;
327+ return this . RecalculateDependentFormStatuses ( state ) ;
292328 } ) ;
293329 } catch ( error ) {
294- const formError = ConstructFormError ( error ) ;
330+ const formError = ConstructFormError ( error , FormErrorOrigin . Validation ) ;
295331 if ( formError == null ) {
296332 throw Error ( error ) ;
297333 }
@@ -302,7 +338,7 @@ export class FormStore extends ActionEmitter {
302338 Error : recordify < FormError , FormErrorRecord > ( formError ! )
303339 } as FormState ) ;
304340
305- return this . RecalculateDependentFormState ( state ) ;
341+ return this . RecalculateDependentFormStatuses ( state ) ;
306342 } ) ;
307343 }
308344 }
@@ -320,7 +356,7 @@ export class FormStore extends ActionEmitter {
320356 promise = result ;
321357 } else {
322358 promise = new Promise < void > ( ( resolve , reject ) => {
323- const error = ConstructFormError ( result ) ;
359+ const error = ConstructFormError ( result , FormErrorOrigin . Submit ) ;
324360 if ( error !== undefined ) {
325361 reject ( result ) ;
326362 return ;
@@ -334,24 +370,34 @@ export class FormStore extends ActionEmitter {
334370 state . Form = state . Form . merge ( {
335371 Submitting : true
336372 } as FormState ) ;
373+ return state ;
337374 } ) ;
338-
339375 // Try submitting
340376 try {
341377 await promise ;
342378 // No error and submitting -> false
343379 this . State = this . State . withMutations ( state => {
344380 state . Form = state . Form . merge ( {
345381 Submitting : false ,
382+ SuccessfullySubmitted : true ,
346383 Error : undefined
347384 } as FormState ) ;
385+ return state ;
348386 } ) ;
349- } catch ( err ) {
387+ } catch ( caughtError ) {
388+ // Set error origin
389+ const constructedError = ConstructFormError ( caughtError , FormErrorOrigin . Submit ) ;
390+ let error : FormErrorRecord ;
391+ if ( constructedError != null ) {
392+ error = recordify < FormError , FormErrorRecord > ( constructedError ) ;
393+ }
394+
350395 // Error and submitting -> false
351396 this . State = this . State . withMutations ( state => {
352397 state . Form = state . Form . merge ( {
353398 Submitting : false ,
354- Error : err
399+ SuccessfullySubmitted : false ,
400+ Error : error
355401 } as FormState ) ;
356402 } ) ;
357403 }
@@ -380,7 +426,7 @@ export class FormStore extends ActionEmitter {
380426 }
381427 } ) ;
382428
383- return this . RecalculateDependentFormState ( state ) ;
429+ return this . RecalculateDependentFormStatuses ( state ) ;
384430 } ) ;
385431 }
386432
@@ -406,7 +452,7 @@ export class FormStore extends ActionEmitter {
406452 }
407453 } ) ;
408454
409- return this . RecalculateDependentFormState ( state ) ;
455+ return this . RecalculateDependentFormStatuses ( state ) ;
410456 } ) ;
411457 }
412458
@@ -490,7 +536,7 @@ export class FormStore extends ActionEmitter {
490536 return formStoreObject ;
491537 }
492538
493- protected RecalculateDependentFormState ( formStoreState : FormStoreStateRecord ) : FormStoreStateRecord {
539+ protected RecalculateDependentFormStatuses ( formStoreState : FormStoreStateRecord ) : FormStoreStateRecord {
494540 let updater : FormStoreStateStatus = this . GetInitialStoreStatus ( ) ;
495541
496542 // TODO: might build curried function for more efficient checking.
@@ -664,7 +710,7 @@ export class FormStore extends ActionEmitter {
664710 return result ;
665711 }
666712
667- protected RemoveValues < T > ( array : T [ ] , valuesToRemove : T [ ] , concat : boolean = true ) {
713+ protected RemoveValues < T > ( array : T [ ] , valuesToRemove : T [ ] , concat : boolean = true ) : T [ ] {
668714 let result = concat ? array . concat ( ) : array ;
669715 for ( const value of valuesToRemove ) {
670716 let index ;
0 commit comments