trenesky commited on
Commit
f24850b
·
verified ·
1 Parent(s): 28e7a2d

sync should work automaticaly, when note is added to be remembered on notes (online) - Follow Up Deployment

Browse files
Files changed (2) hide show
  1. index.html +93 -36
  2. prompts.txt +2 -1
index.html CHANGED
@@ -6,6 +6,9 @@
6
  <meta name="mobile-web-app-capable" content="yes">
7
  <meta name="theme-color" content="#3b82f6">
8
  <title>Shared Notes</title>
 
 
 
9
  <script src="https://cdn.tailwindcss.com"></script>
10
  <script src="https://unpkg.com/feather-icons"></script>
11
  <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
@@ -105,9 +108,23 @@
105
  }
106
  feather.replace();
107
 
108
- // Simple mock database (in a real app, you'd use Firebase or similar)
109
- let notes = JSON.parse(localStorage.getItem('shared-notes')) || [];
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  let lastSync = new Date();
 
111
 
112
  // DOM Elements
113
  const noteInput = document.getElementById('note-input');
@@ -118,13 +135,13 @@
118
  // Render notes
119
  function renderNotes() {
120
  notesList.innerHTML = '';
121
- notes.forEach((note, index) => {
122
  const li = document.createElement('li');
123
  li.className = 'note-item flex items-center justify-between p-4 text-lg bg-gray-50 dark:bg-gray-700 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-600 transition-colors duration-200';
124
  li.innerHTML = `
125
  <span class="text-gray-800 dark:text-gray-200">${note.text}</span>
126
  <button
127
- data-id="${index}"
128
  class="delete-btn opacity-0 group-hover:opacity-100 text-red-500 hover:text-red-700 dark:hover:text-red-400 transition-opacity duration-200"
129
  >
130
  <i data-feather="trash-2" class="w-4 h-4"></i>
@@ -135,31 +152,60 @@
135
  feather.replace();
136
  }
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  // Add new note
139
  function addNote() {
140
  const text = noteInput.value.trim();
141
  if (text) {
142
- notes.push({ text });
143
- noteInput.value = '';
144
- saveNotes();
145
- renderNotes();
146
- simulateSync();
 
 
 
 
 
 
 
147
  }
148
  }
149
 
150
  // Delete note
151
- function deleteNote(index) {
152
- notes.splice(index, 1);
153
- saveNotes();
154
- renderNotes();
155
- simulateSync();
156
- }
157
-
158
- // Save to localStorage
159
- function saveNotes() {
160
- localStorage.setItem('shared-notes', JSON.stringify(notes));
161
- lastSync = new Date();
162
- updateSyncTime();
163
  }
164
 
165
  // Update sync time display
@@ -176,15 +222,6 @@
176
  return date.toLocaleString();
177
  }
178
 
179
- // Simulate sync with other users (in a real app, you'd use WebSockets)
180
- function simulateSync() {
181
- setTimeout(() => {
182
- // In a real app, this would sync with a server
183
- lastSync = new Date();
184
- updateSyncTime();
185
- }, 1000);
186
- }
187
-
188
  // Event listeners
189
  addBtn.addEventListener('click', addNote);
190
  noteInput.addEventListener('keypress', (e) => {
@@ -193,14 +230,36 @@
193
 
194
  notesList.addEventListener('click', (e) => {
195
  if (e.target.closest('.delete-btn')) {
196
- const index = e.target.closest('.delete-btn').dataset.id;
197
- deleteNote(index);
198
  }
199
  });
200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  // Initial render
202
- renderNotes();
203
- updateSyncTime();
204
 
205
  // Prevent zoom on input focus for mobile
206
  document.addEventListener('touchstart', function(e) {
@@ -209,8 +268,6 @@
209
  }
210
  }, { passive: false });
211
 
212
- // Simulate periodic sync (every 30 seconds)
213
- setInterval(simulateSync, 30000);
214
 
215
  // Add touch feedback for buttons
216
  document.querySelectorAll('button').forEach(btn => {
 
6
  <meta name="mobile-web-app-capable" content="yes">
7
  <meta name="theme-color" content="#3b82f6">
8
  <title>Shared Notes</title>
9
+ <!-- Firebase SDK -->
10
+ <script src="https://www.gstatic.com/firebasejs/9.6.0/firebase-app-compat.js"></script>
11
+ <script src="https://www.gstatic.com/firebasejs/9.6.0/firebase-firestore-compat.js"></script>
12
  <script src="https://cdn.tailwindcss.com"></script>
13
  <script src="https://unpkg.com/feather-icons"></script>
14
  <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
 
108
  }
109
  feather.replace();
110
 
111
+ // Firebase configuration
112
+ const firebaseConfig = {
113
+ apiKey: "AIzaSyDpQZq8n3J3Z3X3X3X3X3X3X3X3X3X3X3X",
114
+ authDomain: "shared-notes-app.firebaseapp.com",
115
+ projectId: "shared-notes-app",
116
+ storageBucket: "shared-notes-app.appspot.com",
117
+ messagingSenderId: "123456789012",
118
+ appId: "1:123456789012:web:3X3X3X3X3X3X3X3X3X3X3X"
119
+ };
120
+
121
+ // Initialize Firebase
122
+ const app = firebase.initializeApp(firebaseConfig);
123
+ const db = firebase.firestore();
124
+
125
+ let notes = [];
126
  let lastSync = new Date();
127
+ let unsubscribe = null;
128
 
129
  // DOM Elements
130
  const noteInput = document.getElementById('note-input');
 
135
  // Render notes
136
  function renderNotes() {
137
  notesList.innerHTML = '';
138
+ notes.forEach((note) => {
139
  const li = document.createElement('li');
140
  li.className = 'note-item flex items-center justify-between p-4 text-lg bg-gray-50 dark:bg-gray-700 rounded-lg hover:bg-gray-100 dark:hover:bg-gray-600 transition-colors duration-200';
141
  li.innerHTML = `
142
  <span class="text-gray-800 dark:text-gray-200">${note.text}</span>
143
  <button
144
+ data-id="${note.id}"
145
  class="delete-btn opacity-0 group-hover:opacity-100 text-red-500 hover:text-red-700 dark:hover:text-red-400 transition-opacity duration-200"
146
  >
147
  <i data-feather="trash-2" class="w-4 h-4"></i>
 
152
  feather.replace();
153
  }
154
 
155
+ // Subscribe to real-time updates
156
+ function subscribeToNotes() {
157
+ unsubscribe = db.collection('notes')
158
+ .orderBy('createdAt', 'desc')
159
+ .onSnapshot((snapshot) => {
160
+ notes = [];
161
+ snapshot.forEach((doc) => {
162
+ notes.push({
163
+ id: doc.id,
164
+ text: doc.data().text,
165
+ createdAt: doc.data().createdAt
166
+ });
167
+ });
168
+ lastSync = new Date();
169
+ updateSyncTime();
170
+ renderNotes();
171
+ }, (error) => {
172
+ console.error("Error getting notes:", error);
173
+ document.getElementById('connection-status').innerHTML = `
174
+ <span class="w-2 h-2 rounded-full bg-red-500"></span>
175
+ <span>Disconnected</span>
176
+ `;
177
+ });
178
+ }
179
+
180
  // Add new note
181
  function addNote() {
182
  const text = noteInput.value.trim();
183
  if (text) {
184
+ db.collection('notes').add({
185
+ text: text,
186
+ createdAt: firebase.firestore.FieldValue.serverTimestamp()
187
+ })
188
+ .then(() => {
189
+ noteInput.value = '';
190
+ lastSync = new Date();
191
+ updateSyncTime();
192
+ })
193
+ .catch((error) => {
194
+ console.error("Error adding note:", error);
195
+ });
196
  }
197
  }
198
 
199
  // Delete note
200
+ function deleteNote(id) {
201
+ db.collection('notes').doc(id).delete()
202
+ .then(() => {
203
+ lastSync = new Date();
204
+ updateSyncTime();
205
+ })
206
+ .catch((error) => {
207
+ console.error("Error deleting note:", error);
208
+ });
 
 
 
209
  }
210
 
211
  // Update sync time display
 
222
  return date.toLocaleString();
223
  }
224
 
 
 
 
 
 
 
 
 
 
225
  // Event listeners
226
  addBtn.addEventListener('click', addNote);
227
  noteInput.addEventListener('keypress', (e) => {
 
230
 
231
  notesList.addEventListener('click', (e) => {
232
  if (e.target.closest('.delete-btn')) {
233
+ const id = e.target.closest('.delete-btn').dataset.id;
234
+ deleteNote(id);
235
  }
236
  });
237
 
238
+ // Clean up on page unload
239
+ window.addEventListener('beforeunload', () => {
240
+ if (unsubscribe) unsubscribe();
241
+ });
242
+
243
+ // Initialize app
244
+ function initApp() {
245
+ subscribeToNotes();
246
+ updateSyncTime();
247
+
248
+ // Set up connection state listener
249
+ firebase.firestore().enableNetwork()
250
+ .then(() => {
251
+ document.getElementById('connection-status').innerHTML = `
252
+ <span class="w-2 h-2 rounded-full bg-green-500 animate-pulse"></span>
253
+ <span>Connected</span>
254
+ `;
255
+ })
256
+ .catch((error) => {
257
+ console.error("Error connecting:", error);
258
+ });
259
+ }
260
+
261
  // Initial render
262
+ initApp();
 
263
 
264
  // Prevent zoom on input focus for mobile
265
  document.addEventListener('touchstart', function(e) {
 
268
  }
269
  }, { passive: false });
270
 
 
 
271
 
272
  // Add touch feedback for buttons
273
  document.querySelectorAll('button').forEach(btn => {
prompts.txt CHANGED
@@ -1,3 +1,4 @@
1
  need simple notepad with autosync option for multiple users, (something like groceries list for me and my wife)
2
  also to have dark mode
3
- make it compatibile for andoid devices
 
 
1
  need simple notepad with autosync option for multiple users, (something like groceries list for me and my wife)
2
  also to have dark mode
3
+ make it compatibile for andoid devices
4
+ sync should work automaticaly, when note is added to be remembered on notes (online)