github.io/static/old/it1/bibliotek/bibliotek.js

52 lines
1.4 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 22:29:16 +00:00
db.collection("bibliotek")
2020-03-27 14:48:13 +00:00
.withConverter(bookConverter)
.get()
.then(function(query) {
$('#books').html('<br/>')
2020-03-27 14:48:13 +00:00
query.forEach(function(doc) {
book = doc.data()
book.toHtml($('#books'))
2020-03-27 14:48:13 +00:00
})
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()
2020-03-27 21:24:01 +00:00
}).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()
2020-03-27 21:24:01 +00:00
}).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)
})