refactor: show management status as toasts

Route User Management and Role Master action results through fixed top-right notifications, including SPA partial responses, validation failures, session actions, and network errors.
This commit is contained in:
DevSoftTech Server 2026-08-09 23:58:59 +05:30
parent 8f25904a13
commit 9872e1c726
9 changed files with 50 additions and 62 deletions

View File

@ -16,7 +16,7 @@ foreach ($allPermissions as $key => $label) {
<button class="btn btn-light" type="button" onclick="load_form_div('<?= base_url('setting/admin/roles') ?>','maindiv','Role Master');"><i class="bi bi-arrow-left"></i> Back to Role Master</button>
</div>
<div class="card-body">
<?php if (! empty($errors)) : ?><div class="alert alert-danger"><?php foreach ($errors as $error) : ?><div><?= esc($error) ?></div><?php endforeach ?></div><?php endif ?>
<?php foreach ((array) ($errors ?? []) as $error) : ?><span class="d-none" data-page-notification data-notification-type="error" data-notification-title="Role Master" data-notification-message="<?= esc((string) $error, 'attr') ?>"></span><?php endforeach ?>
<?php if ($isSuperAdmin) : ?><div class="alert alert-info">Super Admin permissions are protected against lockout and cannot be changed.</div><?php endif ?>
<form id="frm_role_master" action="<?= $isEdit ? base_url('setting/admin/roles/edit/' . (int) $role['id']) : base_url('setting/admin/roles') ?>" method="post">
@ -75,8 +75,9 @@ foreach ($allPermissions as $key => $label) {
var form = this;
$.post($(form).attr('action'), $(form).serialize()).done(function(html) {
$('#maindiv').html(html);
showPageNotifications(document.getElementById('maindiv'));
}).fail(function(xhr) {
alert((xhr.responseJSON && xhr.responseJSON.error_text) || 'Unable to save role.');
notify('error', 'Role Master', (xhr.responseJSON && xhr.responseJSON.error_text) || 'Unable to save role.');
});
});
})();

View File

@ -14,11 +14,8 @@
</div>
</div>
<div class="card-body">
<?php if (! empty($message)) : ?><div class="alert alert-success"><?= esc($message) ?></div><?php endif ?>
<?php if (! empty($errors)) : ?>
<div class="alert alert-danger"><?php foreach ($errors as $error) : ?><div><?= esc($error) ?></div><?php endforeach ?></div>
<?php endif ?>
<?php if (! empty($message)) : ?><span class="d-none" data-page-notification data-notification-type="success" data-notification-title="Role Master" data-notification-message="<?= esc((string) $message, 'attr') ?>"></span><?php endif ?>
<?php foreach ((array) ($errors ?? []) as $error) : ?><span class="d-none" data-page-notification data-notification-type="error" data-notification-title="Role Master" data-notification-message="<?= esc((string) $error, 'attr') ?>"></span><?php endforeach ?>
<div class="table-responsive">
<table class="table table-striped align-middle">
<thead><tr><th>Role</th><th>Code</th><th>Permissions</th><th>Users</th><th>Status</th><th>Type</th><th>Action</th></tr></thead>
@ -59,11 +56,12 @@
data[csrfName] = csrfHash;
$.post(url, data, null, 'json').done(function(response) {
csrfHash = response.csrf_hash || csrfHash;
notify('success', 'Role Master', response.message || 'Role updated successfully.');
load_form_div('<?= base_url('setting/admin/roles') ?>', 'maindiv', 'Role Master');
}).fail(function(xhr) {
var response = xhr.responseJSON || {};
csrfHash = response.csrf_hash || csrfHash;
alert(response.error_text || 'Unable to update role.');
notify('error', 'Role Master', response.error_text || 'Unable to update role.');
});
}

View File

@ -13,13 +13,7 @@
<div class="card-body">
<?php $errors = $errors ?? session('errors'); ?>
<?php $formData = $formData ?? []; ?>
<?php if (! empty($errors)) : ?>
<div class="alert alert-danger">
<?php foreach ((array) $errors as $error) : ?>
<div><?= esc($error) ?></div>
<?php endforeach ?>
</div>
<?php endif ?>
<?php foreach ((array) $errors as $error) : ?><span class="d-none" data-page-notification data-notification-type="error" data-notification-title="Create User" data-notification-message="<?= esc((string) $error, 'attr') ?>"></span><?php endforeach ?>
<form class="needs-validation" novalidate action="<?= base_url('setting/admin/user-management/new') ?>" method="post">
<?= csrf_field() ?>
<div class="row g-3">
@ -84,9 +78,10 @@
$.post($(form).attr('action'), $(form).serialize())
.done(function(html) {
$('#maindiv').html(html);
showPageNotifications(document.getElementById('maindiv'));
})
.fail(function() {
alert('Request failed. Please try again.');
.fail(function(xhr) {
notify('error', 'Create User', (xhr.responseJSON && xhr.responseJSON.error_text) || 'Request failed. Please try again.');
});
});
})();

View File

@ -11,13 +11,7 @@
<div class="card-body">
<?php $errors = $errors ?? session('errors'); ?>
<?php $formData = $formData ?? []; ?>
<?php if (! empty($errors)) : ?>
<div class="alert alert-danger">
<?php foreach ((array) $errors as $error) : ?>
<div><?= esc($error) ?></div>
<?php endforeach ?>
</div>
<?php endif ?>
<?php foreach ((array) $errors as $error) : ?><span class="d-none" data-page-notification data-notification-type="error" data-notification-title="Edit User" data-notification-message="<?= esc((string) $error, 'attr') ?>"></span><?php endforeach ?>
<form id="frm_user_edit" class="needs-validation" novalidate action="<?= base_url('setting/admin/user-management/edit/' . (int) ($user->id ?? 0)) ?>" method="post">
<?= csrf_field() ?>
@ -82,9 +76,10 @@
$.post($(form).attr('action'), $(form).serialize())
.done(function(html) {
$('#maindiv').html(html);
showPageNotifications(document.getElementById('maindiv'));
})
.fail(function() {
alert('Request failed. Please try again.');
.fail(function(xhr) {
notify('error', 'Edit User', (xhr.responseJSON && xhr.responseJSON.error_text) || 'Request failed. Please try again.');
});
});
})();

View File

@ -16,16 +16,8 @@
<div class="card-body">
<?php $message = $message ?? session('message'); ?>
<?php $errors = $errors ?? session('errors'); ?>
<?php if (! empty($message)) : ?>
<div class="alert alert-success"><?= esc($message) ?></div>
<?php endif ?>
<?php if (! empty($errors)) : ?>
<div class="alert alert-danger">
<?php foreach ((array) $errors as $error) : ?>
<div><?= esc($error) ?></div>
<?php endforeach ?>
</div>
<?php endif ?>
<?php if (! empty($message)) : ?><span class="d-none" data-page-notification data-notification-type="success" data-notification-title="User Management" data-notification-message="<?= esc((string) $message, 'attr') ?>"></span><?php endif ?>
<?php foreach ((array) $errors as $error) : ?><span class="d-none" data-page-notification data-notification-type="error" data-notification-title="User Management" data-notification-message="<?= esc((string) $error, 'attr') ?>"></span><?php endforeach ?>
<div class="row g-2 mb-3">
<div class="col-md-4">
<label class="form-label mb-1" for="um_filter_person">Filter by Person Name</label>
@ -165,12 +157,13 @@
'<?= csrf_token() ?>': $('input[name="<?= csrf_token() ?>"]').first().val() || '<?= csrf_hash() ?>'
}, function(data) {
if (!data || parseInt(data.update || '0', 10) !== 1) {
window.alert((data && data.error_text) || 'Unable to delete user.');
notify('error', 'Delete User', (data && data.error_text) || 'Unable to delete user.');
return;
}
notify('success', 'Delete User', data.error_text || 'User deleted successfully.');
load_form_div('<?= base_url('setting/admin/user-management') ?>', 'maindiv', 'User Management');
}, 'json').fail(function(xhr) {
window.alert((xhr.responseJSON && xhr.responseJSON.error_text) || 'Unable to delete user.');
notify('error', 'Delete User', (xhr.responseJSON && xhr.responseJSON.error_text) || 'Unable to delete user.');
});
});
})();

View File

@ -14,16 +14,8 @@
<div class="card-body">
<?php $message = $message ?? session('message'); ?>
<?php $errors = $errors ?? session('errors'); ?>
<?php if (! empty($message)) : ?>
<div class="alert alert-success"><?= esc($message) ?></div>
<?php endif ?>
<?php if (! empty($errors)) : ?>
<div class="alert alert-danger">
<?php foreach ((array) $errors as $error) : ?>
<div><?= esc($error) ?></div>
<?php endforeach ?>
</div>
<?php endif ?>
<?php if (! empty($message)) : ?><span class="d-none" data-page-notification data-notification-type="success" data-notification-title="Additional Permissions" data-notification-message="<?= esc((string) $message, 'attr') ?>"></span><?php endif ?>
<?php foreach ((array) $errors as $error) : ?><span class="d-none" data-page-notification data-notification-type="error" data-notification-title="Additional Permissions" data-notification-message="<?= esc((string) $error, 'attr') ?>"></span><?php endforeach ?>
<?php
$selectedEmail = '';
$selectedPersonName = '';
@ -293,9 +285,10 @@
$.post($(form).attr('action'), $(form).serialize())
.done(function(html) {
$('#maindiv').html(html);
showPageNotifications(document.getElementById('maindiv'));
})
.fail(function() {
alert('Request failed. Please try again.');
.fail(function(xhr) {
notify('error', 'Additional Permissions', (xhr.responseJSON && xhr.responseJSON.error_text) || 'Request failed. Please try again.');
});
});
})();

View File

@ -10,13 +10,7 @@
</div>
<div class="card-body">
<?php $errors = $errors ?? session('errors'); ?>
<?php if (! empty($errors)) : ?>
<div class="alert alert-danger">
<?php foreach ((array) $errors as $error) : ?>
<div><?= esc($error) ?></div>
<?php endforeach ?>
</div>
<?php endif ?>
<?php foreach ((array) $errors as $error) : ?><span class="d-none" data-page-notification data-notification-type="error" data-notification-title="Reset Password" data-notification-message="<?= esc((string) $error, 'attr') ?>"></span><?php endforeach ?>
<div class="mb-3">
<div><strong>Login ID:</strong> <?= esc((string) ($user->username ?? '')) ?></div>
@ -97,9 +91,10 @@
$.post($(form).attr('action'), $(form).serialize())
.done(function(html) {
$('#maindiv').html(html);
showPageNotifications(document.getElementById('maindiv'));
})
.fail(function() {
alert('Request failed. Please try again.');
.fail(function(xhr) {
notify('error', 'Reset Password', (xhr.responseJSON && xhr.responseJSON.error_text) || 'Request failed. Please try again.');
});
});
})();

View File

@ -18,7 +18,6 @@
<div class="alert alert-info py-2">
<strong><?= count($sessions) ?></strong> user<?= count($sessions) === 1 ? '' : 's' ?> currently online. A user can have only one active HMS session.
</div>
<div id="user_session_notice"></div>
<div class="table-responsive">
<table class="table table-striped align-middle">
<thead>
@ -88,14 +87,15 @@
'<?= csrf_token() ?>': $('input[name="<?= csrf_token() ?>"]').first().val() || '<?= csrf_hash() ?>'
}, function(data) {
if (!data || parseInt(data.update || '0', 10) !== 1) {
$('#user_session_notice').html('<div class="alert alert-danger">' + $('<div>').text((data && data.error_text) || 'Unable to end session.').html() + '</div>');
notify('error', 'Force Logout', (data && data.error_text) || 'Unable to end session.');
button.disabled = false;
return;
}
notify('success', 'Force Logout', data.error_text || 'User session ended.');
load_form_div(listUrl, 'maindiv', 'Active Sessions');
}, 'json').fail(function(xhr) {
var message = (xhr.responseJSON && xhr.responseJSON.error_text) || 'Unable to end session.';
$('#user_session_notice').html('<div class="alert alert-danger">' + $('<div>').text(message).html() + '</div>');
notify('error', 'Force Logout', message);
button.disabled = false;
});
});

View File

@ -339,6 +339,9 @@
mainEl.innerHTML = html;
}
executeInjectedScripts('main');
if (typeof window.showPageNotifications === 'function') {
window.showPageNotifications(mainEl);
}
if (typeof initfunc === 'function') {
initfunc();
}
@ -388,6 +391,9 @@
targetEl.innerHTML = html;
}
executeInjectedScripts(xdiv);
if (typeof window.showPageNotifications === 'function') {
window.showPageNotifications(targetEl);
}
if (typeof initfunc === 'function') {
initfunc();
}
@ -479,6 +485,18 @@
}
};
}
window.showPageNotifications = function(root) {
var scope = root && root.querySelectorAll ? root : document;
scope.querySelectorAll('[data-page-notification]:not([data-notification-shown])').forEach(function(item) {
item.setAttribute('data-notification-shown', '1');
window.notify(
item.getAttribute('data-notification-type') || 'error',
item.getAttribute('data-notification-title') || 'Notification',
item.getAttribute('data-notification-message') || ''
);
});
};
</script>
<script>
(function() {