1
1
const socket = io ( )
2
2
3
+ // Elements
4
+ const $messageForm = document . querySelector ( '#message-form' )
5
+ const $messageFormInput = $messageForm . querySelector ( 'input' )
6
+ const $messageFormButton = $messageForm . querySelector ( 'button' )
7
+ const $sendLocationButton = document . querySelector ( '#send-location' )
8
+
3
9
socket . on ( 'message' , ( message ) => {
4
10
console . log ( message )
5
11
} )
6
12
7
- document . querySelector ( '#message-form' ) . addEventListener ( 'submit' , ( e ) => {
13
+ $messageForm . addEventListener ( 'submit' , ( e ) => {
8
14
e . preventDefault ( )
9
15
16
+ $messageFormButton . setAttribute ( 'disabled' , 'disabled' )
17
+
10
18
const message = e . target . elements . message . value
11
19
12
20
socket . emit ( 'sendMessage' , message , ( error ) => {
21
+ $messageFormButton . removeAttribute ( 'disabled' )
22
+ $messageFormInput . value = ''
23
+ $messageFormInput . focus ( )
24
+
13
25
if ( error ) {
14
26
return console . log ( error )
15
27
}
@@ -18,16 +30,19 @@ document.querySelector('#message-form').addEventListener('submit', (e) => {
18
30
} )
19
31
} )
20
32
21
- document . querySelector ( '#send-location' ) . addEventListener ( 'click' , ( ) => {
33
+ $sendLocationButton . addEventListener ( 'click' , ( ) => {
22
34
if ( ! navigator . geolocation ) {
23
35
return alert ( 'Geolocation is not supported by your browser.' )
24
36
}
25
37
38
+ $sendLocationButton . setAttribute ( 'disabled' , 'disabled' )
39
+
26
40
navigator . geolocation . getCurrentPosition ( ( position ) => {
27
41
socket . emit ( 'sendLocation' , {
28
42
latitude : position . coords . latitude ,
29
43
longitude : position . coords . longitude
30
44
} , ( ) => {
45
+ $sendLocationButton . removeAttribute ( 'disabled' )
31
46
console . log ( 'Location shared!' )
32
47
} )
33
48
} )
0 commit comments