@@ -102,21 +102,27 @@ export const Page = function <
102
102
}
103
103
104
104
componentDidMount ( ) {
105
- if ( ! condition ( this . context ) ) {
106
- this . setState ( { loading : false , unauthorised : true } ) ;
107
- } else {
108
- this . query ( ) ;
109
- }
110
-
111
- this . setState ( { alerts : this . context . alerts || [ ] } ) ;
112
- this . context . setAlerts ( [ ] ) ;
113
-
114
- if ( didMount ) {
115
- didMount ( {
116
- context : this . context ,
117
- addAlert : ( alert ) => this . addAlert ( alert ) ,
118
- } ) ;
119
- }
105
+ // condition check after query, for object permissions
106
+ this . query ( ) . then ( ( item ) => {
107
+ const actionContext = {
108
+ ...this . context ,
109
+ hasObjectPermission : ( permission ) =>
110
+ item ?. my_permissions ?. includes ?.( permission ) ,
111
+ } ;
112
+ if ( ! condition ( actionContext ) ) {
113
+ this . setState ( { loading : false , unauthorised : true } ) ;
114
+ }
115
+
116
+ this . setState ( { alerts : this . context . alerts || [ ] } ) ;
117
+ this . context . setAlerts ( [ ] ) ;
118
+
119
+ if ( didMount ) {
120
+ didMount ( {
121
+ context : this . context ,
122
+ addAlert : ( alert ) => this . addAlert ( alert ) ,
123
+ } ) ;
124
+ }
125
+ } ) ;
120
126
}
121
127
122
128
render ( ) {
@@ -193,29 +199,33 @@ export const Page = function <
193
199
194
200
if ( ! name ) {
195
201
this . setState ( { loading : false } ) ;
196
- return ;
202
+ return Promise . resolve ( null ) ;
197
203
}
198
204
199
- this . setState ( { loading : true } , ( ) => {
200
- query ( { name } )
201
- . then ( ( item ) => {
202
- this . setState ( {
203
- item,
204
- loading : false ,
205
- } ) ;
206
- } )
207
- . catch ( ( e ) => {
208
- const { status, statusText } = e . response ;
209
- this . setState ( {
210
- loading : false ,
211
- item : null ,
212
- } ) ;
213
- this . addAlert ( {
214
- title : errorTitle ,
215
- variant : 'danger' ,
216
- description : errorMessage ( status , statusText ) ,
205
+ return new Promise ( ( resolve , reject ) => {
206
+ this . setState ( { loading : true } , ( ) => {
207
+ query ( { name } )
208
+ . then ( ( item ) => {
209
+ this . setState ( {
210
+ item,
211
+ loading : false ,
212
+ } ) ;
213
+ resolve ( item ) ;
214
+ } )
215
+ . catch ( ( e ) => {
216
+ const { status, statusText } = e . response ;
217
+ this . setState ( {
218
+ loading : false ,
219
+ item : null ,
220
+ } ) ;
221
+ this . addAlert ( {
222
+ title : errorTitle ,
223
+ variant : 'danger' ,
224
+ description : errorMessage ( status , statusText ) ,
225
+ } ) ;
226
+ reject ( ) ;
217
227
} ) ;
218
- } ) ;
228
+ } ) ;
219
229
} ) ;
220
230
}
221
231
0 commit comments