From 9155b79ec9f0de816d3cb5b329c4a5b475dc4e04 Mon Sep 17 00:00:00 2001 From: obvTiger Date: Fri, 28 Mar 2025 16:03:05 +0100 Subject: [PATCH] fix: checkbox data content --- index.js | 23 +++++++++++++++++ lib/generators/InputElementGenerator.js | 21 ++++++++++++++-- lib/generators/ServerCodeGenerator.js | 33 ++++++++++++++++++++++--- 3 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..8734dc8 --- /dev/null +++ b/index.js @@ -0,0 +1,23 @@ +module.exports = (app, prisma) => { + app.get("/", async (req, res) => { + try { + const data = req.body; + + // input valiation + + // business logic + + // output validation + + // output + const response = { + + }; + + res.json({ response }); + } catch (error) { + console.log('Error:', error); + res.status(500).json({ error: 'Internal server error' }); + } + }); +} \ No newline at end of file diff --git a/lib/generators/InputElementGenerator.js b/lib/generators/InputElementGenerator.js index 70a9cb4..639155c 100644 --- a/lib/generators/InputElementGenerator.js +++ b/lib/generators/InputElementGenerator.js @@ -55,6 +55,23 @@ class InputElementGenerator { attributes = ' type="range"'; } + // Extract and handle ID attribute + let idAttr = ""; + const idProp = node.props.find(p => typeof p === "string" && p.startsWith("id:")); + if (idProp) { + const idValue = idProp.substring(idProp.indexOf(":") + 1).trim().replace(/^"|"$/g, ""); + idAttr = ` id="${idValue}"`; + node.elementId = idValue; + + // Register as reactive element with the parent generator + if (this.parentGenerator && this.parentGenerator.jsGenerator) { + this.parentGenerator.jsGenerator.registerReactiveElement(idValue); + if (this.options.debug) { + console.log(`[InputElementGenerator] Registered checkbox with ID: ${idValue} as reactive`); + } + } + } + const valueProp = node.props.find((p) => p.startsWith("value:")); if (valueProp) { const value = valueProp.substring(valueProp.indexOf(":") + 1).trim(); @@ -70,7 +87,7 @@ class InputElementGenerator { if (node.children.length > 0) { let html = ``; return html; } else { - return ``; + return ``; } } } diff --git a/lib/generators/ServerCodeGenerator.js b/lib/generators/ServerCodeGenerator.js index d7342df..c10a6f6 100644 --- a/lib/generators/ServerCodeGenerator.js +++ b/lib/generators/ServerCodeGenerator.js @@ -100,7 +100,25 @@ const _bp_api = { apiCode += `async function _bp_serverAction_${elementId}(e) { const data = {}; -${params.map(param => ` data.${param} = ${param} ? ${param}.value : null;`).join('\n')} +${params.map(param => ` const ${param}_element = document.getElementById('${param}'); + if (${param}_element) { + console.log('Found element: ${param}', ${param}_element); + if (${param}_element.type === 'checkbox') { + data.${param} = ${param}_element.checked; + console.log('Checkbox ${param} value:', ${param}_element.checked); + } else if (${param}_element.type === 'radio') { + data.${param} = ${param}_element.checked; + } else if (${param}_element.value !== undefined) { + data.${param} = ${param}_element.value; + } else { + data.${param} = ${param}_element.textContent; + } + } else { + console.error('[Blueprint] Element with ID ${param} not found'); + data.${param} = null; + }`).join('\n')} + + console.log('Submitting data:', data); try { const result = await _bp_api.post('${endpoint}', data); @@ -114,7 +132,11 @@ ${params.map(param => ` data.${param} = ${param} ? ${param}.value : null;`).joi else { const element = document.getElementById(key); if (element) { - element.textContent = result[key]; + if (element.tagName.toLowerCase() === 'input') { + element.value = result[key]; + } else { + element.textContent = result[key]; + } console.log(\`[Blueprint API] Updated element #\${key} with value: \${result[key]}\`); } } @@ -164,7 +186,12 @@ function createBlueprintApiServer(port = 3001) { serverCode += ` app.post('${endpoint}', async (req, res) => { try { - ${params.map(param => `const ${param} = req.body.${param};`).join('\n ')} + ${params.map(param => { + return `const ${param} = req.body.${param} !== undefined ? req.body.${param} : null; + if (${param} === null) { + console.error(\`Missing parameter: ${param}\`); + }`; + }).join('\n ')} let result; try {