Enhance task synchronization and logging; update service worker caching strategy

This commit is contained in:
2025-01-31 18:59:16 +01:00
parent 2a7005f53c
commit b277b1f8f0
3 changed files with 45 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
import { checkSession, login, logout } from './auth.js';
import { saveTask } from './tasks.js';
import { saveTags, loadTags } from './tags.js';
import { idb } from './utils.js';
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
@@ -32,8 +33,7 @@ if ('serviceWorker' in navigator) {
});
}
document.addEventListener('DOMContentLoaded', function() {
document.addEventListener('DOMContentLoaded', async function() {
const loginForm = document.getElementById('loginForm');
const loginContainer = document.getElementById('loginContainer');
const appContainer = document.getElementById('appContainer');
@@ -55,12 +55,13 @@ document.addEventListener('DOMContentLoaded', function() {
const tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);
// Initialize datepicker with tomorrow as the default date
// Initialize datepicker with tomorrow as the default date and disable past dates
M.Datepicker.init(document.querySelectorAll('.datepicker'), {
format: 'yyyy-mm-dd',
defaultDate: tomorrow,
setDefaultDate: true,
firstDay: 1
firstDay: 1,
minDate: today // Disable past dates
});
// Initialize timepicker
@@ -187,7 +188,7 @@ document.addEventListener('DOMContentLoaded', function() {
// Save task to server or IndexedDB if offline
try {
const data = await saveTask(taskData);
document.getElementById('responseMessage').textContent = "Task saved successfully!";
document.getElementById('responseMessage').textContent = data.message;
taskForm.reset(); // Reset the form after saving the task
} catch (error) {
if (error.status === 401) {
@@ -199,4 +200,19 @@ document.addEventListener('DOMContentLoaded', function() {
}
}
});
// Synchronize tasks when back online
window.addEventListener('online', async () => {
const db = await idb.openDB('org-todo-pwa', 1);
const tasks = await db.getAll('tasks');
for (const task of tasks) {
try {
await saveTask(task);
await db.delete('tasks', task.id);
console.log(`Task synchronized: ${task.subject}`);
} catch (error) {
console.error('Error synchronizing task:', error);
}
}
});
});