1
1
/**
2
2
* WordPress dependencies.
3
3
*/
4
- import domReady from '@wordpress/dom-ready' ;
5
- import { render } from '@wordpress/element' ;
4
+ import { render , useEffect , useRef , useState , WPElement } from '@wordpress/element' ;
6
5
7
6
/**
8
7
* Internal dependencies.
@@ -14,47 +13,110 @@ import {
14
13
apiEndpoint ,
15
14
apiHost ,
16
15
argsSchema ,
17
- authorization ,
18
- dateFormat ,
19
- statusLabels ,
20
- timeFormat ,
16
+ credentialsApiUrl ,
17
+ credentialsNonce ,
21
18
requestIdBase ,
22
19
} from './config' ;
23
20
21
+ /**
22
+ * Order search provider component.
23
+ *
24
+ * Bundles several provider components with authentication handling.
25
+ *
26
+ * @param {object } props Component props.
27
+ * @param {WPElement } props.children Component children.
28
+ * @returns {WPElement }
29
+ */
30
+ const AuthenticatedApiSearchProvider = ( { children } ) => {
31
+ /**
32
+ * State.
33
+ */
34
+ const [ credentials , setCredentials ] = useState ( null ) ;
35
+
36
+ /**
37
+ * Refs.
38
+ */
39
+ const hasRefreshed = useRef ( false ) ;
40
+
41
+ /**
42
+ * Refresh credentials on authentication errors.
43
+ *
44
+ * @returns {void }
45
+ */
46
+ const onAuthError = ( ) => {
47
+ if ( hasRefreshed . current ) {
48
+ setCredentials ( null ) ;
49
+ return ;
50
+ }
51
+
52
+ fetch ( credentialsApiUrl , {
53
+ headers : { 'X-WP-Nonce' : credentialsNonce } ,
54
+ method : 'POST' ,
55
+ } )
56
+ . then ( ( response ) => response . text ( ) )
57
+ . then ( setCredentials ) ;
58
+
59
+ hasRefreshed . current = true ;
60
+ } ;
61
+
62
+ /**
63
+ * Set credentials on initialization.
64
+ *
65
+ * @returns {void }
66
+ */
67
+ const onInit = ( ) => {
68
+ fetch ( credentialsApiUrl , {
69
+ headers : { 'X-WP-Nonce' : credentialsNonce } ,
70
+ } )
71
+ . then ( ( response ) => response . text ( ) )
72
+ . then ( setCredentials ) ;
73
+ } ;
74
+
75
+ /**
76
+ * Effects.
77
+ */
78
+ useEffect ( onInit , [ ] ) ;
79
+
80
+ /**
81
+ * Render.
82
+ */
83
+ return credentials ? (
84
+ < ApiSearchProvider
85
+ apiEndpoint = { apiEndpoint }
86
+ apiHost = { apiHost }
87
+ argsSchema = { argsSchema }
88
+ authorization = { `Basic ${ credentials } ` }
89
+ requestIdBase = { requestIdBase }
90
+ onAuthError = { onAuthError }
91
+ >
92
+ { children }
93
+ </ ApiSearchProvider >
94
+ ) : null ;
95
+ } ;
96
+
24
97
/**
25
98
* Initialize.
99
+ *
100
+ * @returns {void }
26
101
*/
27
- const init = ( ) => {
102
+ const init = async ( ) => {
28
103
const form = document . getElementById ( 'posts-filter' ) ;
29
104
const input = form . s ;
30
105
31
106
if ( ! input ) {
32
107
return ;
33
108
}
34
109
35
- const app = document . createElement ( 'div' ) ;
110
+ const el = document . createElement ( 'div' ) ;
36
111
37
- input . parentElement . appendChild ( app ) ;
112
+ input . parentElement . appendChild ( el ) ;
38
113
39
114
render (
40
- < ApiSearchProvider
41
- apiEndpoint = { apiEndpoint }
42
- apiHost = { apiHost }
43
- argsSchema = { argsSchema }
44
- authorization = { authorization }
45
- requestIdBase = { requestIdBase }
46
- defaultIsOn
47
- >
48
- < App
49
- adminUrl = { adminUrl }
50
- dateFormat = { dateFormat }
51
- input = { input }
52
- statusLabels = { statusLabels }
53
- timeFormat = { timeFormat }
54
- />
55
- </ ApiSearchProvider > ,
56
- app ,
115
+ < AuthenticatedApiSearchProvider >
116
+ < App adminUrl = { adminUrl } input = { input } />
117
+ </ AuthenticatedApiSearchProvider > ,
118
+ el ,
57
119
) ;
58
120
} ;
59
121
60
- domReady ( init ) ;
122
+ init ( ) ;
0 commit comments