Add password visibility toggle to user and admin forms
This commit is contained in:
1 parent
e68b24e1c7
commit
7c96b952e9
5 files changed
+165
-28
No files matched your search
+45
-9
@@ -23,9 +23,36 @@ async function postForm(formData) {
|
||||
return response.json();
|
||||
}
|
||||
|
||||
const EYE_OPEN_SVG = `
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" class="icon-eye">
|
||||
<path d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7Z" />
|
||||
<circle cx="12" cy="12" r="3" />
|
||||
</svg>
|
||||
`;
|
||||
|
||||
const EYE_OFF_SVG = `
|
||||
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" class="icon-eye-off">
|
||||
<path d="M9.88 9.88a3 3 0 1 0 4.24 4.24" />
|
||||
<path d="M10.73 5.08A10.43 10.43 0 0 1 12 5c7 0 10 7 10 7a13.16 13.16 0 0 1-1.67 2.68" />
|
||||
<path d="M6.61 6.61A13.526 13.526 0 0 0 2 12s3 7 10 7a9.74 9.74 0 0 0 5.39-1.61" />
|
||||
<line x1="2" y1="2" x2="22" y2="22" />
|
||||
</svg>
|
||||
`;
|
||||
|
||||
function resetPasswordVisibility(form) {
|
||||
form.querySelectorAll('.password-toggle-btn').forEach((btn) => {
|
||||
const input = document.getElementById(btn.dataset.target);
|
||||
if (input) input.type = 'password';
|
||||
btn.setAttribute('aria-label', 'Show password');
|
||||
btn.innerHTML = EYE_OPEN_SVG;
|
||||
});
|
||||
}
|
||||
|
||||
// Add User modal
|
||||
function openUserModal() {
|
||||
document.getElementById('form-user').reset();
|
||||
const form = document.getElementById('form-user');
|
||||
form.reset();
|
||||
resetPasswordVisibility(form);
|
||||
userModal.classList.add('is-open');
|
||||
document.getElementById('user-field-username').focus();
|
||||
}
|
||||
@@ -37,8 +64,10 @@ function closeUserModal() {
|
||||
// Change Password modal
|
||||
function openPasswordModal(userId, username) {
|
||||
const isSelf = String(userId) === currentUserId;
|
||||
const form = document.getElementById('form-password');
|
||||
|
||||
document.getElementById('form-password').reset();
|
||||
form.reset();
|
||||
resetPasswordVisibility(form);
|
||||
document.getElementById('password-field-user-id').value = userId;
|
||||
document.getElementById('modal-password-title').textContent = isSelf
|
||||
? '🔑 Change Your Password'
|
||||
@@ -60,14 +89,21 @@ document.getElementById('btn-cancel-user').addEventListener('click', closeUserMo
|
||||
document.getElementById('btn-close-password-modal').addEventListener('click', closePasswordModal);
|
||||
document.getElementById('btn-cancel-password').addEventListener('click', closePasswordModal);
|
||||
|
||||
userModal.addEventListener('click', (e) => { if (e.target === userModal) closeUserModal(); });
|
||||
passwordModal.addEventListener('click', (e) => { if (e.target === passwordModal) closePasswordModal(); });
|
||||
// Password visibility toggles (works inside both modals)
|
||||
document.addEventListener('click', (e) => {
|
||||
const toggleBtn = e.target.closest('.password-toggle-btn');
|
||||
if (!toggleBtn) return;
|
||||
|
||||
window.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Escape') {
|
||||
closeUserModal();
|
||||
closePasswordModal();
|
||||
}
|
||||
const targetId = toggleBtn.dataset.target;
|
||||
const input = document.getElementById(targetId);
|
||||
if (!input) return;
|
||||
|
||||
const isPassword = input.type === 'password';
|
||||
input.type = isPassword ? 'text' : 'password';
|
||||
|
||||
toggleBtn.setAttribute('aria-label', isPassword ? 'Hide password' : 'Show password');
|
||||
toggleBtn.innerHTML = isPassword ? EYE_OFF_SVG : EYE_OPEN_SVG;
|
||||
input.focus();
|
||||
});
|
||||
|
||||
// Create user
|
||||
|
||||
Reference in new issue
Block a user