Skip to content

Commit 219a0db

Browse files
authored
Merge pull request #32 from M3nin0/hotfix/geographic-identifier-selection-error
form: fixing the names loading of the Geographic identifier items
2 parents 0a38a4e + fbc623a commit 219a0db

File tree

4 files changed

+85
-28
lines changed

4 files changed

+85
-28
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@geo-knowledge-hub/invenio-geographic-components-react",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"main": "dist/cjs/index.js",
55
"browser": "dist/cjs/index.js",
66
"module": "dist/esm/index.js",

src/lib/components/form/fields/GeographicIdentifiersField.js

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import React, { useState } from 'react';
1010
import PropTypes from 'prop-types';
1111

12+
import axios from 'axios';
13+
14+
import _get from 'lodash/get';
1215
import _split from 'lodash/split';
1316
import _capitalize from 'lodash/capitalize';
1417

@@ -48,10 +51,14 @@ export const GeographicIdentifiersField = ({
4851
noQueryMessage,
4952
suggestionAPIUrl,
5053
}) => {
54+
// States
55+
const [initialValuesLoaded, setInitialValuesLoaded] = useState(false);
56+
5157
const [fieldState, setFieldState] = useState({
5258
limitTo: limitOptions[0].value,
5359
});
5460

61+
// Auxiliary functions
5562
const prepareSuggest = (searchQuery) => {
5663
const limitTo = fieldState.limitTo;
5764
const prefix = limitTo === 'all' ? '' : `${limitTo}::`;
@@ -61,7 +68,7 @@ export const GeographicIdentifiersField = ({
6168

6269
const serializeIdentifiers = (identifiers) =>
6370
identifiers.map((identifier) => {
64-
const scheme = _split(identifier.id, '::', 1).at(0); // pattern from geoidentifiers
71+
const scheme = _split(identifier.id, '::', 1).at(0); // Pattern from GeoIdentifiers
6572
const schemeText = scheme ? `(${_capitalize(scheme)})` : '';
6673

6774
return {
@@ -74,6 +81,41 @@ export const GeographicIdentifiersField = ({
7481
};
7582
});
7683

84+
// Function to transform the Initial Values in a format valid for the Component.
85+
// By now, we are using this "basic" approach, where we request the API many times.
86+
// This is temporary and in the future can be revised.
87+
const transformInitialValues = (initialValues, setFieldValue) => {
88+
if (initialValues.length !== 0 && !initialValuesLoaded) {
89+
Promise.all(
90+
initialValues.map(async (identifier) => {
91+
const identifierValue = _get(identifier, 'identifier');
92+
93+
if (identifierValue) {
94+
// getting data from the identifier api
95+
const identifierApi = `${suggestionAPIUrl}/${identifierValue}`;
96+
const result = await axios.get(identifierApi);
97+
98+
// extracting the values
99+
if (result.status === 200) {
100+
return result.data;
101+
}
102+
}
103+
104+
return identifier;
105+
})
106+
).then((res) => {
107+
// Saving the transformed values
108+
setFieldValue(serializeIdentifiers(res));
109+
110+
// Enable the component
111+
setInitialValuesLoaded(true);
112+
});
113+
} else {
114+
// Enable the component
115+
setInitialValuesLoaded(true);
116+
}
117+
};
118+
77119
return (
78120
<GroupField className={'main-group-field'}>
79121
<Form.Field width={5}>
@@ -100,29 +142,42 @@ export const GeographicIdentifiersField = ({
100142
</GroupField>
101143
</Form.Field>
102144
<Field name={fieldPath}>
103-
{({ form: { values } }) => {
145+
{({ form: { values, setFieldValue } }) => {
146+
// Looking for initial values
147+
transformInitialValues(getIn(values, fieldPath, []), (value) => {
148+
setFieldValue(fieldPath, value);
149+
});
150+
104151
return (
105-
<RemoteSelectField
106-
clearable={clearable}
107-
fieldPath={fieldPath}
108-
initialSuggestions={getIn(values, fieldPath, [])}
109-
multiple={multiple}
110-
noQueryMessage={noQueryMessage}
111-
placeholder={placeholder}
112-
preSearchChange={prepareSuggest}
113-
required={required}
114-
serializeSuggestions={serializeIdentifiers}
115-
suggestionAPIUrl={suggestionAPIUrl}
116-
onValueChange={({ formikProps }, selectedSuggestions) => {
117-
formikProps.form.setFieldValue(fieldPath, selectedSuggestions);
118-
}}
119-
value={getIn(values, fieldPath, []).map((val) => val.name)}
120-
label={
121-
<label className="mobile-hidden">&nbsp;</label>
122-
} /** For alignment purposes */
123-
allowAdditions={false}
124-
width={11}
125-
/>
152+
<>
153+
{/* Presenting the component after define the Initial values. */}
154+
{initialValuesLoaded ? (
155+
<RemoteSelectField
156+
clearable={clearable}
157+
fieldPath={fieldPath}
158+
initialSuggestions={getIn(values, fieldPath, [])}
159+
multiple={multiple}
160+
noQueryMessage={noQueryMessage}
161+
placeholder={placeholder}
162+
preSearchChange={prepareSuggest}
163+
required={required}
164+
serializeSuggestions={serializeIdentifiers}
165+
suggestionAPIUrl={suggestionAPIUrl}
166+
onValueChange={({ formikProps }, selectedSuggestions) => {
167+
formikProps.form.setFieldValue(
168+
fieldPath,
169+
selectedSuggestions
170+
);
171+
}}
172+
value={getIn(values, fieldPath, []).map((val) => val.name)}
173+
label={
174+
<label className="mobile-hidden">&nbsp;</label>
175+
} /** For alignment purposes */
176+
allowAdditions={false}
177+
width={11}
178+
/>
179+
) : null}
180+
</>
126181
);
127182
}}
128183
</Field>
@@ -159,8 +214,8 @@ GeographicIdentifiersField.defaultProps = {
159214
fieldPath: 'identifiers',
160215
limitOptions: [
161216
{
162-
text: 'Geonames',
163-
value: 'GEONAMES', // Available on: Invenio Geographic Identifiers.
217+
text: 'GeoNames',
218+
value: 'geonames', // Available on: Invenio Geographic Identifiers.
164219
},
165220
{
166221
text: 'All',

src/lib/contrib/deposit/fields/LocationsFieldSerializer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* under the terms of the MIT License; see LICENSE file for more details.
77
*/
88

9+
import axios from 'axios';
10+
911
import _get from 'lodash/get';
1012
import _set from 'lodash/set';
1113
import _cloneDeep from 'lodash/cloneDeep';
@@ -40,7 +42,7 @@ export class LocationsFieldSerializer {
4042
const identifiers = _get(location, 'identifiers', []).map(
4143
(identifier) => ({
4244
scheme: identifier.scheme,
43-
identifier: identifier.id,
45+
identifier: identifier.identifier || identifier.id,
4446
})
4547
);
4648

0 commit comments

Comments
 (0)