1
- import { InspectorControls , useBlockProps } from '@wordpress/block-editor' ;
1
+ import { BlockPattern , InspectorControls , useBlockProps } from '@wordpress/block-editor' ;
2
2
import { BlockEditProps } from '@wordpress/blocks' ;
3
3
import { Spinner } from '@wordpress/components' ;
4
- import { useEffect , useState } from '@wordpress/element' ;
4
+ import { useState } from '@wordpress/element' ;
5
5
6
6
import { InnerBlocks } from '@/blocks/remote-data-container/components/InnerBlocks' ;
7
7
import { DataPanel } from '@/blocks/remote-data-container/components/panels/DataPanel' ;
@@ -32,120 +32,97 @@ export function Edit( props: BlockEditProps< RemoteDataBlockAttributes > ) {
32
32
const {
33
33
getInnerBlocks,
34
34
getSupportedPatterns,
35
+ innerBlocksPattern,
35
36
insertPatternBlocks,
36
- markReadyForInsertion,
37
- resetReadyForInsertion,
38
- showPatternSelection,
37
+ resetInnerBlocks,
39
38
} = usePatterns ( blockName , rootClientId ) ;
40
- const { execute } = useRemoteData ( blockName , DISPLAY_QUERY_KEY ) ;
41
- const [ initialLoad , setInitialLoad ] = useState < boolean > ( true ) ;
42
-
43
- function fetchRemoteData ( input : RemoteDataQueryInput , insertBlocks = true ) {
44
- execute ( input , true )
45
- . then ( remoteData => {
46
- if ( remoteData ) {
47
- updateRemoteData (
48
- {
49
- enabledOverrides : props . attributes . remoteData ?. enabledOverrides ?? [ ] ,
50
- ...remoteData ,
51
- } ,
52
- insertBlocks
53
- ) ;
54
- }
55
- } )
56
- . catch ( ( ) => { } )
57
- . finally ( ( ) => {
58
- setInitialLoad ( false ) ;
59
- } ) ;
39
+
40
+ const { data, fetch, loading, reset } = useRemoteData ( {
41
+ blockName,
42
+ externallyManagedRemoteData : props . attributes . remoteData ,
43
+ externallyManagedUpdateRemoteData : updateRemoteData ,
44
+ queryKey : DISPLAY_QUERY_KEY ,
45
+ } ) ;
46
+
47
+ const [ showPatternSelection , setShowPatternSelection ] = useState < boolean > ( false ) ;
48
+
49
+ function refreshRemoteData ( ) : void {
50
+ void fetch ( props . attributes . remoteData ?. queryInput ?? { } ) ;
60
51
}
61
52
62
- // Update the remote data in the block attributes, which is passed via context
63
- // to children blocks. If this is the initial load of remote data, show the
64
- // pattern selection modal so that we can insert the blocks from the pattern.
65
- function updateRemoteData ( remoteData : RemoteData , insertBlocks = false ) {
66
- if ( hasRemoteDataChanged ( props . attributes . remoteData , remoteData ) ) {
67
- props . setAttributes ( { remoteData } ) ;
68
- }
53
+ function resetPatternSelection ( ) : void {
54
+ resetInnerBlocks ( ) ;
55
+ setShowPatternSelection ( false ) ;
56
+ }
69
57
70
- if ( insertBlocks ) {
71
- markReadyForInsertion ( ) ;
72
- }
58
+ function resetRemoteData ( ) : void {
59
+ reset ( ) ;
60
+ resetPatternSelection ( ) ;
73
61
}
74
62
75
- const hasInputVariables = Boolean (
76
- blockConfig . selectors . find ( selector => selector . query_key === DISPLAY_QUERY_KEY ) ?. inputs
77
- ?. length
78
- ) ;
63
+ function onSelectPattern ( pattern : BlockPattern ) : void {
64
+ insertPatternBlocks ( pattern ) ;
65
+ setShowPatternSelection ( false ) ;
66
+ }
79
67
80
- function refreshRemoteData ( ) {
81
- if ( ! props . attributes . remoteData ?. queryInput ) {
82
- if ( hasInputVariables ) {
68
+ function onSelectRemoteData ( queryInput : RemoteDataQueryInput ) : void {
69
+ void fetch ( queryInput ) . then ( ( ) => {
70
+ if ( innerBlocksPattern ) {
71
+ insertPatternBlocks ( innerBlocksPattern ) ;
83
72
return ;
84
73
}
85
74
86
- fetchRemoteData ( { } , true ) ;
87
- } else {
88
- fetchRemoteData ( props . attributes . remoteData . queryInput , false ) ;
89
- }
75
+ setShowPatternSelection ( true ) ;
76
+ } ) ;
90
77
}
91
78
92
- function resetRemoteData ( ) {
93
- props . setAttributes ( { remoteData : undefined } ) ;
94
- resetReadyForInsertion ( ) ;
79
+ function updateRemoteData ( remoteData ?: RemoteData ) : void {
80
+ if ( hasRemoteDataChanged ( props . attributes . remoteData , remoteData ) ) {
81
+ props . setAttributes ( { remoteData } ) ;
82
+ }
95
83
}
96
84
97
- useEffect ( ( ) => {
98
- // Refetch remote data for initial load
99
- refreshRemoteData ( ) ;
100
- } , [ ] ) ;
101
-
102
85
// No remote data has been selected yet, show a placeholder.
103
- if ( ! props . attributes . remoteData ) {
104
- if ( ! hasInputVariables ) {
105
- return null ;
106
- }
107
-
86
+ if ( ! data ) {
108
87
return (
109
88
< div { ...blockProps } >
110
- < Placeholder blockConfig = { blockConfig } fetchRemoteData = { fetchRemoteData } />
89
+ < Placeholder blockConfig = { blockConfig } onSelect = { onSelectRemoteData } />
111
90
</ div >
112
91
) ;
113
92
}
114
93
115
94
if ( showPatternSelection ) {
116
- const supportedPatterns = getSupportedPatterns ( props . attributes . remoteData ?. results [ 0 ] ) ;
117
-
118
- if ( supportedPatterns . length ) {
119
- return (
120
- < div { ...blockProps } >
121
- < PatternSelection
122
- blockName = { blockName }
123
- insertPatternBlocks = { insertPatternBlocks }
124
- onCancel = { resetReadyForInsertion }
125
- supportedPatterns = { supportedPatterns }
126
- />
127
- </ div >
128
- ) ;
129
- }
95
+ const supportedPatterns = getSupportedPatterns ( data . results [ 0 ] ) ;
96
+
97
+ return (
98
+ < div { ...blockProps } >
99
+ < PatternSelection
100
+ blockName = { blockName }
101
+ onCancel = { resetPatternSelection }
102
+ onSelectPattern = { onSelectPattern }
103
+ supportedPatterns = { supportedPatterns }
104
+ />
105
+ </ div >
106
+ ) ;
130
107
}
131
108
132
109
return (
133
110
< >
134
111
< InspectorControls >
135
112
< OverridesPanel
136
113
blockConfig = { blockConfig }
137
- remoteData = { props . attributes . remoteData }
114
+ remoteData = { data }
138
115
updateRemoteData = { updateRemoteData }
139
116
/>
140
117
< DataPanel
141
118
refreshRemoteData = { refreshRemoteData }
142
- remoteData = { props . attributes . remoteData }
119
+ remoteData = { data }
143
120
resetRemoteData = { resetRemoteData }
144
121
/>
145
122
</ InspectorControls >
146
123
147
124
< div { ...blockProps } >
148
- { initialLoad && (
125
+ { loading && (
149
126
< div className = "remote-data-blocks-loading-overlay" >
150
127
< Spinner
151
128
style = { {
@@ -158,7 +135,7 @@ export function Edit( props: BlockEditProps< RemoteDataBlockAttributes > ) {
158
135
< InnerBlocks
159
136
blockConfig = { blockConfig }
160
137
getInnerBlocks = { getInnerBlocks }
161
- remoteData = { props . attributes . remoteData }
138
+ remoteData = { data }
162
139
/>
163
140
</ div >
164
141
</ >
0 commit comments