Skip to content

Commit

Permalink
Context references (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarParra authored Oct 26, 2023
1 parent f011650 commit fb8311c
Show file tree
Hide file tree
Showing 45 changed files with 653 additions and 639 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ Powerful formula-syntax evaluator for Apex and LWC.

### Unlocked Package (`expression` namespace)

[![Install Unlocked Package in a Sandbox](assets/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04tDm0000011MhkIAE)
[![Install Unlocked Package in Production](assets/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04tDm0000011MhkIAE)
[![Install Unlocked Package in a Sandbox](assets/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04tDm0000011MhzIAE)
[![Install Unlocked Package in Production](assets/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04tDm0000011MhzIAE)

Install with SF CLI:

```shell
sf package install --apex-compile package --wait 20 --package 04tDm0000011MhkIAE
sf package install --apex-compile package --wait 20 --package 04tDm0000011MhzIAE
```

Install with SFDX CLI:

```shell
sfdx force:package:install --apexcompile package --wait 20 --package 04tDm0000011MhkIAE
sfdx force:package:install --apexcompile package --wait 20 --package 04tDm0000011MhzIAE
```

### Direct Deployment to Salesforce
Expand Down Expand Up @@ -1723,19 +1723,19 @@ by the `Expression` language.

### Unlocked Package (`expression` namespace)

[![Install Unlocked Package in a Sandbox](assets/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04tDm0000011MhpIAE)
[![Install Unlocked Package in Production](assets/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04tDm0000011MhpIAE)
[![Install Unlocked Package in a Sandbox](assets/btn-install-unlocked-package-sandbox.png)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04tDm0000011Mi4IAE)
[![Install Unlocked Package in Production](assets/btn-install-unlocked-package-production.png)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04tDm0000011Mi4IAE)

Install with SF CLI:

```shell
sf package install --apex-compile package --wait 20 --package 04tDm0000011MhpIAE
sf package install --apex-compile package --wait 20 --package 04tDm0000011Mi4IAE
```

Install with SFDX CLI:

```shell
sfdx force:package:install --apexcompile package --wait 20 --package 04tDm0000011MhpIAE
sfdx force:package:install --apexcompile package --wait 20 --package 04tDm0000011Mi4IAE
```

## Components
Expand Down
2 changes: 1 addition & 1 deletion expression-components/main/staticresources/tw/css/main.css

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<template>
<div class="rounded-md bg-red-50 p-4">
<div class={containerClasses}>
<div class="flex">
<div class="flex-shrink-0">
<svg class="h-5 w-5 text-red-400" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<svg lwc:if={isError} class="h-5 w-5 text-red-400" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.28 7.22a.75.75 0 00-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 101.06 1.06L10 11.06l1.72 1.72a.75.75 0 101.06-1.06L11.06 10l1.72-1.72a.75.75 0 00-1.06-1.06L10 8.94 8.28 7.22z" clip-rule="evenodd" />
</svg>
<svg lwc:if={isWarning} class="h-5 w-5 text-yellow-400" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M8.485 2.495c.673-1.167 2.357-1.167 3.03 0l6.28 10.875c.673 1.167-.17 2.625-1.516 2.625H3.72c-1.347 0-2.189-1.458-1.515-2.625L8.485 2.495zM10 5a.75.75 0 01.75.75v3.5a.75.75 0 01-1.5 0v-3.5A.75.75 0 0110 5zm0 9a1 1 0 100-2 1 1 0 000 2z" clip-rule="evenodd" />
</svg>
</div>
<div class="ml-3">
<h3 class="text-sm font-medium text-yellow-800">{title}</h3>
<div class="mt-2 text-sm text-yellow-700">
<h3 class={titleTextClasses}>{title}</h3>
<div class={messageTextClasses}>
<p>{message}</p>
</div>
</div>
</div>
</div>

</template>
42 changes: 41 additions & 1 deletion expression-components/main/ui/components/base/lwc/alert/alert.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
import {api, LightningElement} from 'lwc';
import { api, LightningElement } from 'lwc';
import { classNames } from 'c/utils';

export default class Alert extends LightningElement {
@api title;
@api message;
/**
* The variant changes the appearance of the alert.
* Accepted variants include:
* - error
* - warning
*/
@api type;

get containerClasses() {
return classNames(
'rounded-md p-4',
{'bg-red-50': this.isError},
{'bg-yellow-50': this.isWarning},
);
}

get titleTextClasses() {
return classNames(
'text-sm font-medium',
{'text-red-800': this.isError},
{'text-yellow-800': this.isWarning},
);
}

get messageTextClasses() {
return classNames(
'mt-2 text-sm',
{'text-red-700': this.isError},
{'text-yellow-700': this.isWarning},
);
}

get isWarning() {
return this.type === 'warning';
}

get isError() {
return this.type === 'error';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<template>
<slot></slot>
<div if:true={cannotShowPreview}>
<c-alert type="warning" title="Unable to show preview"
message="To see a preview of the component, please provide a Preview Context ID when the Context URL Parameter is set.">
</c-alert>
</div>
<div if:true={error}>
<c-alert type="error" title="Error parsing expression" message={error}></c-alert>
</div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { LightningElement, api } from 'lwc';

export default class ExpressionElementContainer extends LightningElement {
@api cannotShowPreview = false;
@api error = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<description>Expression Element Container</description>
<isExposed>false</isExposed>
<masterLabel>Expression Element Container</masterLabel>
</LightningComponentBundle>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import TwElement from "c/twElement";
import evaluate from '@salesforce/apex/FormulaEvaluatorUiController.evaluate';
import { CurrentPageReference } from "lightning/navigation";
import { wire } from "lwc";

export default class ExpressionSiteElement extends TwElement {
// These 4 need to be set by the extending class as `@api` properties.
contextUrlParam;
previewContextId;
expr;
respectSharing;

currentPageReference;
computed;
error;
contextId = null

@wire(CurrentPageReference)
setCurrentPageReference(currentPageReference) {
this.currentPageReference = currentPageReference;
if (this.contextUrlParam) {
this.contextId = this.isInBuilder ? this.previewContextId : this.currentPageReference.state[this.contextUrlParam];
}
}

@wire(evaluate, {recordId: '$contextId', formula: '$expr', respectSharing: '$respectSharing'})
evaluate({error, data}) {
if (error) {
console.error(error);
this.error = error.body.message;
} else {
this.computed = data;
this.validate();
}
}

get loading() {
return !this.computed && !this.error;
}

get hasError() {
return this.error;
}

get ready() {
return !this.loading && !this.hasError && !this.cannotShowPreview;
}

get isInBuilder() {
return this.currentPageReference?.state?.app === 'commeditor';
}

get cannotShowPreview() {
// If in the builder, and the contextUrlParam is set but the previewContextId is not,
// then we cannot show the preview.
if (!this.contextUrlParam) {
return false;
}

if (this.isInBuilder) {
return !this.previewContextId;
}

return false;
}

validate() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<description>Expression Site Element</description>
<isExposed>false</isExposed>
<masterLabel>Expression Site Element</masterLabel>
</LightningComponentBundle>
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<template>
<div if:true={ready}>
<c-base-button action={computed} size="lg"></c-base-button>
</div>
<div if:true={error}>
<c-alert title="Error parsing expression" message={error}></c-alert>
</div>
<c-expression-element-container cannot-show-preview={cannotShowPreview} error={error}>
<div if:true={ready}>
<c-base-button action={computed} size="lg"></c-base-button>
</div>
</c-expression-element-container>
</template>
Original file line number Diff line number Diff line change
@@ -1,39 +1,13 @@
import TwElement from "c/twElement";
import evaluate from '@salesforce/apex/FormulaEvaluatorUiController.evaluate';
import { api, wire } from "lwc";
import ExpressionSiteElement from "c/expressionSiteElement";
import { api } from "lwc";

export default class Button extends TwElement {
export default class Button extends ExpressionSiteElement {
@api contextUrlParam;
@api previewContextId;
@api expr;
@api centerItems;
@api respectSharing;

computed;
error;

@wire(evaluate, {recordId: '', formula: '$expr', respectSharing: '$respectSharing'})
evaluate({error, data}) {
if (error) {
console.error(error);
this.error = error.body.message;
} else {
this.computed = data;
this._validate();
}
}

get loading() {
return !this.computed && !this.error;
}

get hasError() {
return this.error;
}

get ready() {
return !this.loading && !this.hasError;
}

_validate() {
validate() {
if (!this.computed) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
</targets>
<targetConfigs>
<targetConfig targets="lightningCommunity__Default">
<property name="contextUrlParam" type="String" required="false" label="Context URL Parameter"
description="Optional - The name of the URL parameter that contains the context ID." />
<property name="previewContextId" type="String" required="false" label="Preview Context ID"
description="Optional - The ID of the context record to use when previewing the component in the builder." />
<property name="expr" type="String" placeholder="1 + 1" required="true" label="Formula Expression"
description="Formula expression to be evaluated." default="" />
<property name="respectSharing" type="Boolean" default="true" label="Respect Sharing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
<template>
<div if:true={ready} class="mx-auto max-w-7xl">
<div if:true={hasText} class="mx-auto max-w-2xl lg:mx-0">
<h2 if:true={computed.title} class="text-3xl font-bold tracking-tight text-dxp-text-contrast sm:text-4xl">
{computed.title}
</h2>
<p if:true={computed.description} class="mt-6 text-lg leading-8 text-dxp-text-contrast-3">
{computed.description}
</p>
<c-expression-element-container cannot-show-preview={cannotShowPreview} error={error}>
<div lwc:if={ready} class="mx-auto max-w-7xl">
<div if:true={hasText} class="mx-auto max-w-2xl lg:mx-0">
<h2 if:true={computed.title} class="text-3xl font-bold tracking-tight text-dxp-text-contrast sm:text-4xl">
{computed.title}
</h2>
<p if:true={computed.description} class="mt-6 text-lg leading-8 text-dxp-text-contrast-3">
{computed.description}
</p>
</div>
<dl class="mx-auto mt-16 grid max-w-2xl grid-cols-1 gap-x-8 gap-y-16 text-dxp-text-contrast leading-7 sm:grid-cols-2 lg:mx-0 lg:max-w-none lg:grid-cols-3">
<template for:each={computed.features} for:item="feature">
<div key={feature.title}>
<dt class="font-semibold text-gray-900">{feature.title}</dt>
<dd class="mt-1 text-dxp-text-contrast-3">{feature.description}</dd>
</div>
</template>
</dl>
</div>
<dl class="mx-auto mt-16 grid max-w-2xl grid-cols-1 gap-x-8 gap-y-16 text-dxp-text-contrast leading-7 sm:grid-cols-2 lg:mx-0 lg:max-w-none lg:grid-cols-3">
<template for:each={computed.features} for:item="feature">
<div key={feature.title}>
<dt class="font-semibold text-gray-900">{feature.title}</dt>
<dd class="mt-1 text-dxp-text-contrast-3">{feature.description}</dd>
</div>
</template>
</dl>
</div>
<div if:true={error}>
<c-alert title="Error parsing expression" message={error}></c-alert>
</div>
</c-expression-element-container>
</template>
Original file line number Diff line number Diff line change
@@ -1,42 +1,17 @@
import TwElement from "c/twElement";
import evaluate from '@salesforce/apex/FormulaEvaluatorUiController.evaluate';
import { api, wire } from "lwc";
import ExpressionSiteElement from "c/expressionSiteElement";
import { api } from "lwc";

export default class Features extends TwElement {
export default class Features extends ExpressionSiteElement {
@api contextUrlParam;
@api previewContextId;
@api expr;
@api respectSharing;

computed;
error;

@wire(evaluate, {recordId: '', formula: '$expr', respectSharing: '$respectSharing'})
evaluate({error, data}) {
if (error) {
console.error(error);
this.error = error.body.message;
} else {
this.computed = data;
this._validate();
}
}

get loading() {
return !this.computed && !this.error;
}

get hasError() {
return this.error;
}

get ready() {
return !this.loading && !this.hasError;
}

get hasText() {
return this.computed?.title || this.computed?.description;
}

_validate() {
validate() {
if (!this.computed) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
</targets>
<targetConfigs>
<targetConfig targets="lightningCommunity__Default">
<property name="contextUrlParam" type="String" required="false" label="Context URL Parameter"
description="Optional - The name of the URL parameter that contains the context ID." />
<property name="previewContextId" type="String" required="false" label="Preview Context ID"
description="Optional - The ID of the context record to use when previewing the component in the builder." />
<property name="expr" type="String" placeholder="1 + 1" required="true" label="Formula Expression"
description="Formula expression to be evaluated." default="" />
<property name="respectSharing" type="Boolean" default="true" label="Respect Sharing"
Expand Down
Loading

0 comments on commit fb8311c

Please sign in to comment.