@@ -29,13 +29,40 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
29
29
setQuery ,
30
30
] = useState ( "" ) ;
31
31
32
+ const [
33
+ page ,
34
+ setPage ,
35
+ ] = useState < number > ( 0 ) ;
36
+
37
+ const [
38
+ canLoadMore ,
39
+ setCanLoadMore ,
40
+ ] = useState < boolean > ( true ) ;
41
+
42
+ const [
43
+ context ,
44
+ setContext ,
45
+ ] = useState < never | undefined > ( undefined ) ;
46
+
47
+ const [
48
+ pageable ,
49
+ setPageable ,
50
+ ] = useState ( {
51
+ page : 0 ,
52
+ prevContext : { } ,
53
+ data : [ ] ,
54
+ values : new Set ( ) ,
55
+ } )
56
+
32
57
const configuredPropsUpTo : Record < string , unknown > = { } ;
33
58
for ( let i = 0 ; i < idx ; i ++ ) {
34
59
const prop = configurableProps [ i ] ;
35
60
configuredPropsUpTo [ prop . name ] = configuredProps [ prop . name ] ;
36
61
}
37
62
const componentConfigureInput : ComponentConfigureOpts = {
38
63
userId,
64
+ page,
65
+ prevContext : context ,
39
66
componentId : component . key ,
40
67
propName : prop . name ,
41
68
configuredProps : configuredPropsUpTo ,
@@ -55,9 +82,18 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
55
82
setError ,
56
83
] = useState < { name : string ; message : string ; } > ( ) ;
57
84
85
+ const onLoadMore = ( ) => {
86
+ setPage ( pageable . page )
87
+ setContext ( pageable . prevContext )
88
+ setPageable ( {
89
+ ...pageable ,
90
+ prevContext : { } ,
91
+ } )
92
+ }
93
+
58
94
// TODO handle error!
59
95
const {
60
- isFetching, data , refetch,
96
+ isFetching, refetch,
61
97
} = useQuery ( {
62
98
queryKey : [
63
99
"componentConfigure" ,
@@ -67,11 +103,11 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
67
103
setError ( undefined ) ;
68
104
const res = await client . componentConfigure ( componentConfigureInput ) ;
69
105
70
- // console.log("res", res)
71
106
// XXX look at errors in response here too
72
107
const {
73
108
options, stringOptions, errors,
74
109
} = res ;
110
+
75
111
if ( errors ?. length ) {
76
112
// TODO field context setError? (for validity, etc.)
77
113
try {
@@ -84,8 +120,9 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
84
120
}
85
121
return [ ] ;
86
122
}
123
+ let _options = [ ]
87
124
if ( options ?. length ) {
88
- return options ;
125
+ _options = options ;
89
126
}
90
127
if ( stringOptions ?. length ) {
91
128
const options = [ ] ;
@@ -95,13 +132,45 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
95
132
value : stringOption ,
96
133
} ) ;
97
134
}
98
- return options ;
135
+ _options = options ;
136
+ }
137
+
138
+ const newOptions = [ ]
139
+ const allValues = new Set ( pageable . values )
140
+ for ( const o of _options || [ ] ) {
141
+ const value = typeof o === "string"
142
+ ? o
143
+ : o . value
144
+ if ( allValues . has ( value ) ) {
145
+ continue
146
+ }
147
+ allValues . add ( value )
148
+ newOptions . push ( o )
149
+ }
150
+ let data = pageable . data
151
+ if ( newOptions . length ) {
152
+ data = [
153
+ ...pageable . data ,
154
+ ...newOptions ,
155
+ ]
156
+ setPageable ( {
157
+ page : page + 1 ,
158
+ prevContext : res . context ,
159
+ data,
160
+ values : allValues ,
161
+ } )
162
+ } else {
163
+ setCanLoadMore ( false )
99
164
}
100
- return [ ] ;
165
+ return data ;
101
166
} ,
102
167
enabled : ! ! queryEnabled ,
103
168
} ) ;
104
169
170
+ const showLoadMoreButton = ( ) => {
171
+ return ! isFetching && ! error && canLoadMore
172
+ }
173
+
105
174
// TODO show error in different spot!
106
175
const placeholder = error
107
176
? error . message
@@ -116,7 +185,9 @@ export function RemoteOptionsContainer({ queryEnabled }: RemoteOptionsContainerP
116
185
117
186
return (
118
187
< ControlSelect
119
- options = { data || [ ] }
188
+ showLoadMoreButton = { showLoadMoreButton ( ) }
189
+ onLoadMore = { onLoadMore }
190
+ options = { pageable . data }
120
191
// XXX isSearchable if pageQuery? or maybe in all cases? or maybe NOT when pageQuery
121
192
selectProps = { {
122
193
isLoading : isFetching ,
0 commit comments