@luomus/laji-form 15.1.94 → 15.1.96

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.
@@ -41,10 +41,12 @@ export default class TaxonSetPopulatorField extends React.Component<FieldProps>
41
41
  formData: PropTypes.Validator<object>;
42
42
  };
43
43
  static getName(): string;
44
- taxonSets: any[];
44
+ selectedTaxonSets: string[];
45
+ unitTaxonSets: Record<string, string[]>;
45
46
  getStateFromProps(): {
46
47
  onChange: (formData: any) => Promise<void>;
47
48
  };
49
+ componentDidMount(): Promise<void>;
48
50
  onChange: (formData: any) => Promise<void>;
49
51
  private fetchTaxaFromSet;
50
52
  }
@@ -80,21 +80,24 @@ const propsPropType = PropTypes.shape({
80
80
  let TaxonSetPopulatorField = class TaxonSetPopulatorField extends React.Component {
81
81
  constructor() {
82
82
  super(...arguments);
83
- this.taxonSets = [];
83
+ this.selectedTaxonSets = [];
84
+ this.unitTaxonSets = {};
84
85
  this.onChange = (formData) => __awaiter(this, void 0, void 0, function* () {
85
86
  var _a, _b, _c;
86
- const previousTaxonSets = this.taxonSets;
87
+ const { translations } = this.props.formContext;
88
+ const previousTaxonSets = this.selectedTaxonSets;
87
89
  const currentTaxonSets = ((_a = formData === null || formData === void 0 ? void 0 : formData.taxonCensus) === null || _a === void 0 ? void 0 : _a.map((item) => item.censusTaxonSetID)) || [];
88
90
  const deletedTaxonSets = previousTaxonSets.filter((item) => !currentTaxonSets.includes(item));
89
91
  const addedTaxonSets = currentTaxonSets.filter((item) => !previousTaxonSets.includes(item));
90
- this.taxonSets = ((_b = formData === null || formData === void 0 ? void 0 : formData.taxonCensus) === null || _b === void 0 ? void 0 : _b.map((item) => item.censusTaxonSetID)) || [];
92
+ this.selectedTaxonSets = ((_b = formData === null || formData === void 0 ? void 0 : formData.taxonCensus) === null || _b === void 0 ? void 0 : _b.map((item) => item.censusTaxonSetID)) || [];
91
93
  if (deletedTaxonSets.length > 0) {
92
94
  deletedTaxonSets.map((deletedTaxonSetId) => {
93
95
  var _a;
94
96
  const currentUnits = Array.isArray((_a = this.props.formData) === null || _a === void 0 ? void 0 : _a.units) ? this.props.formData.units : [];
95
97
  let observationsExist = false;
96
98
  const deletedTaxonSetUnits = currentUnits.filter((unit) => {
97
- return unit.taxonSets && unit.taxonSets.includes(deletedTaxonSetId);
99
+ return this.unitTaxonSets[unit.identifications[0].taxonID]
100
+ && this.unitTaxonSets[unit.identifications[0].taxonID].includes(deletedTaxonSetId);
98
101
  });
99
102
  deletedTaxonSetUnits.map((unit) => {
100
103
  var _a, _b, _c, _d;
@@ -105,9 +108,7 @@ let TaxonSetPopulatorField = class TaxonSetPopulatorField extends React.Componen
105
108
  ((_b = unit.unitFact) === null || _b === void 0 ? void 0 : _b.broodCount) ||
106
109
  ((_c = unit.unitFact) === null || _c === void 0 ? void 0 : _c.femalesWithBroodsCount) ||
107
110
  ((_d = unit.unitFact) === null || _d === void 0 ? void 0 : _d.juvenileCount)) {
108
- window.alert("Tätä lajiryhmää ei voi poistaa, koska siihen on kirjattu havaintoja.\n\n" +
109
- "This taxon set cannot be removed because it contains observations.\n\n" +
110
- "Den här artgruppen kan inte tas bort eftersom det finns observationer i den.");
111
+ window.alert(translations === null || translations === void 0 ? void 0 : translations.TaxonSetDeletionFailed);
111
112
  observationsExist = true;
112
113
  const updatedFormData = Object.assign(Object.assign({}, formData), { taxonCensus: [
113
114
  ...formData.taxonCensus,
@@ -116,7 +117,7 @@ let TaxonSetPopulatorField = class TaxonSetPopulatorField extends React.Componen
116
117
  taxonCensusType: "MY.taxonCensusTypeCounted"
117
118
  }
118
119
  ] });
119
- this.taxonSets = [...this.taxonSets, deletedTaxonSetId];
120
+ this.selectedTaxonSets = [...this.selectedTaxonSets, deletedTaxonSetId];
120
121
  this.props.onChange(updatedFormData);
121
122
  return;
122
123
  }
@@ -125,7 +126,8 @@ let TaxonSetPopulatorField = class TaxonSetPopulatorField extends React.Componen
125
126
  return;
126
127
  }
127
128
  const updatedUnits = currentUnits.filter((unit) => {
128
- return !unit.taxonSets || !unit.taxonSets.includes(deletedTaxonSetId);
129
+ return !this.unitTaxonSets[unit.identifications[0].taxonID]
130
+ || !this.unitTaxonSets[unit.identifications[0].taxonID].includes(deletedTaxonSetId);
129
131
  });
130
132
  const updatedFormData = Object.assign(Object.assign({}, formData), { units: updatedUnits });
131
133
  this.props.onChange(updatedFormData);
@@ -135,21 +137,22 @@ let TaxonSetPopulatorField = class TaxonSetPopulatorField extends React.Componen
135
137
  const currentUnits = Array.isArray((_c = this.props.formData) === null || _c === void 0 ? void 0 : _c.units) ? this.props.formData.units : [];
136
138
  // if current units include any units with taxon sets that are being added, return to avoid duplicates
137
139
  const duplicateUnits = currentUnits.filter((unit) => {
138
- return unit.taxonSets && unit.taxonSets.some((taxonSetId) => addedTaxonSets.includes(taxonSetId));
140
+ return this.unitTaxonSets[unit.identifications[0].taxonID]
141
+ && this.unitTaxonSets[unit.identifications[0].taxonID].some((taxonSetId) => addedTaxonSets.includes(taxonSetId));
139
142
  });
140
143
  if (duplicateUnits.length > 0) {
141
144
  return;
142
145
  }
143
146
  const results = yield this.fetchTaxaFromSet(this.props, addedTaxonSets);
144
147
  const newUnits = results.map((result) => {
148
+ this.unitTaxonSets[result.id] = result.taxonSets || [];
145
149
  return {
146
150
  identifications: [{
147
151
  taxon: result.scientificName,
148
152
  taxonID: result.id,
149
153
  taxonVerbatim: result.vernacularName
150
154
  }],
151
- informalTaxonGroups: result.informalTaxonGroups || [],
152
- taxonSets: result.taxonSets || []
155
+ informalTaxonGroups: result.informalTaxonGroups || []
153
156
  };
154
157
  });
155
158
  const updatedFormData = Object.assign(Object.assign({}, formData), { units: [...currentUnits, ...newUnits] });
@@ -163,6 +166,30 @@ let TaxonSetPopulatorField = class TaxonSetPopulatorField extends React.Componen
163
166
  getStateFromProps() {
164
167
  return { onChange: this.onChange };
165
168
  }
169
+ componentDidMount() {
170
+ return __awaiter(this, void 0, void 0, function* () {
171
+ var _a, _b, _c;
172
+ const taxonCensus = ((_a = this.props.formData) === null || _a === void 0 ? void 0 : _a.taxonCensus) || [];
173
+ this.selectedTaxonSets = taxonCensus.map((item) => item.censusTaxonSetID);
174
+ let resultTaxonSets = [];
175
+ if (this.selectedTaxonSets.length > 0) {
176
+ const allResults = yield Promise.all(this.selectedTaxonSets.map(taxonSetId => this.fetchTaxaFromSet(this.props, [taxonSetId])));
177
+ resultTaxonSets = allResults.flat().map((result) => ({
178
+ id: result.id,
179
+ taxonSets: result.taxonSets || []
180
+ }));
181
+ }
182
+ (_c = (_b = this.props.formData) === null || _b === void 0 ? void 0 : _b.units) === null || _c === void 0 ? void 0 : _c.forEach((unit) => {
183
+ var _a, _b, _c;
184
+ 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;
185
+ if (!taxonID) {
186
+ return;
187
+ }
188
+ const taxonSets = ((_c = resultTaxonSets === null || resultTaxonSets === void 0 ? void 0 : resultTaxonSets.find((result) => result.id === taxonID)) === null || _c === void 0 ? void 0 : _c.taxonSets) || [];
189
+ this.unitTaxonSets[taxonID] = taxonSets;
190
+ });
191
+ });
192
+ }
166
193
  fetchTaxaFromSet(props, taxonSets) {
167
194
  return __awaiter(this, void 0, void 0, function* () {
168
195
  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.94",
3
+ "version": "15.1.96",
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",