@@ -103,6 +103,11 @@ export class FormStore extends ActionEmitter {
103103 props ?: FieldProps ,
104104 fieldsGroupId ?: string
105105 ) : void {
106+ if ( this . State . Fields . has ( fieldId ) ||
107+ this . State . FieldsGroups . has ( fieldId ) ) {
108+ throw new Error ( `simplr-forms: Field '${ fieldId } ' already exists in form '${ this . FormId } .` ) ;
109+ }
110+
106111 // Construct field state
107112 const fieldState : FieldStoreState = {
108113 Name : name ,
@@ -154,40 +159,54 @@ export class FormStore extends ActionEmitter {
154159 }
155160
156161 // Add field into form store state
157- this . State = this . State . withMutations ( state => {
158- state . Fields = state . Fields . set ( fieldId , recordify < FieldStoreState , FieldStoreStateRecord > ( fieldState ) ) ;
159- } ) ;
162+ this . State = this . State . merge ( {
163+ Fields : this . State . Fields . set ( fieldId , recordify < FieldStoreState , FieldStoreStateRecord > ( fieldState ) )
164+ } as FormStoreStateRecord ) ;
160165
161166 this . emit ( new Actions . FieldRegistered ( this . FormId , fieldId ) ) ;
162167 }
163168
164- public RegisterFieldsGroup ( id : string , name : string , parentId ?: string ) : void {
169+ public RegisterFieldsGroup ( fieldsGroupId : string , name : string , parentId ?: string ) : void {
170+ if ( this . State . Fields . has ( fieldsGroupId ) ||
171+ this . State . FieldsGroups . has ( fieldsGroupId ) ) {
172+ throw new Error ( `simplr-forms: FieldsGroup '${ fieldsGroupId } ' already exists in form '${ this . FormId } .` ) ;
173+ }
174+
165175 const fgState : FieldsGroupStoreState = {
166176 Name : name ,
167177 Parent : parentId
168178 } ;
169179
170180 const fgStateRecord = recordify < FieldsGroupStoreState , FieldsGroupStoreStateRecord > ( fgState ) ;
171- this . State = this . State . withMutations ( state => {
172- state . FieldsGroups = state . FieldsGroups . set ( id , fgStateRecord ) ;
173- } ) ;
174181
175- this . emit ( new Actions . FieldsGroupRegistered ( this . FormId , id ) ) ;
182+ // Add fields group into form store state
183+ this . State = this . State . merge ( {
184+ FieldsGroups : this . State . FieldsGroups . set ( fieldsGroupId , fgStateRecord )
185+ } as FormStoreStateRecord ) ;
186+
187+ this . emit ( new Actions . FieldsGroupRegistered ( this . FormId , fieldsGroupId ) ) ;
176188 }
177189
178- public RegisterFieldsArray ( id : string , name : string , index : number , parentId ?: string ) : void {
179- const fgState : FieldsGroupStoreState = {
190+ public RegisterFieldsArray ( fieldsArrayId : string , name : string , index : number , parentId ?: string ) : void {
191+ if ( this . State . Fields . has ( fieldsArrayId ) ||
192+ this . State . FieldsGroups . has ( fieldsArrayId ) ) {
193+ throw new Error ( `simplr-forms: FieldsArray '${ fieldsArrayId } ' already exists in form '${ this . FormId } .` ) ;
194+ }
195+
196+ const faState : FieldsGroupStoreState = {
180197 Name : name ,
181198 ArrayName : name ,
182199 Parent : parentId
183200 } ;
184201
185- const fgStateRecord = recordify < FieldsGroupStoreState , FieldsGroupStoreStateRecord > ( fgState ) ;
186- this . State = this . State . withMutations ( state => {
187- state . FieldsGroups = state . FieldsGroups . set ( id , fgStateRecord ) ;
188- } ) ;
202+ const faStateRecord = recordify < FieldsGroupStoreState , FieldsGroupStoreStateRecord > ( faState ) ;
189203
190- this . emit ( new Actions . FieldsArrayRegistered ( this . FormId , id ) ) ;
204+ // Add fields array into form store state
205+ this . State = this . State . merge ( {
206+ FieldsGroups : this . State . FieldsGroups . set ( fieldsArrayId , faStateRecord )
207+ } as FormStoreStateRecord ) ;
208+
209+ this . emit ( new Actions . FieldsArrayRegistered ( this . FormId , fieldsArrayId ) ) ;
191210 }
192211
193212 public UnregisterField ( fieldId : string ) : void {
@@ -197,6 +216,20 @@ export class FormStore extends ActionEmitter {
197216 } ) ;
198217 }
199218
219+ public UnregisterFieldsGroup ( fieldsGroupId : string ) : void {
220+ // Remove fields group from form store state
221+ this . State = this . State . withMutations ( state => {
222+ state . FieldsGroups = state . FieldsGroups . remove ( fieldsGroupId ) ;
223+ } ) ;
224+ }
225+
226+ public UnregisterFieldsArray ( fieldsGroupId : string ) : void {
227+ // Remove fields array from form store state
228+ this . State = this . State . withMutations ( state => {
229+ state . FieldsGroups = state . FieldsGroups . remove ( fieldsGroupId ) ;
230+ } ) ;
231+ }
232+
200233 public HasField ( fieldId : string ) : boolean {
201234 return this . State . Fields . has ( fieldId ) ;
202235 }
0 commit comments