github.io/it1/bibliotek/bibliotek.js

45 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-03-18 02:00:22 +00:00
2020-03-27 14:48:13 +00:00
var firestore = firebase.firestore
var list = function() {
// 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) {
html = ''
query.forEach(function(doc) {
book = doc.data()
html += book.toHtml()
})
$('#books').html(html)
})
.catch(function(error) {
$('#books').html('<h3>Kunne ikke hente bøker!</h3><p>'+error+'</p>')
$('#books').css('color', 'red')
})
}
var add = function() {
2020-03-26 15:10:55 +00:00
let millis = Date.parse($('#published')[0].value)
2020-03-27 14:48:13 +00:00
db.collection("bibliotek").add({ // withConverter ?
tittel: $('#title')[0].value,
forfatter: $('#author')[0].value,
forlag: $('#publisher')[0].value,
terningkast: $('#rating')[0].value,
2020-03-27 14:48:13 +00:00
utgitt: new firestore.Timestamp(millis/1000, 0)
}).then(function() {
2020-03-27 14:48:13 +00:00
$('#status').html('<p>La til bok '+$('#title')[0].value+'!</p>')
$('#status').css('color', 'green')
}).catch(function() {
2020-03-27 14:48:13 +00:00
$('#status').html('<p>Problem med å legge til bok '+$('#title')[0].value+'!</p>')
$('#status').css('color', 'red')
})
}
2020-03-18 02:00:22 +00:00
2020-03-27 14:48:13 +00:00
$(document).ready(function() {
list()
})