fix: checkbox data content
This commit is contained in:
parent
79a7f92ff9
commit
9155b79ec9
3 changed files with 72 additions and 5 deletions
23
index.js
Normal file
23
index.js
Normal file
|
@ -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' });
|
||||
}
|
||||
});
|
||||
}
|
|
@ -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 = `<label class="${className}-container">`;
|
||||
html += `<input class="${className}"${attributes}>`;
|
||||
html += `<input class="${className}"${attributes}${idAttr}>`;
|
||||
|
||||
node.children.forEach((child) => {
|
||||
child.parent = node;
|
||||
|
@ -80,7 +97,7 @@ class InputElementGenerator {
|
|||
html += `</label>`;
|
||||
return html;
|
||||
} else {
|
||||
return `<input class="${className}"${attributes}>`;
|
||||
return `<input class="${className}"${attributes}${idAttr}>`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Reference in a new issue