feat: Add offline media capture capability and enhance offline sync system with comprehensive action type support

This commit is contained in:
2026-05-25 18:41:54 +02:00
parent d4d5097fe2
commit 07ffce437f
+29 -3
View File
@@ -42,6 +42,35 @@ window.offlineProgressUpdate = function(phaseId, progress, comment, location) {
window.queueOfflineAction('progress_update', payload); 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 // Sync pending actions when online
window.addEventListener('online', () => { window.addEventListener('online', () => {
// Trigger a sync attempt // Trigger a sync attempt
@@ -49,9 +78,6 @@ window.addEventListener('online', () => {
.then(res => res.json()) .then(res => res.json())
.then(data => { .then(data => {
console.log('Synced:', 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 => { .catch(err => {
console.error('Error triggering sync:', err); console.error('Error triggering sync:', err);