@masterteam/form-builder 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,9 +21,11 @@ import {
21
21
  DeleteSection,
22
22
  GetFormConfiguration,
23
23
  MoveField,
24
+ ReorderFields,
24
25
  ResetFormBuilderState,
25
26
  ResetFormConfiguration,
26
27
  SetModuleInfo,
28
+ SetProperties,
27
29
  UpdateField,
28
30
  UpdateSection,
29
31
  } from './form-builder.actions';
@@ -40,6 +42,9 @@ const DEFAULT_STATE: FormBuilderStateModel = {
40
42
  // Form data
41
43
  formConfiguration: null,
42
44
 
45
+ // Properties for field enrichment
46
+ properties: [],
47
+
43
48
  // Loading state (from LoadingStateShape)
44
49
  loadingActive: [],
45
50
  errors: {},
@@ -98,6 +103,11 @@ export class FormBuilderState extends CrudStateBase<
98
103
  return state?.moduleId ?? null;
99
104
  }
100
105
 
106
+ @Selector()
107
+ static getProperties(state: FormBuilderStateModel) {
108
+ return state?.properties ?? [];
109
+ }
110
+
101
111
  // ============================================================================
102
112
  // Module Configuration Actions
103
113
  // ============================================================================
@@ -127,6 +137,16 @@ export class FormBuilderState extends CrudStateBase<
127
137
  ctx.setState(DEFAULT_STATE);
128
138
  }
129
139
 
140
+ @Action(SetProperties)
141
+ setProperties(
142
+ ctx: StateContext<FormBuilderStateModel>,
143
+ action: SetProperties,
144
+ ) {
145
+ ctx.patchState({
146
+ properties: action.properties,
147
+ });
148
+ }
149
+
130
150
  // ============================================================================
131
151
  // Form Configuration Actions
132
152
  // ============================================================================
@@ -382,13 +402,16 @@ export class FormBuilderState extends CrudStateBase<
382
402
  const sections = (currentState.formConfiguration?.sections ?? []).map(
383
403
  (section) => {
384
404
  if (section.id === action.sectionId) {
385
- // Update the field and sort by order
386
- const updatedFields = section.fields.map((f) =>
387
- f.id === updatedField.id ? updatedField : f,
405
+ // Update the field but preserve current array order (from optimistic update)
406
+ // Don't re-sort by order property as other fields may have stale order values
407
+ const updatedFields = section.fields.map((f, index) =>
408
+ f.id === updatedField.id
409
+ ? { ...updatedField, order: index }
410
+ : { ...f, order: index },
388
411
  );
389
412
  return {
390
413
  ...section,
391
- fields: updatedFields.sort((a, b) => a.order - b.order),
414
+ fields: updatedFields,
392
415
  };
393
416
  }
394
417
  return section;
@@ -490,6 +513,76 @@ export class FormBuilderState extends CrudStateBase<
490
513
  });
491
514
  }
492
515
 
516
+ @Action(ReorderFields)
517
+ reorderFields(
518
+ ctx: StateContext<FormBuilderStateModel>,
519
+ action: ReorderFields,
520
+ ) {
521
+ const state = ctx.getState();
522
+ const apiPath = `${this.getApiPath(state)}/sections/${action.sectionId}/fields/reorder`;
523
+ const req$ = this.http.put<Response<FormField[]>>(apiPath, action.payload);
524
+
525
+ // Create order map for optimistic update
526
+ const orderMap = new Map(action.payload.map((p) => [p.id, p.order]));
527
+
528
+ // Apply optimistic update
529
+ const optimisticSections = (state.formConfiguration?.sections ?? []).map(
530
+ (section) => {
531
+ if (section.id === action.sectionId) {
532
+ const reorderedFields = section.fields
533
+ .map((f) => ({
534
+ ...f,
535
+ order: orderMap.get(f.id) ?? f.order,
536
+ }))
537
+ .sort((a, b) => a.order - b.order);
538
+ return { ...section, fields: reorderedFields };
539
+ }
540
+ return section;
541
+ },
542
+ );
543
+
544
+ ctx.patchState({
545
+ formConfiguration: {
546
+ ...state.formConfiguration!,
547
+ sections: optimisticSections,
548
+ },
549
+ });
550
+
551
+ return handleApiRequest({
552
+ ctx,
553
+ key: FormBuilderActionKey.ReorderFields,
554
+ request$: req$,
555
+ onSuccess: (res, currentState) => {
556
+ const updatedFields = res.data;
557
+ const fieldMap = new Map(updatedFields.map((f) => [f.id, f]));
558
+
559
+ const sections = (currentState.formConfiguration?.sections ?? []).map(
560
+ (section) => {
561
+ if (section.id === action.sectionId) {
562
+ const mergedFields = section.fields
563
+ .map((f) => fieldMap.get(f.id) ?? f)
564
+ .sort((a, b) => a.order - b.order);
565
+ return { ...section, fields: mergedFields };
566
+ }
567
+ return section;
568
+ },
569
+ );
570
+ return {
571
+ formConfiguration: {
572
+ ...currentState.formConfiguration!,
573
+ sections,
574
+ },
575
+ };
576
+ },
577
+ onError: (_error, _currentState) => {
578
+ // Revert to original state on error
579
+ return {
580
+ formConfiguration: state.formConfiguration,
581
+ };
582
+ },
583
+ });
584
+ }
585
+
493
586
  @Action(MoveField)
494
587
  moveField(ctx: StateContext<FormBuilderStateModel>, action: MoveField) {
495
588
  const state = ctx.getState();