This commit is contained in:
2025-05-01 14:42:12 -06:00
parent 693d11c506
commit bd59f428ba
13 changed files with 288 additions and 124 deletions
+72
View File
@@ -0,0 +1,72 @@
const form = document.querySelector('#login_form');
const username_field = form.querySelector('#username');
const password_field = form.querySelector('#password');
const password_hash_field = form.querySelector('#password_hash');
const submit_button = form.querySelector('input[type="submit"]');
submit_button.addEventListener('click', async (e) => {
e.preventDefault();
const password_hash = await hashString(password_field.value);
const urlParams = new URLSearchParams(document.location.search);
const redirect = decodeURI(urlParams.get('redirect'));
requestAuth(username_field.value, password_hash, redirect);
});
function requestAuth(username, password_hash, redirect) {
fetch('http://localhost:7477/auth', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'username=' + username + '&password_hash=' + password_hash,
})
.then(response => {
if (!response.ok) {
console.log('Login Failed!');
}
return response.json(); // Parse response body as JSON
})
.then(data => {
notifyServer(data["id"], username, 1, redirect);
});
}
function notifyServer(id, username, role, redirect) {
fetch('director.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'ajax_id=login&user_id=' + id + '&username=' + username + '&role=' + role,
})
.then(response => {
return response.json();
})
.then(data => {
if ('startViewTransition' in document) {
document.startViewTransition(() => {
if (redirect == null) {
window.location.href = 'articles.php?view=list&sort=new';
} else {
window.location.href = redirect;
}
});
} else {
// Fallback for browsers that don't support View Transitions
if (redirect == null) {
window.location.href = 'articles.php?view=list&sort=new';
} else {
window.location.href = redirect;
}
}
});
}
async function hashString(str) {
const encoded = new TextEncoder().encode(str);
const digest = await crypto.subtle.digest('SHA-256', encoded);
const hashArray = Array.from(new Uint8Array(digest));
const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
return hashHex;
}
+16
View File
@@ -0,0 +1,16 @@
const nav = document.querySelector('nav');
const nav_links = nav.querySelectorAll('ul li a');
const theme_toggle = nav.querySelector('button');
nav_links.forEach((el) => {
el.addEventListener('click', () => {
el.preventDefault();
if ('startViewTransition' in document) {
document.startViewTransition(() => {
window.location.href = el.getAttribute('href');
});
} else {
window.location.href = el.getAttribute('href');
}
});
});
+2 -63
View File
@@ -15,72 +15,11 @@
document.addEventListener('DOMContentLoaded', function() {
var todos = document.getElementsByClassName('todo_item');
var action_items = (document.getElementById('todo_actions')).querySelectorAll('button');
// General Modal handling
function handle_modal(modal) {
console.log('handling modal');
let modal_close = modal.querySelector('.modal_close');
let submit_button = modal.querySelector('input[type="submit"]');
modal.style.display = 'block';
modal_close.addEventListener('click', function() {
modal.style.display = "none";
});
submit_button.addEventListener('click', function(e) {
e.preventDefault();
let todo = {
'title': modal.querySelector('#title').value,
'description': modal.querySelector('#description'),
'priority': modal.querySelector('#priority'),
'due_date': modal.querySelector('#due_date'),
};
fetch('director.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'ajax_id=todo&action_id=create_update_todo&todo_id=' + this.todo_id + '&todo=' + encodeURIComponent(JSON.stringify(todo)),
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json(); // Or handle other response formats
})
.then(responseData => {
console.log('Response received:', responseData);
// Handle the server's response
});
});
}
for (let i = 0; i < action_items.length; i++) {
let action = action_items[i];
action.addEventListener('click', function(e) {
e.preventDefault();
switch (action.value) {
case 'new':
let new_todo_item = document.querySelector('#new_todo_item');
handle_modal(new_todo_item);
break;
}
});
}
//var todos = document.querySelectorAll('todo_item');
for (let i = 0; i < todos.length; i++) {
let item = todos[i];
var item = todos[i];
// Detailed todo item view via a modal
let modal_button = item.querySelector('.modal_activation_area');
let modal = item.querySelector('.modal');
modal_button.addEventListener('click', () => handle_modal(modal));
// Checkboxes & AJAX for updating the DB
var checkbox = item.querySelector('input[type="checkbox"]');
checkbox.todo_id = item.querySelector('input[name="todo_id"]').value;
checkbox.is_checked = checkbox.checked;