Newer
Older
let converse = function ($http) {
let apiUrl = 'http://localhost:8081'
// Initialize Firebase
let config = {
apiKey: "AIzaSyCUq3YUcVzUkUpIyXmpGd_L43JX_xKyq64",
authDomain: "tugasbesar3-2017.firebaseapp.com",
databaseURL: "https://tugasbesar3-2017.firebaseio.com",
projectId: "tugasbesar3-2017",
storageBucket: "tugasbesar3-2017.appspot.com",
messagingSenderId: "948017499394"
}
class Account {
constructor (id) {
this.id = id
this.onchat = (msg) => {
console.log(msg)
}
}
async initAsync () {
firebase.initializeApp(config)
let messaging = firebase.messaging()
messaging.onMessage(msg => {
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
console.log(msg)
this.onchat(msg.data.message)
})
try {
await messaging.requestPermission()
} catch (e) {
console.log(e)
alert('Please grant notification permission.')
}
try {
let data = JSON.stringify({
'token': await messaging.getToken()
})
console.log(data)
await $http.post(`${apiUrl}/chat/${this.id}/token`, data)
} catch (e) {
console.log(e)
alert('Unable to initialize chat service.')
}
}
chatAsync (message) {
let data = JSON.stringify({
'message': message
})
return $http.post(`${apiUrl}/chat/${this.id}`, data)
}
}
class Driver extends Account {
async waitAsync (locations, timeout) {
let response = await $http.post(`${apiUrl}/driver/${this.id}`, null, {
timeout: timeout
})
return response.data.driver_id
}
}
class User extends Account {
async listAsync (location) {
let response = await $http.get(`${apiUrl}/driver/find?location=${location}`)
return response.data.driver_ids
}
pickAsync (id) {
let data = JSON.stringify({
'user_id': id
})
return $http.post(`${apiUrl}/driver/${this.id}/pick`, data)
}
}
return {
Driver: Driver,
User: User
}
}