gjestebok

This commit is contained in:
sava 2020-03-27 23:29:16 +01:00
parent 891f219379
commit cff16efbf5
8 changed files with 122 additions and 11 deletions

View File

@ -2,9 +2,7 @@
var db = firebase.firestore() var db = firebase.firestore()
var list = function(user) { var list = function(user) {
// db.collection('bibliotek').doc("8ZBX5gmIaRwQBCjgLLg0").get().then(function(doc){console.log(doc.data())}) db.collection("bibliotek")
// db.collection('bibliotek').get().then(function(query){query.forEach(function(doc){console.log(doc.data())})})
db.collection('bibliotek')
.withConverter(bookConverter) .withConverter(bookConverter)
.get() .get()
.then(function(query) { .then(function(query) {
@ -52,9 +50,3 @@ firebase.auth().onAuthStateChanged(function(user) {
list(user) list(user)
}) })
/*
$(document).ready(function() {
list(firebase.auth().currentUser)
})
*/

View File

@ -11,7 +11,7 @@
<script src="https://www.gstatic.com/firebasejs/7.12.0/firebase-firestore.js"></script> <script src="https://www.gstatic.com/firebasejs/7.12.0/firebase-firestore.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script> <script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="/it1/js/init-firebase.js"></script> <script type="text/javascript" src="/it1/js/init-firebase.js"></script>
<script type="text/javascript" src="/it1/bibliotek/bok.js"></script> <script type="text/javascript" src="/it1/js/converters/bok.js"></script>
</head> </head>
<body> <body>
<script type="text/javascript" id="navbar" src="/it1/js/navbar.js"></script> <script type="text/javascript" id="navbar" src="/it1/js/navbar.js"></script>

4
it1/css/gjestebok.css Normal file
View File

@ -0,0 +1,4 @@
#kommentar {
font-size: 1.1rem;
}

View File

@ -0,0 +1,44 @@
var db = firebase.firestore()
var list = function() {
db.collection('gjestebok')
.withConverter(commentConverter)
.get()
.then(function(query) {
html = '<br/>'
query.forEach(function(doc) {
book = doc.data()
html += book.toHtml()
})
$('#kommentarer').html(html)
}).catch(function(error) {
$('#kommentarer').html('<h3>Kunne ikke hente bøker!</h3><br/><p>'+error+'</p>')
$('#kommentarer').css('color', 'red')
})
}
var add = function() {
db.collection("gjestebok")
.withConverter(commentConverter)
.add(new Comment(
$('#navn')[0].value,
$('#melding')[0].value
)).then(function() {
$('#status').html('<p>La til melding med navn '+$('#navn')[0].value+'!</p>')
$('#status').css('color', 'green')
list()
}).catch(function() {
$('#status').html('<p>Problem med å legge til melding med navn '+$('#navn')[0].value+'!</p>')
$('#status').css('color', 'red')
})
}
list()
/*
$(document).ready(function() {
list(firebase.auth().currentUser)
})
*/

38
it1/gjestebok/index.html Normal file
View File

@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<title>Firebase basert gjestebok</title>
<meta charset="utf-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" crossorigin="anonymous">
<link rel="stylesheet" href="/it1/main.css">
<link rel="stylesheet" href="/it1/css/form.css">
<link rel="stylesheet" href="/it1/css/gjestebok.css">
<script src="https://www.gstatic.com/firebasejs/7.12.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.12.0/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.12.0/firebase-firestore.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="/it1/js/init-firebase.js"></script>
<script type="text/javascript" src="/it1/js/converters/kommentar.js"></script>
</head>
<body>
<script type="text/javascript" id="navbar" src="/it1/js/navbar.js"></script>
<div id="content">
<form action="#" method="POST" id="form" class="top">
<div class="form-group">
<div class="row">
<div class="col col-sm-3"><input type="name" class="form-control" id="navn" placeholder="Navn"></div>
<div class="col col-sm-9"><input type="name" class="form-control" id="melding" placeholder="Melding"></div>
</div>
</div>
<a type="submit" class="btn btn-primary" href="javascript: add()">Legg til</a>
</form>
<div id="status">
<p>&nbsp;</p>
</div>
<!-- <a type="submit" class="btn btn-primary" href="javascript: list()">Hent kommentarer</a> -->
<div id="kommentarer">
</div>
</div>
<script type="text/javascript" src="/it1/gjestebok/gjestebok.js"></script>
</body>
</html>

View File

@ -18,6 +18,9 @@
<h3 style="margin: 3vh">Denne siden inneholder ting jeg har laget med firebase i IT1!</h3> <h3 style="margin: 3vh">Denne siden inneholder ting jeg har laget med firebase i IT1!</h3>
<br /> <br />
<ul id="oppgaver"> <ul id="oppgaver">
<li>
<h5><a href="/it1/gjestebok/">Gjestebok</a></h5>
</li>
<li> <li>
<h5><a href="/it1/bibliotek/">Bibliotek</a></h5> <h5><a href="/it1/bibliotek/">Bibliotek</a></h5>
</li> </li>

View File

@ -41,7 +41,7 @@ bookConverter = {
data.terningkast, data.terningkast,
data.utgitt.toDate(), data.utgitt.toDate(),
snapshot.id snapshot.id
) )
} }
} }

View File

@ -0,0 +1,30 @@
class Comment {
constructor(name, message) {
this.name = name,
this.message = message
}
toString() {
return this.name+': '+this.message
}
toHtml() {
return '<p id="kommentar">'+this.name+': '+this.message+'</p>'
}
}
commentConverter = {
toFirestore: function(comment) {
return {
navn: comment.name,
melding: comment.message
}
},
fromFirestore: function(snapshot, options) {
const data = snapshot.data(options)
return new Comment(
data.navn,
data.melding
)
}
}