99import  React ,  {  useState  }  from  'react' ; 
1010import  PropTypes  from  'prop-types' ; 
1111
12+ import  axios  from  'axios' ; 
13+ 
14+ import  _get  from  'lodash/get' ; 
1215import  _split  from  'lodash/split' ; 
1316import  _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" >  </ 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" >  </ 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' , 
0 commit comments