@@ -77,18 +77,24 @@ export function ContentStackCredentialsProvider({
77
77
}
78
78
79
79
interface ContentStackFetcherProps {
80
- entryUID ?: string ;
81
80
contentType ?: string ;
82
- limit ?: number ;
81
+ fetchType ?: string ;
82
+ entryUID ?: string ;
83
+ filter ?: boolean ;
84
+ filterField ?: string ;
85
+ filterType ?: 'where' | 'greaterThanOrEqualTo' | 'lessThanOrEqualTo' ;
86
+ filterValue ?: string ;
87
+ order : boolean ;
83
88
orderBy ?: string ;
84
89
ascending ?: boolean ;
90
+ limit ?: number ;
85
91
children ?: ReactNode ;
86
92
className ?: string ;
87
93
noLayout ?: boolean ;
88
94
setControlContextData ?: ( data : {
89
95
types ?: { title : string ; uid : string } [ ] ;
90
96
entries ?: { title : string ; uid : string } [ ] ;
91
- fields ?: { display_name : string ; uid : string } [ ] ;
97
+ fields ?: { display_name : string ; uid : string } [ ] ;
92
98
} ) => void ;
93
99
}
94
100
@@ -132,6 +138,15 @@ export const ContentStackFetcherMeta: ComponentMeta<ContentStackFetcherProps> =
132
138
displayName : "Content type" ,
133
139
description : "Content type to be queried." ,
134
140
} ,
141
+ fetchType : {
142
+ type : "choice" ,
143
+ options : [
144
+ { label : 'Single Entry' , value : 'single' } ,
145
+ { label : 'All Entries' , value : 'all' } ,
146
+ ] ,
147
+ displayName : "Fetch type" ,
148
+ description : "What type of query to use." ,
149
+ } ,
135
150
entryUID : {
136
151
type : "choice" ,
137
152
options : ( props , ctx ) =>
@@ -140,11 +155,45 @@ export const ContentStackFetcherMeta: ComponentMeta<ContentStackFetcherProps> =
140
155
value : entry . uid ,
141
156
} ) ) ?? [ ] ,
142
157
displayName : "Entry UID" ,
143
- description : "Query in Content Type." ,
158
+ description : "Select a single entry." ,
159
+ hidden : props => props . fetchType !== 'single' ,
144
160
} ,
145
- limit : {
146
- type : "number" ,
147
- displayName : "Limit Results" ,
161
+ filter : {
162
+ type : "boolean" ,
163
+ displayName : "Filter Entries" ,
164
+ hidden : props => props . fetchType !== 'all' ,
165
+ } ,
166
+ filterField : {
167
+ type : "choice" ,
168
+ options : ( props , ctx ) =>
169
+ ctx ?. fields ?. map ( ( field ) => ( {
170
+ label : field . display_name ,
171
+ value : field . uid ,
172
+ } ) ) ?? [ ] ,
173
+ displayName : "Filter On" ,
174
+ description : "For Created/Updated At, YYYY-MM-DD is supported" ,
175
+ hidden : props => ! props . filter || props . fetchType !== 'all' ,
176
+ } ,
177
+ filterType : {
178
+ type : "choice" ,
179
+ options : [
180
+ { label : 'Equals' , value : 'where' } ,
181
+ { label : 'Greater Than' , value : 'greaterThanOrEqualTo' } ,
182
+ { label : 'Less Than' , value : 'lessThanOrEqualTo' }
183
+ ] ,
184
+ displayName : "Filter Type" ,
185
+ hidden : props => ! props . filter || props . fetchType !== 'all' ,
186
+ } ,
187
+ filterValue : {
188
+ type : "string" ,
189
+ displayName : "Filter Value" ,
190
+ description : "May not work on non-string fields." ,
191
+ hidden : props => ! props . filter || props . fetchType !== 'all' ,
192
+ } ,
193
+ order : {
194
+ type : "boolean" ,
195
+ displayName : "Order Entries" ,
196
+ hidden : props => props . fetchType !== 'all' ,
148
197
} ,
149
198
orderBy : {
150
199
type : "choice" ,
@@ -154,11 +203,18 @@ export const ContentStackFetcherMeta: ComponentMeta<ContentStackFetcherProps> =
154
203
value : field . uid ,
155
204
} ) ) ?? [ ] ,
156
205
displayName : "Order By" ,
206
+ hidden : props => ! props . order || props . fetchType !== 'all' ,
157
207
} ,
158
208
ascending : {
159
209
type : "choice" ,
160
- options : [ { label : 'Ascending' , value : true } , { label : 'Descending' , value : false } ] ,
210
+ options : [ { label : 'Ascending' , value : true } , { label : 'Descending' , value : false } ] ,
161
211
displayName : "Order Direction" ,
212
+ hidden : props => ! props . order || props . fetchType !== 'all' ,
213
+ } ,
214
+ limit : {
215
+ type : "number" ,
216
+ displayName : "Limit Results" ,
217
+ hidden : props => props . fetchType === 'single' ,
162
218
} ,
163
219
noLayout : {
164
220
type : "boolean" ,
@@ -171,11 +227,17 @@ export const ContentStackFetcherMeta: ComponentMeta<ContentStackFetcherProps> =
171
227
} ;
172
228
173
229
export function ContentStackFetcher ( {
174
- entryUID,
175
230
contentType,
176
- limit,
231
+ fetchType,
232
+ entryUID,
233
+ filter,
234
+ filterField,
235
+ filterType,
236
+ filterValue,
237
+ order,
177
238
orderBy,
178
239
ascending,
240
+ limit,
179
241
children,
180
242
className,
181
243
noLayout,
@@ -192,7 +254,7 @@ export function ContentStackFetcher({
192
254
} ) ;
193
255
194
256
const { data : entryData } = usePlasmicQueryData < any | null > (
195
- contentType && entryUID
257
+ contentType && entryUID && fetchType === 'single'
196
258
? `${ cacheKey } /${ contentType } /entry/${ entryUID } `
197
259
: null ,
198
260
async ( ) => {
@@ -213,24 +275,29 @@ export function ContentStackFetcher({
213
275
) ;
214
276
215
277
const { data : entriesData } = usePlasmicQueryData < any | null > (
216
- contentType ? `${ cacheKey } /${ contentType } /entries${ limit ? "/limit/" + limit : '' } ${ orderBy ? "/order/" + orderBy + ( ascending ? '/ascending' : '' ) : '' } ` : null ,
278
+ contentType && fetchType === 'all' ? `${ cacheKey } /${ contentType } /entries${
279
+ limit ? "/limit/" + limit : ''
280
+ } ${
281
+ order && orderBy ? "/order/" + orderBy + ( ascending ? '/ascending' : '' ) : ''
282
+ } ${
283
+ filter && filterField && filterType && filterValue ? `/filter/${ filterField } /${ filterType } /${ filterValue } ` : ''
284
+ } ` : null ,
217
285
async ( ) => {
218
286
let Query = Stack . ContentType ( `${ contentType ! } ` ) . Query ( ) ;
219
- if ( orderBy ) {
287
+ if ( filter && filterField && filterType && filterValue ) {
288
+ Query = Query [ filterType ] ( filterField , filterValue ) ;
289
+ }
290
+ if ( order && orderBy ) {
220
291
Query = Query [ ascending ? 'ascending' : 'descending' ] ( orderBy ) ;
221
292
}
222
- if ( limit ) {
293
+ if ( limit ) {
223
294
Query = Query . limit ( limit ) ;
224
295
}
225
296
return await Query . toJSON ( ) . find ( ) ;
226
297
}
227
298
) ;
228
299
229
- const schema = [ { display_name : 'Created At' , uid : 'created_at' } , { display_name : 'Updated At' , uid : 'updated_at' } ] ;
230
- if ( contentTypes ) {
231
- schema . push ( ...contentTypes ?. filter ( ( x : any ) => x . uid === contentType ) ?. [ 0 ] ?. schema ) ;
232
- }
233
-
300
+ const schema = [ { display_name : 'Created At' , uid : 'created_at' } , { display_name : 'Updated At' , uid : 'updated_at' } , ...( contentTypes ?. filter ( ( x : any ) => x . uid === contentType ) ?. [ 0 ] ?. schema ?? [ ] ) ] ;
234
301
setControlContextData ?.( {
235
302
types : contentTypes ,
236
303
entries : entriesData ?. [ 0 ] ,
0 commit comments