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 => {
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()
})
let response = await $http.post(`${apiUrl}/chat/${this.id}/init`, 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)
let response = await $http.post(`${apiUrl}/driver/${this.id}/wait`, '', {
async checkAsync (id) {
try {
await $http.get(`${apiUrl}/driver/${id}`)
return true
} catch (e) {
return false
}
}
return $http.post(`${apiUrl}/driver/${id}/pick`, data)