11import './style.css'
2+ import Dexie from 'dexie' ;
23
3- const form = document . querySelector ( 'form' )
4- const posts = document . querySelector ( 'ol' )
54
6- form ?. addEventListener ( 'submit' , e => {
5+ const db = new Dexie ( 'Cycleoops' ) ;
6+
7+ // Declare tables, IDs and indexes
8+ db . version ( 1 ) . stores ( {
9+ notes : '++id, time, text, lat, lon'
10+ } ) ;
11+
12+ const form = document . querySelector ( 'form' ) !
13+ const posts = document . querySelector ( 'ol' ) !
14+
15+
16+ 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+
26+ for ( const note of notes ) {
27+
28+ const li = document . createElement ( 'li' )
29+
30+ li . innerText = JSON . stringify ( note ) ;
31+
32+ posts . appendChild ( li )
33+
34+
35+ }
36+
37+ }
38+
39+ update ( )
40+
41+ form ?. addEventListener ( 'submit' , async e => {
742 e . preventDefault ( )
843
944 const data = new FormData ( e . target )
10- console . log ( "d" , data . get ( 'message' ) )
45+ const text = data . get ( "message" )
46+ const time = Date . now ( )
47+
48+
49+ const [ lat , lon ] = await new Promise ( ( resolve , reject ) => {
50+
51+ navigator . geolocation . getCurrentPosition ( ( position ) => {
52+ console . log ( position . coords ) ;
53+ resolve ( [ position . coords . latitude , position . coords . longitude ] )
54+
55+
56+ } , ( ) => {
57+
58+ resolve ( [ 0 , 0 ] )
59+
60+ } ) ;
61+
62+
63+ } )
64+
65+
66+ const noteId = await db . notes . add ( {
67+ time,
68+ text,
69+ lat,
70+ lon
71+ } )
72+
73+ console . log ( noteId )
74+
75+
76+ update ( )
77+
1178
1279} )
0 commit comments