@iapps/ng-dhis2-ui 1.2.134 → 1.2.136

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.
@@ -6670,7 +6670,7 @@ const InternationalPhoneField = ({ name, label, placeholder, value, required, di
6670
6670
  flex: '0 0 auto',
6671
6671
  width: 260,
6672
6672
  } },
6673
- React__default.createElement(FilterableSingleSelectField, { selected: selectedIsoCode, onChange: handleCountryChange, disabled: disabled, dense: true, filterable: true, onFilterChange: ({ value }) => setFilterText(value), noMatchText: "No matching country" }, filteredCountries.map((country) => (React__default.createElement(SingleSelectOption, { key: country.isoCode, value: country.isoCode, label: `${country.flag} ${country.name} (${country.dialCode})` }))))),
6673
+ React__default.createElement(FilterableSingleSelectField, { selected: selectedIsoCode, onChange: handleCountryChange, disabled: disabled, dense: true, filterable: true, onFilterChange: ({ value }) => setFilterText(value), noMatchText: "No matching country" }, filteredCountries.map((country) => (React__default.createElement(SingleSelectOption, { disabled: disabled, key: country.isoCode, value: country.isoCode, label: `${country.flag} ${country.name} (${country.dialCode})` }))))),
6674
6674
  React__default.createElement("div", { style: { flex: '1 1 0', minWidth: 0 } },
6675
6675
  React__default.createElement(InputField$1, { type: "tel", name: name, placeholder: placeholder ?? '767000000', value: localNumber, required: required, disabled: disabled, error: hasError, validationText: effectiveValidationText, autoComplete: "tel", max: selectedCountry.isoCode === 'TZ' ? '9' : '15', onChange: handleLocalChange, onBlur: () => {
6676
6676
  if (selectedCountry.isoCode === 'TZ') {
@@ -10268,6 +10268,47 @@ class ProgramEntryFormMetaData {
10268
10268
  this.config = config;
10269
10269
  return this;
10270
10270
  }
10271
+ getFieldIdentifiers(field) {
10272
+ const f = field;
10273
+ const candidateIds = [
10274
+ f.key,
10275
+ field.id,
10276
+ f.code,
10277
+ f.attribute,
10278
+ f.trackedEntityAttribute,
10279
+ f.dataElement,
10280
+ ];
10281
+ return candidateIds.filter((value) => typeof value === 'string' && value.length > 0);
10282
+ }
10283
+ getAutoAssignedFieldIdSet() {
10284
+ const autoAssigned = this.config?.autoAssignedValues;
10285
+ if (!autoAssigned?.length) {
10286
+ return null;
10287
+ }
10288
+ if (!this.autoAssignedFieldIdSet) {
10289
+ this.autoAssignedFieldIdSet = new Set(autoAssigned
10290
+ .map(({ field }) => field)
10291
+ .filter((value) => typeof value === 'string' && value.length > 0));
10292
+ }
10293
+ return this.autoAssignedFieldIdSet;
10294
+ }
10295
+ shouldDisableField(field) {
10296
+ const fieldIdentifiers = this.getFieldIdentifiers(field);
10297
+ if (!fieldIdentifiers.length) {
10298
+ return false;
10299
+ }
10300
+ const autoAssignedIdSet = this.getAutoAssignedFieldIdSet();
10301
+ if (!autoAssignedIdSet) {
10302
+ return false;
10303
+ }
10304
+ return fieldIdentifiers.some((id) => autoAssignedIdSet.has(id));
10305
+ }
10306
+ withDisabledFlags(field) {
10307
+ return {
10308
+ ...field,
10309
+ disabled: true,
10310
+ };
10311
+ }
10271
10312
  getProgramPromise() {
10272
10313
  const program = this.config.program;
10273
10314
  const programQuery = this.d2.programModule.program
@@ -10370,37 +10411,115 @@ class ProgramEntryFormMetaData {
10370
10411
  if (!this.program) {
10371
10412
  return [];
10372
10413
  }
10373
- return new ProgramEntryFormFieldUtil(this.program, this.config).fields;
10414
+ const fields = new ProgramEntryFormFieldUtil(this.program, this.config)
10415
+ .fields;
10416
+ const autoAssigned = this.config?.autoAssignedValues;
10417
+ if (!autoAssigned?.length) {
10418
+ return fields;
10419
+ }
10420
+ return fields.map((field) => this.shouldDisableField(field) ? this.withDisabledFlags(field) : field);
10374
10421
  }
10422
+ // get sections(): IProgramEntryFormSection[] {
10423
+ // if (!this.program) {
10424
+ // return [];
10425
+ // }
10426
+ // const { displayType, formType } = this.config;
10427
+ // if (!this.program || this.config.displayType !== 'FLAT') {
10428
+ // return [];
10429
+ // }
10430
+ // if (displayType === 'FLAT' && formType === 'TRACKER') {
10431
+ // return new ProgramEntryFormSectionUtil(this.program, this.config)
10432
+ // .programEntryFormSections;
10433
+ // }
10434
+ // return new ProgramEntryFormSectionUtil(this.program, this.config)
10435
+ // .programEntryFormSections;
10436
+ // }
10437
+ // get programStageSections(): IProgramEntryFormSection[] {
10438
+ // if (!this.program) {
10439
+ // return [];
10440
+ // }
10441
+ // const { displayType, formType } = this.config;
10442
+ // if (displayType === 'FLAT' && formType === 'EVENT') {
10443
+ // return new ProgramStageEntryFormSectionUtil(this.program, this.config)
10444
+ // .programStageEntryFormSections;
10445
+ // }
10446
+ // if (displayType === 'SECTION') {
10447
+ // return new ProgramStageEntryFormSectionUtil(this.program, this.config)
10448
+ // .programStageEntryFormSections;
10449
+ // }
10450
+ // return [];
10451
+ // }
10375
10452
  get sections() {
10376
10453
  if (!this.program) {
10377
10454
  return [];
10378
10455
  }
10379
- const { displayType, formType } = this.config;
10380
- if (!this.program || this.config.displayType !== 'FLAT') {
10456
+ const { displayType } = this.config;
10457
+ if (displayType !== 'FLAT') {
10381
10458
  return [];
10382
10459
  }
10383
- if (displayType === 'FLAT' && formType === 'TRACKER') {
10384
- return new ProgramEntryFormSectionUtil(this.program, this.config)
10385
- .programEntryFormSections;
10460
+ const baseSections = new ProgramEntryFormSectionUtil(this.program, this.config).programEntryFormSections;
10461
+ const autoAssigned = this.config?.autoAssignedValues;
10462
+ if (!autoAssigned?.length) {
10463
+ return baseSections;
10386
10464
  }
10387
- return new ProgramEntryFormSectionUtil(this.program, this.config)
10388
- .programEntryFormSections;
10465
+ return baseSections.map((section) => {
10466
+ const formFields = section.formFields ?? [];
10467
+ if (!formFields.length) {
10468
+ return section;
10469
+ }
10470
+ let hasDisabledField = false;
10471
+ const updatedFields = formFields.map((field) => {
10472
+ if (this.shouldDisableField(field)) {
10473
+ hasDisabledField = true;
10474
+ return this.withDisabledFlags(field);
10475
+ }
10476
+ return field;
10477
+ });
10478
+ if (!hasDisabledField) {
10479
+ return section;
10480
+ }
10481
+ return {
10482
+ ...section,
10483
+ formFields: updatedFields,
10484
+ };
10485
+ });
10389
10486
  }
10390
10487
  get programStageSections() {
10391
10488
  if (!this.program) {
10392
10489
  return [];
10393
10490
  }
10394
10491
  const { displayType, formType } = this.config;
10395
- if (displayType === 'FLAT' && formType === 'EVENT') {
10396
- return new ProgramStageEntryFormSectionUtil(this.program, this.config)
10397
- .programStageEntryFormSections;
10492
+ const isFlatEvent = displayType === 'FLAT' && formType === 'EVENT';
10493
+ const isSectionDisplay = displayType === 'SECTION';
10494
+ if (!isFlatEvent && !isSectionDisplay) {
10495
+ return [];
10398
10496
  }
10399
- if (displayType === 'SECTION') {
10400
- return new ProgramStageEntryFormSectionUtil(this.program, this.config)
10401
- .programStageEntryFormSections;
10497
+ const baseSections = new ProgramStageEntryFormSectionUtil(this.program, this.config).programStageEntryFormSections;
10498
+ const autoAssigned = this.config?.autoAssignedValues;
10499
+ if (!autoAssigned?.length) {
10500
+ return baseSections;
10402
10501
  }
10403
- return [];
10502
+ return baseSections.map((section) => {
10503
+ const formFields = section.formFields ?? [];
10504
+ if (!formFields.length) {
10505
+ return section;
10506
+ }
10507
+ let hasDisabledField = false;
10508
+ const updatedFields = formFields.map((field) => {
10509
+ if (this.shouldDisableField(field)) {
10510
+ hasDisabledField = true;
10511
+ return this.withDisabledFlags(field);
10512
+ }
10513
+ return field;
10514
+ });
10515
+ if (!hasDisabledField) {
10516
+ return section;
10517
+ }
10518
+ return {
10519
+ ...section,
10520
+ formFields: updatedFields,
10521
+ };
10522
+ });
10404
10523
  }
10405
10524
  get rules() {
10406
10525
  if (!this.program || !this.program.programRules) {
@@ -10519,23 +10638,54 @@ class ProgramEntryFormModule {
10519
10638
  };
10520
10639
  }, ...(ngDevMode ? [{ debugName: "FormActionButtons" }] : []));
10521
10640
  }
10641
+ // async ngOnInit() {
10642
+ // this.loading.set(true);
10643
+ // try {
10644
+ // // Loading metadata
10645
+ // const metaData = await new ProgramEntryFormMetaData()
10646
+ // .setConfig(this.config())
10647
+ // .get();
10648
+ // this.metaData.set(metaData);
10649
+ // // Load initial instance information
10650
+ // const instance = await this.#getInstance();
10651
+ // this.instance.set(instance);
10652
+ // if (this.config().autoAssignedValues) {
10653
+ // this.#updateInstanceWithAutoAssignedValues(
10654
+ // this.config().autoAssignedValues || []
10655
+ // );
10656
+ // }
10657
+ // this.loading.set(false);
10658
+ // } catch (err) {
10659
+ // this.loading.set(false);
10660
+ // }
10661
+ // }
10522
10662
  async ngOnInit() {
10523
10663
  this.loading.set(true);
10524
10664
  try {
10525
- // Loading metadata
10665
+ const config = this.config();
10526
10666
  const metaData = await new ProgramEntryFormMetaData()
10527
- .setConfig(this.config())
10667
+ .setConfig(config)
10528
10668
  .get();
10669
+ if (!metaData) {
10670
+ console.error('ProgramEntryFormMetaData.get() returned null');
10671
+ return;
10672
+ }
10529
10673
  this.metaData.set(metaData);
10530
- // Load initial instance information
10531
10674
  const instance = await this.#getInstance();
10675
+ if (!instance) {
10676
+ console.error('Failed to create or load instance for program entry form');
10677
+ return;
10678
+ }
10532
10679
  this.instance.set(instance);
10533
- if (this.config().autoAssignedValues) {
10534
- this.#updateInstanceWithAutoAssignedValues(this.config().autoAssignedValues || []);
10680
+ const autoAssigned = config.autoAssignedValues;
10681
+ if (autoAssigned?.length) {
10682
+ this.#updateInstanceWithAutoAssignedValues(autoAssigned);
10535
10683
  }
10536
- this.loading.set(false);
10537
10684
  }
10538
- catch (err) {
10685
+ catch (error) {
10686
+ console.error('Error during ProgramEntryForm initialization', error);
10687
+ }
10688
+ finally {
10539
10689
  this.loading.set(false);
10540
10690
  }
10541
10691
  }
@@ -10552,18 +10702,122 @@ class ProgramEntryFormModule {
10552
10702
  onCancel() {
10553
10703
  this.formCancel.emit();
10554
10704
  }
10705
+ #resolveFieldRuntimeId(configuredKey, metaData) {
10706
+ const formFields = metaData?.formFields;
10707
+ if (!formFields || formFields.length === 0) {
10708
+ return configuredKey;
10709
+ }
10710
+ for (let index = 0; index < formFields.length; index++) {
10711
+ const currentField = formFields[index];
10712
+ if (currentField.id === configuredKey) {
10713
+ return currentField.id;
10714
+ }
10715
+ if (currentField.attribute === configuredKey ||
10716
+ currentField.trackedEntityAttribute === configuredKey) {
10717
+ return currentField.id;
10718
+ }
10719
+ if (currentField.dataElement === configuredKey) {
10720
+ return currentField.id;
10721
+ }
10722
+ }
10723
+ return configuredKey;
10724
+ }
10725
+ // #updateInstanceWithAutoAssignedValues(
10726
+ // autoAssignedValues: AutoAssignedValues[]
10727
+ // ) {
10728
+ // const assignedDataValues = autoAssignedValues.reduce(
10729
+ // (entities, assignedValue) => {
10730
+ // return {
10731
+ // ...entities,
10732
+ // [assignedValue.field]: assignedValue.value,
10733
+ // };
10734
+ // },
10735
+ // {}
10736
+ // );
10737
+ // this.#updateInstance(assignedDataValues);
10738
+ // }
10739
+ // #updateInstanceWithAutoAssignedValues(
10740
+ // autoAssignedValues: AutoAssignedValues[]
10741
+ // ) {
10742
+ // const valueCount = autoAssignedValues?.length ?? 0;
10743
+ // if (valueCount === 0) {
10744
+ // return;
10745
+ // }
10746
+ // const metaData = this.metaData();
10747
+ // const aggregatedValues: Record<string, unknown> = {};
10748
+ // for (let index = 0; index < valueCount; index++) {
10749
+ // const { field: configuredKey, value } = autoAssignedValues[index];
10750
+ // const semanticKey = metaData
10751
+ // ? this.#resolveFieldRuntimeId(configuredKey, metaData)
10752
+ // : configuredKey;
10753
+ // aggregatedValues[semanticKey] = value;
10754
+ // if (configuredKey !== semanticKey) {
10755
+ // aggregatedValues[configuredKey] = value;
10756
+ // }
10757
+ // }
10758
+ // if (Object.keys(aggregatedValues).length === 0) {
10759
+ // return;
10760
+ // }
10761
+ // this.#updateInstance(aggregatedValues);
10762
+ // }
10555
10763
  #updateInstanceWithAutoAssignedValues(autoAssignedValues) {
10556
- const assignedDataValues = autoAssignedValues.reduce((entities, assignedValue) => {
10557
- return {
10558
- ...entities,
10559
- [assignedValue.field]: assignedValue.value,
10560
- };
10561
- }, {});
10562
- this.#updateInstance(assignedDataValues);
10764
+ const trackedEntityId = this.trackedEntity?.();
10765
+ if (typeof trackedEntityId === 'string' &&
10766
+ trackedEntityId.trim().length > 0) {
10767
+ return;
10768
+ }
10769
+ const valueCount = autoAssignedValues?.length ?? 0;
10770
+ if (valueCount === 0) {
10771
+ return;
10772
+ }
10773
+ const metaData = this.metaData();
10774
+ const aggregatedValues = {};
10775
+ for (let index = 0; index < valueCount; index++) {
10776
+ const { field: configuredKey, value } = autoAssignedValues[index];
10777
+ const semanticKey = metaData
10778
+ ? this.#resolveFieldRuntimeId(configuredKey, metaData)
10779
+ : configuredKey;
10780
+ aggregatedValues[semanticKey] = value;
10781
+ if (configuredKey !== semanticKey) {
10782
+ aggregatedValues[configuredKey] = value;
10783
+ }
10784
+ }
10785
+ if (Object.keys(aggregatedValues).length === 0) {
10786
+ return;
10787
+ }
10788
+ this.#updateInstance(aggregatedValues);
10563
10789
  }
10790
+ // #updateInstance(dataValues: Record<string, unknown>) {
10791
+ // this.instance.update((instance) => {
10792
+ // instance?.updateDataValues(dataValues, this.config().updateTeiOrgUnit);
10793
+ // return instance;
10794
+ // });
10795
+ // }
10796
+ // #updateInstance(dataValues: Record<string, unknown>) {
10797
+ // this.instance.update((instance) => {
10798
+ // if (!instance) {
10799
+ // return instance;
10800
+ // }
10801
+ // instance.updateDataValues(dataValues, this.config().updateTeiOrgUnit);
10802
+ // return instance;
10803
+ // });
10804
+ // }
10564
10805
  #updateInstance(dataValues) {
10806
+ if (!dataValues || Object.keys(dataValues).length === 0) {
10807
+ return;
10808
+ }
10809
+ const shouldUpdateOrgUnit = this.config().updateTeiOrgUnit;
10565
10810
  this.instance.update((instance) => {
10566
- instance?.updateDataValues(dataValues, this.config().updateTeiOrgUnit);
10811
+ if (!instance) {
10812
+ return instance;
10813
+ }
10814
+ for (const key in dataValues) {
10815
+ if (Object.prototype.hasOwnProperty.call(dataValues, key)) {
10816
+ instance[key] =
10817
+ dataValues[key];
10818
+ }
10819
+ }
10820
+ instance.updateDataValues(dataValues, shouldUpdateOrgUnit);
10567
10821
  return instance;
10568
10822
  });
10569
10823
  }