@luomus/laji-form 15.1.95 → 15.1.97

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.
@@ -46,6 +46,7 @@ export default class TaxonSetPopulatorField extends React.Component<FieldProps>
46
46
  getStateFromProps(): {
47
47
  onChange: (formData: any) => Promise<void>;
48
48
  };
49
+ componentDidMount(): Promise<void>;
49
50
  onChange: (formData: any) => Promise<void>;
50
51
  private fetchTaxaFromSet;
51
52
  }
@@ -84,6 +84,7 @@ let TaxonSetPopulatorField = class TaxonSetPopulatorField extends React.Componen
84
84
  this.unitTaxonSets = {};
85
85
  this.onChange = (formData) => __awaiter(this, void 0, void 0, function* () {
86
86
  var _a, _b, _c;
87
+ const { translations } = this.props.formContext;
87
88
  const previousTaxonSets = this.selectedTaxonSets;
88
89
  const currentTaxonSets = ((_a = formData === null || formData === void 0 ? void 0 : formData.taxonCensus) === null || _a === void 0 ? void 0 : _a.map((item) => item.censusTaxonSetID)) || [];
89
90
  const deletedTaxonSets = previousTaxonSets.filter((item) => !currentTaxonSets.includes(item));
@@ -98,32 +99,27 @@ let TaxonSetPopulatorField = class TaxonSetPopulatorField extends React.Componen
98
99
  return this.unitTaxonSets[unit.identifications[0].taxonID]
99
100
  && this.unitTaxonSets[unit.identifications[0].taxonID].includes(deletedTaxonSetId);
100
101
  });
101
- deletedTaxonSetUnits.map((unit) => {
102
+ observationsExist = deletedTaxonSetUnits.some((unit) => {
102
103
  var _a, _b, _c, _d;
103
- if (unit.maleIndividualCount ||
104
+ return unit.maleIndividualCount ||
104
105
  unit.femaleIndividualCount ||
105
106
  unit.nestCount ||
106
107
  ((_a = unit.unitFact) === null || _a === void 0 ? void 0 : _a.destroyedNestCount) ||
107
108
  ((_b = unit.unitFact) === null || _b === void 0 ? void 0 : _b.broodCount) ||
108
109
  ((_c = unit.unitFact) === null || _c === void 0 ? void 0 : _c.femalesWithBroodsCount) ||
109
- ((_d = unit.unitFact) === null || _d === void 0 ? void 0 : _d.juvenileCount)) {
110
- window.alert("Tätä lajiryhmää ei voi poistaa, koska siihen on kirjattu havaintoja.\n\n" +
111
- "This taxon set cannot be removed because it contains observations.\n\n" +
112
- "Den här artgruppen kan inte tas bort eftersom det finns observationer i den.");
113
- observationsExist = true;
114
- const updatedFormData = Object.assign(Object.assign({}, formData), { taxonCensus: [
115
- ...formData.taxonCensus,
116
- {
117
- censusTaxonSetID: deletedTaxonSetId,
118
- taxonCensusType: "MY.taxonCensusTypeCounted"
119
- }
120
- ] });
121
- this.selectedTaxonSets = [...this.selectedTaxonSets, deletedTaxonSetId];
122
- this.props.onChange(updatedFormData);
123
- return;
124
- }
110
+ ((_d = unit.unitFact) === null || _d === void 0 ? void 0 : _d.juvenileCount);
125
111
  });
126
112
  if (observationsExist) {
113
+ window.alert(translations === null || translations === void 0 ? void 0 : translations.TaxonSetDeletionFailed);
114
+ const restoredFormData = Object.assign(Object.assign({}, formData), { taxonCensus: [
115
+ ...formData.taxonCensus,
116
+ {
117
+ censusTaxonSetID: deletedTaxonSetId,
118
+ taxonCensusType: "MY.taxonCensusTypeCounted"
119
+ }
120
+ ] });
121
+ this.selectedTaxonSets = [...this.selectedTaxonSets, deletedTaxonSetId];
122
+ this.props.onChange(restoredFormData);
127
123
  return;
128
124
  }
129
125
  const updatedUnits = currentUnits.filter((unit) => {
@@ -167,6 +163,30 @@ let TaxonSetPopulatorField = class TaxonSetPopulatorField extends React.Componen
167
163
  getStateFromProps() {
168
164
  return { onChange: this.onChange };
169
165
  }
166
+ componentDidMount() {
167
+ return __awaiter(this, void 0, void 0, function* () {
168
+ var _a, _b, _c;
169
+ const taxonCensus = ((_a = this.props.formData) === null || _a === void 0 ? void 0 : _a.taxonCensus) || [];
170
+ this.selectedTaxonSets = taxonCensus.map((item) => item.censusTaxonSetID);
171
+ let resultTaxonSets = [];
172
+ if (this.selectedTaxonSets.length > 0) {
173
+ const allResults = yield Promise.all(this.selectedTaxonSets.map(taxonSetId => this.fetchTaxaFromSet(this.props, [taxonSetId])));
174
+ resultTaxonSets = allResults.flat().map((result) => ({
175
+ id: result.id,
176
+ taxonSets: result.taxonSets || []
177
+ }));
178
+ }
179
+ (_c = (_b = this.props.formData) === null || _b === void 0 ? void 0 : _b.units) === null || _c === void 0 ? void 0 : _c.forEach((unit) => {
180
+ var _a, _b, _c;
181
+ const taxonID = (_b = (_a = unit === null || unit === void 0 ? void 0 : unit.identifications) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.taxonID;
182
+ if (!taxonID) {
183
+ return;
184
+ }
185
+ const taxonSets = ((_c = resultTaxonSets === null || resultTaxonSets === void 0 ? void 0 : resultTaxonSets.find((result) => result.id === taxonID)) === null || _c === void 0 ? void 0 : _c.taxonSets) || [];
186
+ this.unitTaxonSets[taxonID] = taxonSets;
187
+ });
188
+ });
189
+ }
170
190
  fetchTaxaFromSet(props, taxonSets) {
171
191
  return __awaiter(this, void 0, void 0, function* () {
172
192
  var _a;
@@ -888,5 +888,10 @@
888
888
  "fi": "Samaa arvoa ei saa käyttää useammin kuin kerran",
889
889
  "en": "The same value may not be used more than once",
890
890
  "sv": "Samma värde får inte användas mer än en gång"
891
+ },
892
+ "taxonSetDeletionFailed": {
893
+ "fi": "Tätä lajiryhmää ei voi poistaa, koska siihen on kirjattu havaintoja.",
894
+ "en": "This taxon set cannot be removed because it contains observations.",
895
+ "sv": "Den här artgruppen kan inte tas bort eftersom det finns observationer i den."
891
896
  }
892
897
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luomus/laji-form",
3
- "version": "15.1.95",
3
+ "version": "15.1.97",
4
4
  "description": "React module capable of building dynamic forms from Laji form json schemas",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",