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) {
|
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')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-26 01:30:12 +00:00
|
|
|
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-26 01:30:12 +00:00
|
|
|
}
|
2020-03-18 02:00:22 +00:00
|
|
|
|
2020-03-27 21:24:01 +00:00
|
|
|
firebase.auth().onAuthStateChanged(function(user) {
|
|
|
|
list(user)
|
|
|
|
})
|
|
|
|
|