@@ -103,6 +103,11 @@ export class FormStore extends ActionEmitter {
103
103
props ?: FieldProps ,
104
104
fieldsGroupId ?: string
105
105
) : 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
+
106
111
// Construct field state
107
112
const fieldState : FieldStoreState = {
108
113
Name : name ,
@@ -154,40 +159,54 @@ export class FormStore extends ActionEmitter {
154
159
}
155
160
156
161
// 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 ) ;
160
165
161
166
this . emit ( new Actions . FieldRegistered ( this . FormId , fieldId ) ) ;
162
167
}
163
168
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
+
165
175
const fgState : FieldsGroupStoreState = {
166
176
Name : name ,
167
177
Parent : parentId
168
178
} ;
169
179
170
180
const fgStateRecord = recordify < FieldsGroupStoreState , FieldsGroupStoreStateRecord > ( fgState ) ;
171
- this . State = this . State . withMutations ( state => {
172
- state . FieldsGroups = state . FieldsGroups . set ( id , fgStateRecord ) ;
173
- } ) ;
174
181
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 ) ) ;
176
188
}
177
189
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 = {
180
197
Name : name ,
181
198
ArrayName : name ,
182
199
Parent : parentId
183
200
} ;
184
201
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 ) ;
189
203
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 ) ) ;
191
210
}
192
211
193
212
public UnregisterField ( fieldId : string ) : void {
@@ -197,6 +216,20 @@ export class FormStore extends ActionEmitter {
197
216
} ) ;
198
217
}
199
218
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
+
200
233
public HasField ( fieldId : string ) : boolean {
201
234
return this . State . Fields . has ( fieldId ) ;
202
235
}
0 commit comments