github.io/static/old/it1/bibliotek/bibliotek.js
Sivert V. Sæther d64efb5fb4 Hugo go go go
Made a hugo setup from scratch to generate a static blog and portfolio webpage.
The old stuff may still be found in static/old.
2022-03-29 23:00:36 +00:00

52 lines
1.4 KiB
JavaScript

var db = firebase.firestore()
var list = function(user) {
db.collection("bibliotek")
.withConverter(bookConverter)
.get()
.then(function(query) {
$('#books').html('<br/>')
query.forEach(function(doc) {
book = doc.data()
book.toHtml($('#books'))
})
}).catch(function(error) {
$('#books').html('<h3>Kunne ikke hente bøker!</h3><br/><p>'+error+'</p>')
$('#books').css('color', 'red')
})
}
var add = function() {
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()
}).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()
}).catch(function(error) {
alert("Error removing document: ", error)
})
}
firebase.auth().onAuthStateChanged(function(user) {
list(user)
})