added offline mode for xinon app
This commit is contained in:
@@ -3,8 +3,13 @@
|
||||
*
|
||||
* Main module for workorder management in MobileApp.
|
||||
* Provides list view and full-screen detail view with collapsible cards.
|
||||
* Supports offline mode via WorkorderOfflineService.
|
||||
*/
|
||||
|
||||
import workorderService from '/mobile/shared/workorderOfflineService.js';
|
||||
import photoQueue from '/mobile/shared/photoQueue.js';
|
||||
import { isOfflineModeEnabled } from '/mobile/shared/offlineSettings.js';
|
||||
|
||||
export default {
|
||||
name: 'WorkorderModule',
|
||||
emits: ['navigate', 'toast', 'detail-open', 'detail-close'],
|
||||
@@ -184,15 +189,14 @@ export default {
|
||||
const fetchWorkorders = async () => {
|
||||
isLoading.value = true;
|
||||
try {
|
||||
const response = await fetch('/MobileApp/Workorder/Workorder/get', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'include',
|
||||
body: JSON.stringify({ pagination: { page: 1, per_page: 500 } })
|
||||
});
|
||||
const data = await response.json();
|
||||
const data = await workorderService.getWorkorders({ pagination: { page: 1, per_page: 500 } });
|
||||
if (data.success) {
|
||||
workorders.value = data.workorders;
|
||||
if (data.fromCache) {
|
||||
console.log('[WorkorderModule] Loaded from cache');
|
||||
}
|
||||
} else if (data.offline) {
|
||||
emit('toast', 'Offline - Daten aus Cache', 'info');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching workorders:', error);
|
||||
@@ -209,9 +213,8 @@ export default {
|
||||
emit('detail-open', workorder.id);
|
||||
|
||||
try {
|
||||
// Fetch all workorder details in a single request
|
||||
const response = await fetch(`/MobileApp/Workorder/Workorder/getWorkorderDetail?id=${workorder.id}`, { credentials: 'include' });
|
||||
const data = await response.json();
|
||||
// Fetch all workorder details via offline service
|
||||
const data = await workorderService.getWorkorderDetail(workorder.id);
|
||||
|
||||
if (data.success) {
|
||||
selectedWorkorder.value = data.workorder;
|
||||
@@ -219,10 +222,16 @@ export default {
|
||||
cableLength: data.workorder.cableLength || '',
|
||||
cableType: data.workorder.cableType || ''
|
||||
};
|
||||
documentation.value = { docs: data.docs, journals: data.journals };
|
||||
documentation.value = data.documentation || { docs: [], journals: [] };
|
||||
tenantConfig.value = data.tenantConfig;
|
||||
checklist.value = data.checklist;
|
||||
checklist.value = data.checklist || [];
|
||||
technicalData.value = data.technicalData || null;
|
||||
if (data.fromCache) {
|
||||
console.log('[WorkorderModule] Detail loaded from cache');
|
||||
}
|
||||
} else if (data.offline) {
|
||||
emit('toast', 'Offline - keine gecachten Details', 'error');
|
||||
closeDetail();
|
||||
} else {
|
||||
emit('toast', data.message || 'Fehler beim Laden', 'error');
|
||||
}
|
||||
@@ -261,24 +270,20 @@ export default {
|
||||
|
||||
const saveNotes = async () => {
|
||||
try {
|
||||
const response = await fetch('/MobileApp/Workorder/Workorder/updateAdditionalInfo', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'include',
|
||||
body: JSON.stringify({
|
||||
workorderId: selectedWorkorder.value.id,
|
||||
additionalInfo: tempNotes.value
|
||||
})
|
||||
});
|
||||
const data = await response.json();
|
||||
const data = await workorderService.updateNotes(selectedWorkorder.value.id, tempNotes.value);
|
||||
if (data.success) {
|
||||
selectedWorkorder.value.additionalInfo = data.newInfo;
|
||||
selectedWorkorder.value.additionalInfo = tempNotes.value;
|
||||
isEditingNotes.value = false;
|
||||
emit('toast', 'Notiz gespeichert', 'success');
|
||||
// Refresh journals
|
||||
const docRes = await fetch(`/MobileApp/Workorder/Workorder/getDocumentation?workorderId=${selectedWorkorder.value.id}`, { credentials: 'include' });
|
||||
const docData = await docRes.json();
|
||||
if (docData.success) documentation.value.journals = docData.journals;
|
||||
if (data.queued) {
|
||||
emit('toast', 'Notiz wird synchronisiert', 'info');
|
||||
} else {
|
||||
emit('toast', 'Notiz gespeichert', 'success');
|
||||
// Refresh detail to get updated journals
|
||||
const detailData = await workorderService.getWorkorderDetail(selectedWorkorder.value.id);
|
||||
if (detailData.success && detailData.documentation) {
|
||||
documentation.value.journals = detailData.documentation.journals || [];
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
emit('toast', 'Fehler beim Speichern', 'error');
|
||||
@@ -289,20 +294,22 @@ export default {
|
||||
const addJournalEntry = async () => {
|
||||
if (!newJournalText.value.trim()) return;
|
||||
try {
|
||||
const response = await fetch('/MobileApp/Workorder/Workorder/addJournal', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'include',
|
||||
body: JSON.stringify({
|
||||
workorderId: selectedWorkorder.value.id,
|
||||
text: newJournalText.value
|
||||
})
|
||||
});
|
||||
const data = await response.json();
|
||||
const data = await workorderService.addJournal(selectedWorkorder.value.id, newJournalText.value);
|
||||
if (data.success) {
|
||||
documentation.value.journals = data.journals;
|
||||
if (data.queued) {
|
||||
// Add pending journal entry to local display
|
||||
documentation.value.journals.unshift({
|
||||
id: 'pending-' + data.localId,
|
||||
text: newJournalText.value,
|
||||
create: Math.floor(Date.now() / 1000),
|
||||
_pending: true
|
||||
});
|
||||
emit('toast', 'Eintrag wird synchronisiert', 'info');
|
||||
} else if (data.journals) {
|
||||
documentation.value.journals = data.journals;
|
||||
emit('toast', 'Eintrag hinzugefügt', 'success');
|
||||
}
|
||||
newJournalText.value = '';
|
||||
emit('toast', 'Eintrag hinzugefügt', 'success');
|
||||
}
|
||||
} catch (error) {
|
||||
emit('toast', 'Fehler beim Hinzufügen', 'error');
|
||||
@@ -312,19 +319,17 @@ export default {
|
||||
// Cable data
|
||||
const saveCableData = async () => {
|
||||
try {
|
||||
const response = await fetch('/MobileApp/Workorder/Workorder/updateWorkorderData', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'include',
|
||||
body: JSON.stringify({
|
||||
workorderId: selectedWorkorder.value.id,
|
||||
cableLength: cableDataForm.value.cableLength,
|
||||
cableType: cableDataForm.value.cableType
|
||||
})
|
||||
});
|
||||
const data = await response.json();
|
||||
const data = await workorderService.updateCableData(
|
||||
selectedWorkorder.value.id,
|
||||
cableDataForm.value.cableLength,
|
||||
cableDataForm.value.cableType
|
||||
);
|
||||
if (data.success) {
|
||||
emit('toast', 'Daten gespeichert', 'success');
|
||||
if (data.queued) {
|
||||
emit('toast', 'Daten werden synchronisiert', 'info');
|
||||
} else {
|
||||
emit('toast', 'Daten gespeichert', 'success');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
emit('toast', 'Fehler beim Speichern', 'error');
|
||||
@@ -351,6 +356,45 @@ export default {
|
||||
|
||||
isUploading.value = true;
|
||||
const wasFromChecklist = pendingChecklistUpload.value !== null;
|
||||
|
||||
// Check if offline mode is enabled and we're offline
|
||||
const offlineEnabled = isOfflineModeEnabled();
|
||||
const isOffline = !navigator.onLine;
|
||||
|
||||
if (offlineEnabled && isOffline) {
|
||||
// Queue files for later upload
|
||||
try {
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const result = await photoQueue.queue(
|
||||
selectedWorkorder.value.id,
|
||||
files[i],
|
||||
uploadDocType.value,
|
||||
''
|
||||
);
|
||||
if (result.success) {
|
||||
// Add pending doc to local display
|
||||
documentation.value.docs.push({
|
||||
id: 'pending-' + result.localId,
|
||||
documentType: uploadDocType.value,
|
||||
fileName: files[i].name,
|
||||
_pending: true
|
||||
});
|
||||
}
|
||||
}
|
||||
triggerHaptic('light');
|
||||
emit('toast', 'Foto wird bei Verbindung hochgeladen', 'info');
|
||||
showDocUploadSheet.value = false;
|
||||
} catch (error) {
|
||||
emit('toast', 'Fehler beim Speichern', 'error');
|
||||
} finally {
|
||||
isUploading.value = false;
|
||||
if (fileInputRef.value) fileInputRef.value.value = '';
|
||||
pendingChecklistUpload.value = null;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Online upload
|
||||
const formData = new FormData();
|
||||
formData.append('workorderId', selectedWorkorder.value.id);
|
||||
formData.append('documentType', uploadDocType.value);
|
||||
@@ -372,14 +416,11 @@ export default {
|
||||
showDocUploadSheet.value = false;
|
||||
|
||||
// Refresh documentation and checklist
|
||||
const [docRes, checkRes] = await Promise.all([
|
||||
fetch(`/MobileApp/Workorder/Workorder/getDocumentation?workorderId=${selectedWorkorder.value.id}`, { credentials: 'include' }),
|
||||
fetch(`/MobileApp/Workorder/Workorder/getChecklist?workorderId=${selectedWorkorder.value.id}`, { credentials: 'include' })
|
||||
]);
|
||||
const docData = await docRes.json();
|
||||
const checkData = await checkRes.json();
|
||||
if (docData.success) documentation.value = { docs: docData.docs, journals: docData.journals };
|
||||
if (checkData.success) checklist.value = checkData.checklist;
|
||||
const detailData = await workorderService.getWorkorderDetail(selectedWorkorder.value.id);
|
||||
if (detailData.success) {
|
||||
documentation.value = detailData.documentation || { docs: [], journals: [] };
|
||||
checklist.value = detailData.checklist || [];
|
||||
}
|
||||
|
||||
// Auto-advance: If upload was from checklist, open camera for next item
|
||||
if (wasFromChecklist) {
|
||||
@@ -395,7 +436,15 @@ export default {
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
emit('toast', 'Upload fehlgeschlagen', 'error');
|
||||
// If upload fails and offline mode is enabled, queue for later
|
||||
if (offlineEnabled) {
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
await photoQueue.queue(selectedWorkorder.value.id, files[i], uploadDocType.value, '');
|
||||
}
|
||||
emit('toast', 'Foto wird bei Verbindung hochgeladen', 'info');
|
||||
} else {
|
||||
emit('toast', 'Upload fehlgeschlagen', 'error');
|
||||
}
|
||||
} finally {
|
||||
isUploading.value = false;
|
||||
if (fileInputRef.value) fileInputRef.value.value = '';
|
||||
@@ -418,19 +467,17 @@ export default {
|
||||
const typeText = tenantConfig.value?.interventionTypes?.find(t => t.value === problemType.value)?.text || problemType.value;
|
||||
|
||||
try {
|
||||
const response = await fetch('/MobileApp/Workorder/Workorder/requestIntervention', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'include',
|
||||
body: JSON.stringify({
|
||||
workorderId: selectedWorkorder.value.id,
|
||||
interventionType: typeText,
|
||||
journalText: problemComment.value || typeText
|
||||
})
|
||||
});
|
||||
const data = await response.json();
|
||||
const data = await workorderService.requestIntervention(
|
||||
selectedWorkorder.value.id,
|
||||
typeText,
|
||||
problemComment.value || typeText
|
||||
);
|
||||
if (data.success) {
|
||||
emit('toast', 'Problem gemeldet', 'success');
|
||||
if (data.queued) {
|
||||
emit('toast', 'Problem wird bei Verbindung gemeldet', 'info');
|
||||
} else {
|
||||
emit('toast', 'Problem gemeldet', 'success');
|
||||
}
|
||||
showProblemSheet.value = false;
|
||||
closeDetail();
|
||||
fetchWorkorders();
|
||||
@@ -449,21 +496,16 @@ export default {
|
||||
|
||||
const submitComplete = async () => {
|
||||
try {
|
||||
const response = await fetch('/MobileApp/Workorder/Workorder/completeWorkorder', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'include',
|
||||
body: JSON.stringify({
|
||||
workorderId: selectedWorkorder.value.id
|
||||
})
|
||||
});
|
||||
const data = await response.json();
|
||||
const data = await workorderService.completeWorkorder(selectedWorkorder.value.id);
|
||||
if (data.success) {
|
||||
triggerHaptic('success');
|
||||
emit('toast', 'Auftrag abgeschlossen', 'success');
|
||||
showCompleteSheet.value = false;
|
||||
closeDetail();
|
||||
fetchWorkorders();
|
||||
} else if (data.blocked) {
|
||||
// Completion blocked when offline
|
||||
emit('toast', data.message || 'Bitte zuerst synchronisieren', 'warning');
|
||||
} else {
|
||||
emit('toast', data.message || 'Fehler', 'error');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user