Fixes config editing JS messages

main
Yasen Pramatarov 2025-01-26 17:32:37 +02:00
parent 4a8185839d
commit 828020d689
3 changed files with 130 additions and 120 deletions

View File

@ -35,13 +35,18 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get raw input
$jsonData = file_get_contents('php://input');
//DEBUG error_log("Received JSON data: " . $jsonData);
$postData = json_decode($jsonData, true);
if (json_last_error() !== JSON_ERROR_NONE) {
Messages::flash('ERROR', 'DEFAULT', 'Invalid JSON data received', true);
$error = json_last_error_msg();
//DEBUG error_log("JSON decode error: " . $error);
Messages::flash('ERROR', 'DEFAULT', 'Invalid JSON data received: ' . $error, true);
echo json_encode([
'success' => false,
'message' => 'Invalid JSON data received'
'message' => 'Invalid JSON data received: ' . $error
]);
exit;
}
@ -50,12 +55,14 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$result = $configObject->editConfigFile($postData, $config_file);
if ($result === true) {
$messageData = Messages::getMessageData('NOTICE', 'DEFAULT', 'Config file updated successfully', true);
//DEBUG error_log("Config updated successfully");
echo json_encode([
'success' => true,
'message' => 'Config file updated successfully',
'messageData' => $messageData
]);
} else {
//DEBUG error_log("Config update error: " . $result);
$messageData = Messages::getMessageData('ERROR', 'DEFAULT', "Error updating config file: $result", true);
echo json_encode([
'success' => false,

View File

@ -1,12 +1,15 @@
<!-- config file -->
<div class="container-fluid mt-2">
<div id="messages-container"></div>
<div class="row mb-4">
<div class="col-12 mb-5">
<div class="col-12 mb-4">
<h2>Configuration</h2>
<?php if ($configMessage): ?>
<small>Jilo Web configuration file <?= htmlspecialchars($config_file) ?></small>
<?php if ($configMessage) { ?>
<?= $configMessage ?>
<?php endif; ?>
<?php } ?>
</div>
</div>
<div class="card shadow-sm">
@ -41,14 +44,14 @@ function renderConfigItem($key, $value, $path = '') {
$displayName = ucfirst(str_replace('_', ' ', $key));
if (is_array($value)) {
echo "\t\t\t\t\t\t\t\t\t<div class=\"config-section mb-4\">";
echo "\n\t\t\t\t\t\t\t\t\t\t<h6 class=\"border-bottom pb-2 mb-3\">" . htmlspecialchars($displayName) . '</h6>';
echo "\n\t\t\t\t\t\t\t\t\t\t<div class=\"ps-4\">\n";
echo "\t\t\t\t\t\t\t\t<div class=\"config-section mb-4\">";
echo "\n\t\t\t\t\t\t\t\t\t<h6 class=\"border-bottom pb-2 mb-3\">" . htmlspecialchars($displayName) . '</h6>';
echo "\n\t\t\t\t\t\t\t\t\t<div class=\"ps-4\">\n";
foreach ($value as $subKey => $subValue) {
renderConfigItem($subKey, $subValue, $fullPath);
}
echo "\t\t\t\t\t\t\t\t\t\t</div>\n";
echo "\t\t\t\t\t\t\t\t\t</div>\n";
echo "\t\t\t\t\t\t\t\t</div>\n";
} else {
?>
<div class="config-item row mb-3 align-items-center">
@ -95,7 +98,6 @@ function renderConfigItem($key, $value, $path = '') {
</div>
</div>
</div>
<!-- /config file -->
<script>
$(function() {
@ -170,21 +172,20 @@ $(function() {
data[name] = $(this).val();
});
fetch('<?= htmlspecialchars($app_root) ?>?page=config', {
$.ajax({
url: '<?= htmlspecialchars($app_root) ?>?page=config',
method: 'POST',
data: JSON.stringify(data),
contentType: 'application/json',
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(response => {
success: function(response) {
//DEBUG console.log('Response:', response);
// Show message first
if (response.messageData) {
showMessage(response.messageData);
}
// Only update UI if save was successful
if (response.success) {
// Update view mode values
@ -218,24 +219,27 @@ $(function() {
}
}
});
// Exit edit mode
// Finally, exit edit mode
$('.toggle-edit').show();
$('.edit-controls').addClass('d-none');
$('.view-mode').show();
$('.edit-mode').addClass('d-none');
}
$btn.prop('disabled', false).html('<i class="fas fa-save me-2"></i>Save');
})
.catch(error => {
},
error: function(xhr, status, error) {
//DEBUG console.error('AJAX Error:', status, error);
//DEBUG console.error('Response:', xhr.responseText);
showMessage({
type: 'danger',
message: 'Error saving config: ' + error,
dismissible: true
});
},
complete: function() {
$btn.prop('disabled', false).html('<i class="fas fa-save me-2"></i>Save');
}
});
});
});
</script>
<!-- /config file -->

View File

@ -11,7 +11,7 @@
<?php if ($page === 'profile') { ?>
<link rel="stylesheet" type="text/css" href="<?= htmlspecialchars($app_root) ?>static/css/profile.css">
<?php } ?>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
@ -49,4 +49,3 @@
</div>
</div>
</div>