|
1 | 1 | import './style.css' |
2 | | -import Dexie from 'dexie'; |
3 | | - |
| 2 | +import Dexie, { type EntityTable } from "dexie"; |
| 3 | + |
| 4 | +interface Note { |
| 5 | + id: number; |
| 6 | + time: number; |
| 7 | + text: string; |
| 8 | + lat: number; |
| 9 | + lon: number; |
| 10 | +} |
4 | 11 |
|
5 | | -const db = new Dexie('Cycleoops'); |
| 12 | +const db = new Dexie("Cycleoops") as Dexie & { |
| 13 | + notes: EntityTable<Note, "id">; |
| 14 | +}; |
6 | 15 |
|
7 | 16 | // Declare tables, IDs and indexes |
8 | 17 | db.version(1).stores({ |
9 | | - notes: '++id, time, text, lat, lon' |
| 18 | + notes: "++id, time, text, lat, lon", |
10 | 19 | }); |
11 | 20 |
|
12 | | -const form = document.querySelector('form')! |
13 | | -const posts = document.querySelector('ol')! |
14 | | - |
| 21 | +const form = document.querySelector("form")!; |
| 22 | +const posts = document.querySelector("ol")!; |
15 | 23 |
|
16 | 24 | async function update() { |
17 | | - const notes = await db.notes |
18 | | - .orderBy("time") |
19 | | - .reverse() |
20 | | - .toArray(); |
21 | | - |
22 | | - console.log(notes) |
23 | | - |
24 | | - posts.innerHTML = '' |
| 25 | + const notes = await db.notes.orderBy("time").reverse().toArray(); |
25 | 26 |
|
26 | | - for(const note of notes) { |
| 27 | + console.log(notes); |
27 | 28 |
|
28 | | - const li = document.createElement('li') |
| 29 | + posts.innerHTML = ""; |
29 | 30 |
|
30 | | - const timestamp = new Date(note.time) |
31 | | - li.innerText = `${note.text} (${timestamp.toLocaleString()})` |
32 | | - |
33 | | - posts.appendChild(li) |
| 31 | + for (const note of notes) { |
| 32 | + const li = document.createElement("li"); |
34 | 33 |
|
| 34 | + const timestamp = new Date(note.time); |
| 35 | + li.innerText = `${note.text} (${timestamp.toLocaleString()})`; |
35 | 36 |
|
| 37 | + posts.appendChild(li); |
36 | 38 | } |
37 | | - |
38 | 39 | } |
39 | 40 |
|
40 | | -update() |
41 | | - |
42 | | -form?.addEventListener('submit', async e => { |
43 | | - e.preventDefault() |
44 | | - |
45 | | - const data = new FormData(form) |
46 | | - const text = data.get("message") |
47 | | - const time = Date.now() |
48 | | - |
| 41 | +update(); |
49 | 42 |
|
50 | | - const [lat, lon] = await new Promise((resolve, reject) => { |
51 | | - |
52 | | - navigator.geolocation.getCurrentPosition((position) => { |
53 | | - console.log(position.coords); |
54 | | - resolve([position.coords.latitude, position.coords.longitude]) |
| 43 | +form?.addEventListener("submit", async (e) => { |
| 44 | + e.preventDefault(); |
55 | 45 |
|
| 46 | + const data = new FormData(form); |
| 47 | + const text = data.get("message") as string; |
| 48 | + const time = Date.now(); |
56 | 49 |
|
57 | | - }, () => { |
58 | | - |
59 | | - resolve([0,0]) |
60 | | - |
61 | | - }); |
62 | | - |
63 | | - |
64 | | - }) |
65 | | - |
| 50 | + const [lat, lon] = await new Promise<[number, number]>((resolve, reject) => { |
| 51 | + navigator.geolocation.getCurrentPosition( |
| 52 | + (position) => { |
| 53 | + console.log(position.coords); |
| 54 | + resolve([position.coords.latitude, position.coords.longitude]); |
| 55 | + }, |
| 56 | + () => { |
| 57 | + resolve([0, 0]); |
| 58 | + } |
| 59 | + ); |
| 60 | + }); |
66 | 61 |
|
67 | 62 | const noteId = await db.notes.add({ |
68 | 63 | time, |
69 | 64 | text, |
70 | 65 | lat, |
71 | | - lon |
72 | | - }) |
73 | | - |
74 | | - console.log(noteId) |
| 66 | + lon, |
| 67 | + }); |
75 | 68 |
|
| 69 | + console.log(noteId); |
76 | 70 |
|
77 | | - update() |
| 71 | + update(); |
78 | 72 |
|
79 | | - form.reset() |
80 | | - |
81 | | -}) |
| 73 | + form.reset(); |
| 74 | +}); |
82 | 75 |
|
83 | 76 |
|
84 | 77 | // resize any text areas to fit content |
|
0 commit comments