Skip to content

Commit 1505089

Browse files
committed
Remove option and automate setting
1 parent 70c0232 commit 1505089

4 files changed

Lines changed: 16 additions & 19 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "javascriptsnippet",
33
"widgetName": "JavaScriptSnippet",
4-
"version": "2.2.0",
4+
"version": "2.2.1",
55
"description": "Insert custom JavaScript to your page.",
66
"copyright": "2024 Incentro International BV",
77
"author": "Rob Duits",

src/JavaScriptSnippet.jsx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "./ui/JavaScriptSnippet.css";
22
import { createElement, useEffect, useState } from "react";
33

4-
export function JavaScriptSnippet({ attributeList, jsCode, waitForAttributeList, ...rest }) {
4+
export function JavaScriptSnippet({ attributeList, jsCode, ...rest }) {
55
const [canRender, setCanRender] = useState(false);
66
const [javaScriptString, setJavaScriptString] = useState([]);
77
const widgetName = rest.name || "";
@@ -28,7 +28,18 @@ export function JavaScriptSnippet({ attributeList, jsCode, waitForAttributeList,
2828
useEffect(() => {
2929
let JSArray = jsCode;
3030

31-
if (waitForAttributeList && attributeList.length) {
31+
// Auto-detect if we need to wait based on attributeList having items and any pending attributes
32+
const hasAttributes = attributeList && attributeList.length > 0;
33+
const allAttributesAvailable = hasAttributes
34+
? attributeList.every(attr => attr.jsAttribute.status === "available")
35+
: true;
36+
37+
if (hasAttributes) {
38+
if (!allAttributesAvailable) {
39+
setCanRender(false);
40+
return; // Exit early if attributes aren't ready
41+
}
42+
3243
attributeList.map(attr => {
3344
if (attr.jsAttribute.status === "available") {
3445
const useValue =
@@ -58,17 +69,7 @@ export function JavaScriptSnippet({ attributeList, jsCode, waitForAttributeList,
5869

5970
JSArray = JSArray.split("this").join(`'${widgetName}_${uid[0]}'`);
6071
setJavaScriptString(JSArray);
61-
62-
if (!waitForAttributeList && !attributeList.length) {
63-
setCanRender(true);
64-
}
65-
66-
// clean-up function
67-
return () => {
68-
if (waitForAttributeList && attributeList.length) {
69-
setCanRender(true);
70-
}
71-
};
72+
setCanRender(true); // Only set to true when everything is ready
7273
}, [attributeList, jsCode, widgetName]);
7374

7475
if (canRender) {

src/JavaScriptSnippet.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ Whenever using the `this` string inside the widget, it will be replaced by the
5252
</propertyGroup>
5353
</properties>
5454
</property>
55-
<property key="waitForAttributeList" type="boolean" defaultValue="false">
56-
<caption>Wait for AttributeList</caption>
57-
<description>Widget only continues after Attributes have been loaded (on false, the widget might get more inside reloads).</description>
58-
</property>
5955
</propertyGroup>
6056
</properties>
6157
</widget>

src/package.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<package xmlns="http://www.mendix.com/package/1.0/">
3-
<clientModule name="JavaScriptSnippet" version="2.2.0" xmlns="http://www.mendix.com/clientModule/1.0/">
3+
<clientModule name="JavaScriptSnippet" version="2.2.1" xmlns="http://www.mendix.com/clientModule/1.0/">
44
<widgetFiles>
55
<widgetFile path="JavaScriptSnippet.xml" />
66
</widgetFiles>

0 commit comments

Comments
 (0)