Skip to content

Commit

Permalink
fix: remove hook from conditional statement (#455)
Browse files Browse the repository at this point in the history
  • Loading branch information
SwaySway committed Mar 9, 2022
1 parent b8e9922 commit 5b05377
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -617,14 +617,12 @@ export default function CollectionOfCustomButtons(
],
};
const buttonUserFilter = createDataStorePredicate<User>(buttonUserFilterObj);
const buttonUser =
items !== undefined
? items
: useDataStoreBinding({
type: \\"collection\\",
model: User,
criteria: buttonUserFilter,
}).items;
const buttonUserDataStore = useDataStoreBinding({
type: \\"collection\\",
model: User,
criteria: buttonUserFilter,
}).items;
const buttonUser = items !== undefined ? items : buttonUserDataStore;
const buttonColorFilterObj = {
field: \\"userID\\",
operand: \\"user@email.com\\",
Expand Down Expand Up @@ -723,15 +721,13 @@ export default function CollectionOfCustomButtons(
sort: (s: SortPredicate<User>) =>
s.firstName(SortDirection.ASCENDING).lastName(SortDirection.DESCENDING),
};
const buttonUser =
items !== undefined
? items
: useDataStoreBinding({
type: \\"collection\\",
model: User,
criteria: buttonUserFilter,
pagination: buttonUserPagination,
}).items;
const buttonUserDataStore = useDataStoreBinding({
type: \\"collection\\",
model: User,
criteria: buttonUserFilter,
pagination: buttonUserPagination,
}).items;
const buttonUser = items !== undefined ? items : buttonUserDataStore;
const buttonColorFilterObj = {
field: \\"userID\\",
operand: \\"user@email.com\\",
Expand Down Expand Up @@ -827,14 +823,12 @@ export default function CollectionOfCustomButtons(
],
};
const itemsFilter = createDataStorePredicate<User>(itemsFilterObj);
const items =
itemsProp !== undefined
? itemsProp
: useDataStoreBinding({
type: \\"collection\\",
model: User,
criteria: itemsFilter,
}).items;
const itemsDataStore = useDataStoreBinding({
type: \\"collection\\",
model: User,
criteria: itemsFilter,
}).items;
const items = itemsProp !== undefined ? itemsProp : itemsDataStore;
const buttonColorFilterObj = {
field: \\"userID\\",
operand: \\"user@email.com\\",
Expand Down Expand Up @@ -904,13 +898,11 @@ export default function ListingCardCollection(
props: ListingCardCollectionProps
): React.ReactElement {
const { items, overrideItems, overrides, ...rest } = props;
const bananas =
items !== undefined
? items
: useDataStoreBinding({
type: \\"collection\\",
model: UntitledModel,
}).items;
const bananasDataStore = useDataStoreBinding({
type: \\"collection\\",
model: UntitledModel,
}).items;
const bananas = items !== undefined ? items : bananasDataStore;
return (
/* @ts-ignore: TS2322 */
<Collection
Expand Down Expand Up @@ -5021,13 +5013,11 @@ export default function CollectionDefaultValue(
props: CollectionDefaultValueProps
): React.ReactElement {
const { items, overrideItems, overrides, ...rest } = props;
const user =
items !== undefined
? items
: useDataStoreBinding({
type: \\"collection\\",
model: User,
}).items;
const userDataStore = useDataStoreBinding({
type: \\"collection\\",
model: User,
}).items;
const user = items !== undefined ? items : userDataStore;
return (
/* @ts-ignore: TS2322 */
<Collection
Expand Down
52 changes: 41 additions & 11 deletions packages/codegen-ui-react/lib/react-studio-template-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,19 +733,45 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
statements.push(this.buildPaginationStatement(propName, model, sort));
}
this.importCollection.addImport(ImportSource.LOCAL_MODELS, model);
/**
* const userDataStore = useDataStoreBinding({
* type: "collection",
* model: User,
* ...
* }).items;
*/
statements.push(
factory.createVariableStatement(
undefined,
factory.createVariableDeclarationList(
[
factory.createVariableDeclaration(
factory.createIdentifier(this.getDataStoreName(propName)),
undefined,
undefined,
factory.createPropertyAccessExpression(
this.buildUseDataStoreBindingCall(
'collection',
model,
predicate ? this.getFilterName(propName) : undefined,
sort ? this.getPaginationName(propName) : undefined,
),
factory.createIdentifier('items'),
),
),
],
ts.NodeFlags.Const,
),
),
);
/**
* const items = itemsProp !== undefined ? itemsProp : userDataStore;
*/
statements.push(
this.buildPropPrecedentStatement(
propName,
this.hasCollectionPropertyNamedItems(component) ? 'itemsProp' : 'items',
factory.createPropertyAccessExpression(
this.buildUseDataStoreBindingCall(
'collection',
model,
predicate ? this.getFilterName(propName) : undefined,
sort ? this.getPaginationName(propName) : undefined,
),
'items',
),
factory.createIdentifier(this.getDataStoreName(propName)),
),
);
});
Expand Down Expand Up @@ -798,7 +824,8 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
const { model } = bindingProperties;
this.importCollection.addImport(ImportSource.LOCAL_MODELS, model);

/* const buttonColorDataStore = useDataStoreBinding({
/**
* const buttonColorDataStore = useDataStoreBinding({
* type: "collection"
* ...
* }).items[0];
Expand All @@ -825,7 +852,10 @@ export abstract class ReactStudioTemplateRenderer extends StudioTemplateRenderer
),
),
);

/**
* const buttonColor =
* buttonColorProp !== undefined ? buttonColorProp : buttonColorDataStore;
*/
statements.push(
factory.createVariableStatement(
undefined,
Expand Down

0 comments on commit 5b05377

Please sign in to comment.