Skip to content

Commit

Permalink
Merge pull request #1451 from SFDO-Community/feature/1450-get-cronstr…
Browse files Browse the repository at this point in the history
…ue-working-in-lightning-locker

Direct load cronstrue in the LWC to make Locker happy
  • Loading branch information
aheber authored Apr 13, 2024
2 parents 39d592d + 4aa1144 commit 1e2ade2
Show file tree
Hide file tree
Showing 3 changed files with 1,548 additions and 20 deletions.
6 changes: 4 additions & 2 deletions dlrs/main/lwc/classSchedulerModal/classSchedulerModal.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ <h3 class="slds-section__title">
</div>
</lightning-layout-item>
<lightning-layout-item size="12">
<lightning-spinner
lwc:if={isLoadingCurrentSchedule}
></lightning-spinner>
<lightning-layout multiple-rows>
<lightning-layout-item
size="12"
Expand Down Expand Up @@ -55,7 +58,6 @@ <h3 class="slds-section__title">
<lightning-layout multiple-rows>
<lightning-layout-item size="12" class="slds-p-vertical_medium">
<c-cron-builder
values={cronStrings}
oncronupdate={handleOnCronUpdate}
templates={templates}
></c-cron-builder>
Expand All @@ -68,7 +70,7 @@ <h3 class="slds-section__title">New Scheduled Jobs to Add</h3>
<lightning-layout-item flexibility="shrink">
<ul class="slds-has-dividers_around-space">
<template for:each={cronStrings} for:item="cron">
<li key={cron} class="slds-item">
<li key={cron.cronString} class="slds-item">
<lightning-layout vertical-align="center">
<lightning-layout-item flexibility="grow"
>{cron.humanReadable}</lightning-layout-item
Expand Down
48 changes: 30 additions & 18 deletions dlrs/main/lwc/classSchedulerModal/classSchedulerModal.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { api } from "lwc";
import LightningModal from "lightning/modal";
import dlrs from "@salesforce/resourceUrl/dlrs";
import { loadScript } from "lightning/platformResourceLoader";
// import dlrs from "@salesforce/resourceUrl/dlrs";
// import { loadScript } from "lightning/platformResourceLoader";

import { ShowToastEvent } from "lightning/platformShowToastEvent";
import LightningConfirm from "lightning/confirm";
Expand All @@ -11,10 +11,14 @@ import getTotalScheduledJobs from "@salesforce/apex/SchedulerController.getAllSc
import scheduleJobs from "@salesforce/apex/SchedulerController.scheduleJobs";
import cancelScheduledJob from "@salesforce/apex/SchedulerController.cancelScheduledJob";

import CronstrueFactory from "./cronstrue";

export default class ClassSchedulerModal extends LightningModal {
cronstrue;
cronStrings = [];
// async apex jobs
currentSchedule = [];
isLoadingCurrentSchedule = true;
currentColumns = [
{
label: "Name",
Expand Down Expand Up @@ -74,32 +78,39 @@ export default class ClassSchedulerModal extends LightningModal {
templates;

connectedCallback() {
loadScript(this, dlrs + "/js/cronstrue/dist/cronstrue.min.js").then(() => {
// your code with calls to the JS library
console.log("construe loaded");
this.cronStrings.forEach((v) => {
v.humanReadable = window.cronstrue.toString(v.cronString, {
verbose: true
});
this.cronstrue = CronstrueFactory();
// TODO: when LWS is everywhere then we can go back to loading from Static Resource
// loadScript(this, dlrs + "/js/cronstrue/dist/cronstrue.js")
// .then(() => {
// your code with calls to the JS library
console.log("construe loaded");
this.cronStrings.forEach((v) => {
v.humanReadable = this.cronstrue.toString(v.cronString, {
verbose: true
});
});
// })
// .catch((err) => {
// console.error("Failed to load static resource: cronstrue", err);
// })
// .finally(() => {
this.updateScheduledData();
// });
}

handleOnCronUpdate(event) {
// see if this library is globally loaded yet

this.cronStrings = event.detail.value.map((v) => ({
cronString: v,
humanReadable: ""
humanReadable: v
}));
if (window.cronstrue) {
this.cronStrings.forEach((v) => {
v.humanReadable = cronstrue.toString(v.cronString, {
verbose: true
});

this.cronStrings.forEach((v) => {
v.humanReadable = this.cronstrue.toString(v.cronString, {
verbose: true
});
}
});
}

handleRowAction(event) {
Expand All @@ -125,6 +136,7 @@ export default class ClassSchedulerModal extends LightningModal {
}

updateScheduledData() {
this.isLoadingCurrentSchedule = true;
getCurrentJobs({
className: this.className
}).then((jobs) => {
Expand All @@ -133,11 +145,11 @@ export default class ClassSchedulerModal extends LightningModal {
name: j.CronTrigger.CronJobDetail.Name,
nextRunAt: j.CronTrigger.NextFireTime,
cronString: j.CronTrigger.CronExpression,
humanReadable: cronstrue.toString(j.CronTrigger.CronExpression, {
humanReadable: this.cronstrue.toString(j.CronTrigger.CronExpression, {
verbose: true
})
}));
console.log(this.currentSchedule);
this.isLoadingCurrentSchedule = false;
});

getTotalScheduledJobs().then((jobs) => {
Expand Down
Loading

0 comments on commit 1e2ade2

Please sign in to comment.