Add dedicated Save Settings button for user preferences

- Add separate Save Settings button below User Settings card
- Create myProfileSaveSettings controller method for dedicated settings save
- Settings save endpoint: POST /my-profile/save-settings
- Validates sidebar_auto_hide_seconds (0-60 range)
- Provides immediate feedback: success/error message display
- Separate from profile photo and password updates
- Message auto-hides after 2 seconds on success
This commit is contained in:
DevSoftTech Server 2026-04-28 13:54:46 +05:30
parent 5717763496
commit ec6ceaea98
3 changed files with 113 additions and 1 deletions

View File

@ -9,6 +9,7 @@ $routes->get('/', 'Home::index');
$routes->get('dashboard', 'Home::dashboard');
$routes->get('my-profile', 'Home::myProfile');
$routes->post('my-profile/save', 'Home::myProfileSave');
$routes->post('my-profile/save-settings', 'Home::myProfileSaveSettings');
$routes->get('speech-test', 'Home::speechTest');
$routes->post('stt-proxy', 'Home::sttProxy');
$routes->get('stt-proxy/health', 'Home::sttProxyHealth');

View File

@ -630,6 +630,62 @@ class Home extends BaseController
]);
}
public function myProfileSaveSettings()
{
if (! $this->request->is('post')) {
return $this->response->setStatusCode(405)->setJSON([
'success' => 0,
'message' => 'Method not allowed.',
]);
}
$authUser = auth()->user();
if (! $authUser) {
return $this->response->setStatusCode(401)->setJSON([
'success' => 0,
'message' => 'Not authenticated.',
'csrfName' => csrf_token(),
'csrfHash' => csrf_hash(),
]);
}
$userId = (int) $authUser->id;
// Process sidebar auto-hide setting
$sidebarAutoHideSecondsInput = trim((string) $this->request->getPost('sidebar_auto_hide_seconds'));
$userSettingsModel = model('UserSettings');
if ($sidebarAutoHideSecondsInput === '') {
// Empty means use hospital default
$userSettingsModel->deleteUserSetting($userId, 'SIDEBAR_AUTO_HIDE_SECONDS');
$message = 'Settings saved. Using hospital default.';
} else {
$sidebarAutoHideSeconds = (int) $sidebarAutoHideSecondsInput;
if ($sidebarAutoHideSeconds < 0) {
$sidebarAutoHideSeconds = 0;
}
if ($sidebarAutoHideSeconds > 60) {
$sidebarAutoHideSeconds = 60;
}
if ($sidebarAutoHideSeconds > 0) {
$userSettingsModel->setUserSetting($userId, 'SIDEBAR_AUTO_HIDE_SECONDS', (string) $sidebarAutoHideSeconds);
$message = "Sidebar auto-hide set to {$sidebarAutoHideSeconds} seconds.";
} else {
// 0 means disable auto-hide
$userSettingsModel->setUserSetting($userId, 'SIDEBAR_AUTO_HIDE_SECONDS', '0');
$message = 'Sidebar auto-hide disabled.';
}
}
return $this->response->setJSON([
'success' => 1,
'message' => $message,
'csrfName' => csrf_token(),
'csrfHash' => csrf_hash(),
]);
}
/**
* @return array{phone_no:string, full_name:string, profile_photo:string}
*/

View File

@ -80,7 +80,11 @@
</div>
<div class="small text-muted mt-1">How many seconds before sidebar auto-hides. Enter 0 to disable auto-hide. Leave blank to use hospital default.</div>
</div>
<div class="small text-success" id="settings_msg" style="display:none;">Settings auto-saved.</div>
<div class="d-flex gap-2">
<button type="button" class="btn btn-success btn-sm" id="btn_settings_save">Save Settings</button>
</div>
<div class="small text-success mt-2" id="settings_msg" style="display:none;">Settings saved successfully.</div>
<div class="small text-danger mt-2" id="settings_error" style="display:none;"></div>
</div>
</div>
</div>
@ -212,5 +216,56 @@
setMsg('err', 'Unable to update profile');
});
});
$('#btn_settings_save').on('click', function() {
var sidebarAutoHideSeconds = ($('#sidebar_auto_hide_seconds').val() || '').trim();
// Validate input
if (sidebarAutoHideSeconds !== '') {
var val = parseInt(sidebarAutoHideSeconds, 10);
if (isNaN(val) || val < 0 || val > 60) {
$('#settings_error').text('Please enter a value between 0 and 60.').show();
$('#settings_msg').hide();
return;
}
}
var payload = new window.FormData();
payload.append('sidebar_auto_hide_seconds', sidebarAutoHideSeconds);
var csrf = getCsrfPair();
payload.append(csrf.name, csrf.value);
var $btn = $(this);
$btn.prop('disabled', true).text('Saving...');
$.ajax({
url: '<?= base_url('my-profile/save-settings') ?>',
method: 'POST',
data: payload,
processData: false,
contentType: false,
dataType: 'json'
}).done(function(data) {
updateCsrf(data);
$btn.prop('disabled', false).text('Save Settings');
if (parseInt(data.success || '0', 10) !== 1) {
$('#settings_error').text(data.message || 'Unable to save settings').show();
$('#settings_msg').hide();
return;
}
$('#settings_msg').show();
$('#settings_error').hide();
setTimeout(function() {
$('#settings_msg').fadeOut(500);
}, 2000);
}).fail(function() {
$btn.prop('disabled', false).text('Save Settings');
$('#settings_error').text('Unable to save settings').show();
$('#settings_msg').hide();
});
});
})();
</script>