2026-05-07 23:31:33 +02:00
|
|
|
import './bootstrap';
|
|
|
|
|
|
|
|
|
|
import localforage from 'localforage';
|
|
|
|
|
|
2026-05-25 17:59:03 +02:00
|
|
|
// Generic function to queue any offline action
|
|
|
|
|
window.queueOfflineAction = function(action, payload) {
|
|
|
|
|
const pendingAction = { action, payload };
|
2026-05-07 23:31:33 +02:00
|
|
|
if (navigator.onLine) {
|
2026-05-25 17:59:03 +02:00
|
|
|
// Send immediately
|
2026-05-07 23:31:33 +02:00
|
|
|
fetch('/offline/pending', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content },
|
2026-05-25 17:59:03 +02:00
|
|
|
body: JSON.stringify(pendingAction)
|
|
|
|
|
})
|
|
|
|
|
.then(res => res.json())
|
|
|
|
|
.then(data => {
|
|
|
|
|
if (data.queued) {
|
|
|
|
|
console.log('Action queued:', action);
|
|
|
|
|
} else {
|
|
|
|
|
console.error('Failed to queue action:', data);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
console.error('Error queuing action:', err);
|
|
|
|
|
});
|
2026-05-07 23:31:33 +02:00
|
|
|
} else {
|
2026-05-25 17:59:03 +02:00
|
|
|
// Store in IndexedDB (via localforage)
|
2026-05-07 23:31:33 +02:00
|
|
|
localforage.getItem('pendingOffline').then(pending => {
|
|
|
|
|
const list = pending || [];
|
2026-05-25 17:59:03 +02:00
|
|
|
list.push(pendingAction);
|
2026-05-07 23:31:33 +02:00
|
|
|
localforage.setItem('pendingOffline', list);
|
2026-05-25 17:59:03 +02:00
|
|
|
console.log('Action stored offline:', action);
|
|
|
|
|
}).catch(err => {
|
|
|
|
|
console.error('Error storing offline action:', err);
|
2026-05-07 23:31:33 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-05-25 17:59:03 +02:00
|
|
|
|
|
|
|
|
// Function to store offline progress update (for backward compatibility)
|
|
|
|
|
window.offlineProgressUpdate = function(phaseId, progress, comment, location) {
|
|
|
|
|
const payload = { phase_id: phaseId, progress, comment, location };
|
|
|
|
|
window.queueOfflineAction('progress_update', payload);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Sync pending actions when online
|
|
|
|
|
window.addEventListener('online', () => {
|
|
|
|
|
// Trigger a sync attempt
|
|
|
|
|
fetch('/offline/sync', { method: 'POST', headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content } })
|
|
|
|
|
.then(res => res.json())
|
|
|
|
|
.then(data => {
|
|
|
|
|
console.log('Synced:', data);
|
|
|
|
|
// Optionally, clear the local pending list if the sync was successful
|
|
|
|
|
// But note: the backend will mark them as synced, so we rely on the service worker to clean up?
|
|
|
|
|
// We'll leave it to the service worker to handle the state.
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
console.error('Error triggering sync:', err);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Also, we can listen for the service worker's message to update the UI if needed
|
|
|
|
|
// But for now, we'll rely on the service worker's notification and the online event.
|