2 main
This commit is contained in:
@@ -1445,6 +1445,7 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
||||
document.getElementById('updateSchemaButton').addEventListener('click', updateSchemaDetails);
|
||||
|
||||
document.getElementById('schemaFieldsBody').addEventListener('change', function(event) {
|
||||
|
||||
if (event.target.classList.contains('mapping-select')) {
|
||||
let tr = event.target.closest('tr');
|
||||
let mappingId = event.target.getAttribute('data-id');
|
||||
@@ -1560,19 +1561,22 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
||||
if (sourceType === 'XLS') updateXlsDropdowns();
|
||||
if (sourceType === 'API') updateJsonDropdowns();
|
||||
|
||||
} else if (event.target.classList.contains('main-field-checkbox')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.target.classList.contains('main-field-checkbox')) {
|
||||
const checkbox = event.target;
|
||||
const mappingId = checkbox.dataset.mappingId;
|
||||
const value = checkbox.checked ? 1 : 0;
|
||||
|
||||
// Se checked, deseleziona tutti gli altri visivamente
|
||||
if (checkbox.checked) {
|
||||
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
||||
if (cb !== checkbox) cb.checked = false;
|
||||
});
|
||||
const checkedMainFields = document.querySelectorAll('.main-field-checkbox:checked');
|
||||
|
||||
if (checkedMainFields.length > 2) {
|
||||
checkbox.checked = false;
|
||||
alert('Puoi selezionare al massimo 2 campi Main.');
|
||||
return;
|
||||
}
|
||||
|
||||
// Salva l'aggiornamento
|
||||
fetch('update_main_field.php', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -1583,36 +1587,43 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
||||
mapping_id: mappingId,
|
||||
value: value
|
||||
})
|
||||
}).then(response => response.json())
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (!data.success) {
|
||||
console.error("❌ Error updating main_field:", data.message);
|
||||
checkbox.checked = !checkbox.checked;
|
||||
|
||||
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
||||
cb.checked = cb.dataset.originalChecked === 'true';
|
||||
});
|
||||
} else {
|
||||
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
||||
cb.dataset.originalChecked = cb.checked;
|
||||
});
|
||||
|
||||
alert(data.message || 'Errore durante il salvataggio del campo Main.');
|
||||
return;
|
||||
}
|
||||
|
||||
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
||||
cb.dataset.originalChecked = cb.checked ? 'true' : 'false';
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
console.error("❌ Fetch error:", error);
|
||||
checkbox.checked = !checkbox.checked;
|
||||
|
||||
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
||||
cb.checked = cb.dataset.originalChecked === 'true';
|
||||
});
|
||||
|
||||
alert('Errore di comunicazione durante il salvataggio del campo Main.');
|
||||
});
|
||||
} else if (event.target.classList.contains('visible-parts-checkbox')) {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.target.classList.contains('visible-parts-checkbox')) {
|
||||
const checkbox = event.target;
|
||||
const mappingId = checkbox.dataset.mappingId;
|
||||
const value = checkbox.checked ? 1 : 0;
|
||||
|
||||
// salva stato per rollback
|
||||
const prevChecked = checkbox.checked;
|
||||
|
||||
// ✅ UI: se sto mettendo a 1, tolgo la spunta a tutti gli altri SUBITO
|
||||
if (value === 1) {
|
||||
document.querySelectorAll('.visible-parts-checkbox').forEach(cb => {
|
||||
if (cb !== checkbox) cb.checked = false;
|
||||
@@ -1634,26 +1645,22 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
||||
.then(data => {
|
||||
if (!data.success) {
|
||||
console.error("❌ Error updating is_visible_parts:", data.message);
|
||||
|
||||
// rollback UI
|
||||
checkbox.checked = !prevChecked;
|
||||
|
||||
// se avevo tolto le spunte agli altri, ricarico per riallineare la UI al DB
|
||||
// (semplice e safe)
|
||||
location.reload();
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error("❌ Fetch error:", error);
|
||||
|
||||
// rollback UI
|
||||
checkbox.checked = !prevChecked;
|
||||
location.reload();
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
// Salva lo stato originale dei checkbox al caricamento
|
||||
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
||||
cb.dataset.originalChecked = cb.checked;
|
||||
|
||||
Reference in New Issue
Block a user