Skip to content

Commit cc461fb

Browse files
Contentstack package: Add filtering support and improve UX
1 parent abe242b commit cc461fb

File tree

1 file changed

+86
-19
lines changed

1 file changed

+86
-19
lines changed

plasmicpkgs/plasmic-content-stack/src/contentstack.tsx

+86-19
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,24 @@ export function ContentStackCredentialsProvider({
7777
}
7878

7979
interface ContentStackFetcherProps {
80-
entryUID?: string;
8180
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;
8388
orderBy?: string;
8489
ascending?: boolean;
90+
limit?: number;
8591
children?: ReactNode;
8692
className?: string;
8793
noLayout?: boolean;
8894
setControlContextData?: (data: {
8995
types?: { title: string; uid: string }[];
9096
entries?: { title: string; uid: string }[];
91-
fields?: { display_name: string; uid: string}[];
97+
fields?: { display_name: string; uid: string }[];
9298
}) => void;
9399
}
94100

@@ -132,6 +138,15 @@ export const ContentStackFetcherMeta: ComponentMeta<ContentStackFetcherProps> =
132138
displayName: "Content type",
133139
description: "Content type to be queried.",
134140
},
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+
},
135150
entryUID: {
136151
type: "choice",
137152
options: (props, ctx) =>
@@ -140,11 +155,45 @@ export const ContentStackFetcherMeta: ComponentMeta<ContentStackFetcherProps> =
140155
value: entry.uid,
141156
})) ?? [],
142157
displayName: "Entry UID",
143-
description: "Query in Content Type.",
158+
description: "Select a single entry.",
159+
hidden: props => props.fetchType !== 'single',
144160
},
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',
148197
},
149198
orderBy: {
150199
type: "choice",
@@ -154,11 +203,18 @@ export const ContentStackFetcherMeta: ComponentMeta<ContentStackFetcherProps> =
154203
value: field.uid,
155204
})) ?? [],
156205
displayName: "Order By",
206+
hidden: props => !props.order || props.fetchType !== 'all',
157207
},
158208
ascending: {
159209
type: "choice",
160-
options: [{label: 'Ascending', value: true}, {label: 'Descending', value: false}],
210+
options: [{ label: 'Ascending', value: true}, {label: 'Descending', value: false }],
161211
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',
162218
},
163219
noLayout: {
164220
type: "boolean",
@@ -171,11 +227,17 @@ export const ContentStackFetcherMeta: ComponentMeta<ContentStackFetcherProps> =
171227
};
172228

173229
export function ContentStackFetcher({
174-
entryUID,
175230
contentType,
176-
limit,
231+
fetchType,
232+
entryUID,
233+
filter,
234+
filterField,
235+
filterType,
236+
filterValue,
237+
order,
177238
orderBy,
178239
ascending,
240+
limit,
179241
children,
180242
className,
181243
noLayout,
@@ -192,7 +254,7 @@ export function ContentStackFetcher({
192254
});
193255

194256
const { data: entryData } = usePlasmicQueryData<any | null>(
195-
contentType && entryUID
257+
contentType && entryUID && fetchType === 'single'
196258
? `${cacheKey}/${contentType}/entry/${entryUID}`
197259
: null,
198260
async () => {
@@ -213,24 +275,29 @@ export function ContentStackFetcher({
213275
);
214276

215277
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,
217285
async () => {
218286
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){
220291
Query = Query[ascending ? 'ascending' : 'descending'](orderBy);
221292
}
222-
if(limit){
293+
if (limit){
223294
Query = Query.limit(limit);
224295
}
225296
return await Query.toJSON().find();
226297
}
227298
);
228299

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 ?? [])];
234301
setControlContextData?.({
235302
types: contentTypes,
236303
entries: entriesData?.[0],

0 commit comments

Comments
 (0)