From 34d57e848c3e3a87dd20d8d4cfb1cb5b19461507 Mon Sep 17 00:00:00 2001 From: sava Date: Fri, 27 Mar 2020 15:48:13 +0100 Subject: [PATCH] enkelt firestore bibliotek --- it1/bibliotek/bibliotek.css | 9 ++++----- it1/bibliotek/bibliotek.js | 36 ++++++++++++++++++++++++++++++++---- it1/bibliotek/bok.js | 37 +++++++++++++++++++++++++++++++++++++ it1/bibliotek/index.html | 10 +++++++--- 4 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 it1/bibliotek/bok.js diff --git a/it1/bibliotek/bibliotek.css b/it1/bibliotek/bibliotek.css index c6abda1..7f9763f 100644 --- a/it1/bibliotek/bibliotek.css +++ b/it1/bibliotek/bibliotek.css @@ -3,8 +3,11 @@ margin: 2vh; } -#content > * { +.top { margin-top: 9vh; +} + +.middle { margin-left: 13vw; margin-right: 42.0vw; } @@ -13,10 +16,6 @@ margin-top: 1vh; } -#form { - width: 25%; -} - #form > div > * { margin-bottom: 1vh; } diff --git a/it1/bibliotek/bibliotek.js b/it1/bibliotek/bibliotek.js index 8c918f7..593bf21 100644 --- a/it1/bibliotek/bibliotek.js +++ b/it1/bibliotek/bibliotek.js @@ -1,16 +1,44 @@ +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('

Kunne ikke hente bøker!

'+error+'

') + $('#books').css('color', 'red') + }) +} + var add = function() { let millis = Date.parse($('#published')[0].value) - db.collection("bibliotek").add({ + db.collection("bibliotek").add({ // withConverter ? tittel: $('#title')[0].value, forfatter: $('#author')[0].value, forlag: $('#publisher')[0].value, terningkast: $('#rating')[0].value, - utgitt: new firebase.firestore.Timestamp(millis/1000, 0) + utgitt: new firestore.Timestamp(millis/1000, 0) }).then(function() { - $('#status').html('

La til bok '+$('#title')[0].value+'!

') + $('#status').html('

La til bok '+$('#title')[0].value+'!

') + $('#status').css('color', 'green') }).catch(function() { - $('#status').html('

Problem med å legge til bok '+$('#title')[0].value+'!

') + $('#status').html('

Problem med å legge til bok '+$('#title')[0].value+'!

') + $('#status').css('color', 'red') }) } +$(document).ready(function() { + list() +}) + diff --git a/it1/bibliotek/bok.js b/it1/bibliotek/bok.js new file mode 100644 index 0000000..a769e24 --- /dev/null +++ b/it1/bibliotek/bok.js @@ -0,0 +1,37 @@ + +class Book { + constructor(title, author, publisher, rating, published) { + this.title = title + this.author = author + this.publisher = publisher + this.rating = rating + this.published = published.toDate() + } + toString() { + return this.title+' av '+this.author+', ternignkast '+this.rating+', utgitt '+this.published.toString().slice(0,15)+', forlag; '+this.publisher + } + toHtml() { + let span = function(string, color='red') { + return ''+string+'' + } + return '

'+span(this.title)+' av '+span(this.author)+', ternignkast '+span(this.rating)+ + ', utgitt '+span(this.published.toString().slice(0,15), 'green')+', forlag; '+span(this.publisher, 'yellow')+'

' + } +} + +bookConverter = { + toFirestore: function(book) { + return { + title: book.title, + author: book.author, + publisher: book.publisher, + rating: book.rating, + published: book.published + } + }, + fromFirestore: function(snapshot, options) { + const data = snapshot.data(options) + return new Book(data.tittel, data.forfatter, data.forlag, data.terningkast, data.utgitt) + } +} + diff --git a/it1/bibliotek/index.html b/it1/bibliotek/index.html index cb70be7..94638e0 100644 --- a/it1/bibliotek/index.html +++ b/it1/bibliotek/index.html @@ -9,11 +9,12 @@ + -
-
+
+
@@ -34,7 +35,10 @@ Legg til
-

+

 

+
+ Hent bøker +