Edoruin commited on
Commit
34cfba1
·
1 Parent(s): b723304

Fix IndexedDB DataError by using numeric keys and add navigation error handling

Browse files
static/js/app.js CHANGED
@@ -36,27 +36,43 @@ const app = {
36
  * Navigate to a route
37
  */
38
  async navigate(route) {
 
39
  if (!this.routes[route]) {
40
  console.error(`Route not found: ${route}`);
41
  return;
42
  }
43
 
44
- // Destroy previous component
45
- if (this.currentComponent && this.currentComponent.destroy) {
46
- this.currentComponent.destroy();
47
- }
 
48
 
49
- // Update current route
50
- this.currentRoute = route;
51
- this.currentComponent = this.routes[route];
52
 
53
- // Initialize component
54
- if (this.currentComponent.init) {
55
- await this.currentComponent.init();
56
- }
57
 
58
- // Render component
59
- this.render();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  },
61
 
62
  /**
 
36
  * Navigate to a route
37
  */
38
  async navigate(route) {
39
+ console.log(`Navigating to: ${route}`);
40
  if (!this.routes[route]) {
41
  console.error(`Route not found: ${route}`);
42
  return;
43
  }
44
 
45
+ try {
46
+ // Destroy previous component
47
+ if (this.currentComponent && this.currentComponent.destroy) {
48
+ this.currentComponent.destroy();
49
+ }
50
 
51
+ // Update current route
52
+ this.currentRoute = route;
53
+ this.currentComponent = this.routes[route];
54
 
55
+ // Initialize component
56
+ if (this.currentComponent.init) {
57
+ await this.currentComponent.init();
58
+ }
59
 
60
+ // Render component
61
+ this.render();
62
+ } catch (error) {
63
+ console.error(`Navigation to ${route} failed:`, error);
64
+ // Fallback render or simple error message
65
+ const appContainer = document.getElementById('app');
66
+ if (appContainer) {
67
+ appContainer.innerHTML = `
68
+ <div class="card">
69
+ <h2>Error de Carga</h2>
70
+ <p>No se pudo cargar la vista solicitada. Por favor, intenta de nuevo.</p>
71
+ <button class="btn-primary" onclick="window.location.reload()">Refrescar App</button>
72
+ </div>
73
+ `;
74
+ }
75
+ }
76
  },
77
 
78
  /**
static/js/components/capture-form.js CHANGED
@@ -143,7 +143,7 @@ const CaptureFormComponent = {
143
  items: items,
144
  fishingMethod: document.getElementById('fishing-method').value,
145
  depth: parseFloat(document.getElementById('depth').value),
146
- synced: false
147
  };
148
 
149
  // Save to IndexedDB
 
143
  items: items,
144
  fishingMethod: document.getElementById('fishing-method').value,
145
  depth: parseFloat(document.getElementById('depth').value),
146
+ synced: 0
147
  };
148
 
149
  // Save to IndexedDB
static/js/components/history.js CHANGED
@@ -38,10 +38,10 @@ const HistoryComponent = {
38
  return this.captures
39
  .sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp))
40
  .map(capture => `
41
- <li class="history-item ${capture.synced ? 'synced' : 'unsynced'}">
42
  <h3>
43
  ${this.formatDate(capture.timestamp)}
44
- ${capture.synced ? '✅' : '⏳'}
45
  </h3>
46
 
47
  <p>
 
38
  return this.captures
39
  .sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp))
40
  .map(capture => `
41
+ <li class="history-item ${capture.synced === 1 ? 'synced' : 'unsynced'}">
42
  <h3>
43
  ${this.formatDate(capture.timestamp)}
44
+ ${capture.synced === 1 ? '✅' : '⏳'}
45
  </h3>
46
 
47
  <p>
static/js/db.js CHANGED
@@ -33,7 +33,7 @@ const dbService = {
33
  * Get unsynced captures
34
  */
35
  async getUnsyncedCaptures() {
36
- return await db.captures.where('synced').equals(false).toArray();
37
  },
38
 
39
  /**
@@ -41,7 +41,7 @@ const dbService = {
41
  */
42
  async updateSyncStatus(id, synced) {
43
  return await db.captures.update(id, {
44
- synced: synced,
45
  syncedAt: new Date()
46
  });
47
  },
 
33
  * Get unsynced captures
34
  */
35
  async getUnsyncedCaptures() {
36
+ return await db.captures.where('synced').equals(0).toArray();
37
  },
38
 
39
  /**
 
41
  */
42
  async updateSyncStatus(id, synced) {
43
  return await db.captures.update(id, {
44
+ synced: synced ? 1 : 0,
45
  syncedAt: new Date()
46
  });
47
  },
static/js/sync.js CHANGED
@@ -11,11 +11,13 @@ const syncService = {
11
  /**
12
  * Initialize sync service
13
  */
14
- init() {
 
 
15
  // Listen for online/offline events
16
  window.addEventListener('online', () => {
17
  console.log('Connection restored, syncing...');
18
- this.syncNow();
19
  });
20
 
21
  window.addEventListener('offline', () => {
@@ -25,13 +27,17 @@ const syncService = {
25
  // Auto-sync every 5 minutes when online
26
  this.syncInterval = setInterval(() => {
27
  if (navigator.onLine) {
28
- this.syncNow();
29
  }
30
  }, 5 * 60 * 1000);
31
 
32
- // Initial sync if online
33
  if (navigator.onLine) {
34
- this.syncNow();
 
 
 
 
35
  }
36
  },
37
 
 
11
  /**
12
  * Initialize sync service
13
  */
14
+ async init() {
15
+ console.log('Sync service initializing...');
16
+
17
  // Listen for online/offline events
18
  window.addEventListener('online', () => {
19
  console.log('Connection restored, syncing...');
20
+ this.syncNow().catch(err => console.error('Auto-sync online failed:', err));
21
  });
22
 
23
  window.addEventListener('offline', () => {
 
27
  // Auto-sync every 5 minutes when online
28
  this.syncInterval = setInterval(() => {
29
  if (navigator.onLine) {
30
+ this.syncNow().catch(err => console.error('Periodic auto-sync failed:', err));
31
  }
32
  }, 5 * 60 * 1000);
33
 
34
+ // Initial sync if online - wrapped in try-catch for stability
35
  if (navigator.onLine) {
36
+ try {
37
+ await this.syncNow();
38
+ } catch (error) {
39
+ console.warn('Initial sync failed (offline mode fallback):', error);
40
+ }
41
  }
42
  },
43