89 lines
3.2 KiB
JavaScript
89 lines
3.2 KiB
JavaScript
import './bootstrap';
|
|
|
|
import localforage from 'localforage';
|
|
|
|
// Generic function to queue any offline action
|
|
window.queueOfflineAction = function(action, payload) {
|
|
const pendingAction = { action, payload };
|
|
if (navigator.onLine) {
|
|
// Send immediately
|
|
fetch('/offline/pending', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content },
|
|
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);
|
|
});
|
|
} else {
|
|
// Store in IndexedDB (via localforage)
|
|
localforage.getItem('pendingOffline').then(pending => {
|
|
const list = pending || [];
|
|
list.push(pendingAction);
|
|
localforage.setItem('pendingOffline', list);
|
|
console.log('Action stored offline:', action);
|
|
}).catch(err => {
|
|
console.error('Error storing offline action:', err);
|
|
});
|
|
}
|
|
};
|
|
|
|
// 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);
|
|
};
|
|
|
|
// Function to capture and store image for offline upload
|
|
window.captureAndStoreImage = async (file, phaseId, description = '') => {
|
|
try {
|
|
// Convert file to base64
|
|
const base64 = await new Promise((resolve, reject) => {
|
|
const reader = new FileReader();
|
|
reader.onload = () => resolve(reader.result.split(',')[1]); // Get just the base64 part
|
|
reader.onerror = reject;
|
|
reader.readAsDataURL(file);
|
|
});
|
|
|
|
const payload = {
|
|
file: base64,
|
|
path: `media/${new Date().getTime()}_${file.name}`, // Simple path, could be improved
|
|
model_type: 'App\\Models\\Phase',
|
|
model_id: phaseId,
|
|
name: file.name,
|
|
description: description,
|
|
mime_type: file.type
|
|
};
|
|
|
|
window.queueOfflineAction('media_upload', payload);
|
|
return true;
|
|
} catch (error) {
|
|
console.error('Error capturing image:', error);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
// 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);
|
|
})
|
|
.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.
|