github.io/it1/bibliotek/bibliotek.js

61 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-03-18 02:00:22 +00:00
2020-03-27 21:24:01 +00:00
var db = firebase.firestore()
2020-03-27 14:48:13 +00:00
2020-03-27 21:24:01 +00:00
var list = function(user) {
2020-03-27 14:48:13 +00:00
// db.collection('bibliotek').doc("8ZBX5gmIaRwQBCjgLLg0").get().then(function(doc){console.log(doc.data())})
// db.collection('bibliotek').get().then(function(query){query.forEach(function(doc){console.log(doc.data())})})
db.collection('bibliotek')
.withConverter(bookConverter)
.get()
.then(function(query) {
2020-03-27 21:24:01 +00:00
html = '<br/>'
2020-03-27 14:48:13 +00:00
query.forEach(function(doc) {
book = doc.data()
2020-03-27 21:24:01 +00:00
html += book.toHtml(user)
2020-03-27 14:48:13 +00:00
})
$('#books').html(html)
2020-03-27 21:24:01 +00:00
}).catch(function(error) {
$('#books').html('<h3>Kunne ikke hente bøker!</h3><br/><p>'+error+'</p>')
2020-03-27 14:48:13 +00:00
$('#books').css('color', 'red')
})
}
var add = function() {
2020-03-27 21:24:01 +00:00
db.collection("bibliotek")
.withConverter(bookConverter)
.add(new Book(
$('#title')[0].value,
$('#author')[0].value,
$('#publisher')[0].value,
$('#rating')[0].value,
$('#published')[0].value
)).then(function() {
$('#status').html('<p>La til bok '+$('#title')[0].value+'!</p>')
$('#status').css('color', 'green')
list(firebase.auth().currentUser)
}).catch(function() {
$('#status').html('<p>Problem med å legge til bok '+$('#title')[0].value+'!</p>')
$('#status').css('color', 'red')
})
}
var remove = function(id) {
db.collection("bibliotek").doc(id)
.delete().then(function() {
list(firebase.auth().currentUser)
}).catch(function(error) {
alert("Error removing document: ", error)
})
}
2020-03-18 02:00:22 +00:00
2020-03-27 21:24:01 +00:00
firebase.auth().onAuthStateChanged(function(user) {
list(user)
})
/*
2020-03-27 14:48:13 +00:00
$(document).ready(function() {
2020-03-27 21:24:01 +00:00
list(firebase.auth().currentUser)
2020-03-27 14:48:13 +00:00
})
2020-03-27 21:24:01 +00:00
*/
2020-03-27 14:48:13 +00:00