1
1
import { produce } from 'immer' ;
2
- import React , { useContext , useMemo , useState } from 'react' ;
2
+ import React , { useContext , useState } from 'react' ;
3
3
import { useIntl } from 'react-intl' ;
4
4
import useAsync from 'react-use/esm/useAsync' ;
5
5
6
- import Loader from 'components/admin/Loader' ;
7
6
import { CustomFieldTemplate } from 'components/admin/RJSFWrapper' ;
8
7
import {
9
8
REGISTRATION_OBJECTTYPES_ENDPOINT ,
@@ -26,6 +25,15 @@ const Wrapper = ({children}) => (
26
25
</ form >
27
26
) ;
28
27
28
+ const getObjecttypeVersionsEndpoint = objecttype => {
29
+ const bits = [
30
+ '/api/v2/registration/plugins/objects-api/object-types' ,
31
+ encodeURIComponent ( objecttype . uuid ) ,
32
+ 'versions' ,
33
+ ] ;
34
+ return bits . join ( '/' ) ;
35
+ } ;
36
+
29
37
const ObjectsApiOptionsFormFields = ( { index, name, schema, formData, onChange} ) => {
30
38
const intl = useIntl ( ) ;
31
39
const validationErrors = useContext ( ValidationErrorContext ) ;
@@ -48,10 +56,8 @@ const ObjectsApiOptionsFormFields = ({index, name, schema, formData, onChange})
48
56
49
57
// For existing registration options where the URL might not be valid
50
58
// will default to a textinput in that case
51
- const unknownObjecttype = useMemo (
52
- ( ) => objecttype && ! availableObjecttypes . map ( o => o . url ) . includes ( objecttype ) ,
53
- [ availableObjecttypes , objecttype ]
54
- ) ;
59
+ const isUnknownObjecttype =
60
+ objecttype && availableObjecttypes . find ( o => o . url === objecttype ) === undefined ;
55
61
56
62
const onFieldChange = event => {
57
63
const { name, value} = event . target ;
@@ -78,25 +84,24 @@ const ObjectsApiOptionsFormFields = ({index, name, schema, formData, onChange})
78
84
79
85
const { loading : objecttypeVersionLoading , error : objecttypeVersionLoadingError } =
80
86
useAsync ( async ( ) => {
81
- if ( ! objecttype ) return ;
87
+ if ( ! objecttype ) {
88
+ setAvailableObjecttypeVersions ( [ ] ) ;
89
+ return ;
90
+ }
82
91
83
- const response = await get (
84
- REGISTRATION_OBJECTTYPE_VERSIONS_ENDPOINT . replace (
85
- '{objecttypeUuid}' ,
86
- availableObjecttypes . find ( o => o . url === objecttype ) . uuid
87
- )
92
+ const url = getObjecttypeVersionsEndpoint (
93
+ availableObjecttypes . find ( o => o . url === objecttype )
88
94
) ;
95
+ const response = await get ( url ) ;
96
+
89
97
if ( ! response . ok ) {
90
98
throw new Error ( 'Error when loading objecttype versions' ) ;
91
99
}
92
100
93
101
setAvailableObjecttypeVersions ( response . data ) ;
94
102
} , [ objecttype ] ) ;
95
103
96
- const hasApiError = useMemo (
97
- ( ) => objecttypesLoadingError || objecttypeVersionLoadingError ,
98
- [ objecttypesLoadingError , objecttypeVersionLoadingError ]
99
- ) ;
104
+ const hasApiError = objecttypesLoadingError || objecttypeVersionLoadingError ;
100
105
101
106
return (
102
107
< Wrapper >
@@ -115,13 +120,13 @@ const ObjectsApiOptionsFormFields = ({index, name, schema, formData, onChange})
115
120
errors = { buildErrorsComponent ( 'objecttype' ) }
116
121
displayLabel
117
122
>
118
- { unknownObjecttype || hasApiError ? (
123
+ { isUnknownObjecttype || hasApiError ? (
119
124
< TextInput
120
125
id = "root_objecttype"
121
126
name = "objecttype"
122
127
value = { objecttype }
123
128
onChange = { onFieldChange }
124
- allowBlank = { true }
129
+ allowBlank
125
130
/>
126
131
) : (
127
132
< Select
@@ -130,11 +135,10 @@ const ObjectsApiOptionsFormFields = ({index, name, schema, formData, onChange})
130
135
choices = {
131
136
objecttypesLoading
132
137
? LOADING_OPTION
133
- : availableObjecttypes . map ( ot => [ ot . url , `${ ot . name } ( ${ ot . uuid } ) ` ] )
138
+ : availableObjecttypes . map ( ot => [ ot . url , `${ ot . name } ` ] )
134
139
}
135
140
value = { objecttype }
136
141
onChange = { onFieldChange }
137
- allowBlank = { true }
138
142
/>
139
143
) }
140
144
</ CustomFieldTemplate >
@@ -152,7 +156,7 @@ const ObjectsApiOptionsFormFields = ({index, name, schema, formData, onChange})
152
156
errors = { buildErrorsComponent ( 'objecttypeVersion' ) }
153
157
displayLabel
154
158
>
155
- { unknownObjecttype || hasApiError ? (
159
+ { isUnknownObjecttype || hasApiError ? (
156
160
< NumberInput
157
161
id = "root_objecttypeVersion"
158
162
name = "objecttypeVersion"
@@ -167,11 +171,12 @@ const ObjectsApiOptionsFormFields = ({index, name, schema, formData, onChange})
167
171
choices = {
168
172
objecttypeVersionLoading
169
173
? LOADING_OPTION
170
- : availableObjecttypeVersions . map ( otv => [ otv . version , otv . version ] )
174
+ : availableObjecttypeVersions
175
+ . sort ( ( v1 , v2 ) => v2 . version - v1 . version )
176
+ . map ( otv => [ otv . version , otv . version ] )
171
177
}
172
178
value = { objecttypeVersion }
173
179
onChange = { onFieldChange }
174
- allowBlank = { true }
175
180
/>
176
181
) }
177
182
</ CustomFieldTemplate >
0 commit comments