This repository has been archived by the owner on Sep 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.html
58 lines (52 loc) · 2.14 KB
/
blog.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<html>
<head>
<title>Stitch Blog Example</title>
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.3.1/stitch.js"></script>
</head>
<body onload="displayCommentsOnLoad()">
<h3>This is a great blog post</h3>
<div id="content">
I like to write about technology because I want to get on the
front page of hacker news.
</div>
<hr>
<div id="comments"></div>
Add comment:
<input id="new_comment"><input type="submit" onClick="addComment()">
<script src="https://s3.amazonaws.com/stitch-sdks/js/bundles/4.3.1/stitch.js"></script>
<script>
// Initialize the App Client
const client = stitch.Stitch.initializeDefaultAppClient("nycblog2-uqedx");
// Get a MongoDB Service Client
const mongodb = client.getServiceClient(
stitch.RemoteMongoClient.factory,
"mongodb-atlas"
);
// Get a reference to the blog database
const db = mongodb.db("nycblog");
function addComment() {
const newComment = document.getElementById("new_comment");
console.log("add comment", client.auth.user.id)
db.collection("comments")
.insertOne({ owner_id : client.auth.user.id, comment: newComment.value })
.then(displayComments);
newComment.value = "";
}
function displayCommentsOnLoad() {
client.auth
.loginWithCredential(new stitch.AnonymousCredential())
.then(displayComments)
.catch(console.error);
}
function displayComments() {
db.collection("comments")
.find({}, {limit: 1000})
.toArray()
.then(docs => {
const html = docs.map(doc => `<div>${doc.comment}</div>`);
document.getElementById("comments").innerHTML = html;
});
}
</script>
</body>
</html>