-
Notifications
You must be signed in to change notification settings - Fork 0
/
adder.html
117 lines (97 loc) · 3.75 KB
/
adder.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sentence Fusion</title>
<script src="https://unpkg.com/realm-web/dist/bundle.iife.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css">
<style>
.hidden {
display: none !important;
}
body {
width: 50%;
margin: auto;
margin-top: 2rem;
}
</style>
</head>
<body>
<div id="loginSection">
<div class="field">
<label class="label">Email</label>
<div class="control">
<input class="input" type="email" id="email">
</div>
</div>
<div class="field">
<label class="label">Password</label>
<div class="control">
<input class="input" type="password" id="password">
</div>
</div>
<div class="field is-grouped">
<div class="control">
<button class="button is-link" id="loginBtn" onclick="login()">Login</button>
</div>
</div>
</div>
<div id="editor" class="hidden">
<div class="box">
<label class="label">Sentence</label>
<input class="input" type="text" id="sentenceInput">
</div>
<div id="sentences">
</div>
</div>
<script defer>
const loginSection = document.getElementById("loginSection");
const editor = document.getElementById("editor");
const sentences = document.getElementById("sentences");
const sentenceInput = document.getElementById("sentenceInput");
const email = document.getElementById("email");
const password = document.getElementById("password");
// TODO: change the id to your own
const app = new Realm.App({ id: "application" });
async function loginEmailPassword(email, password) {
const credentials = Realm.Credentials.emailPassword(email, password);
const user = await app.logIn(credentials);
console.assert(user.id === app.currentUser.id);
return user;
}
function login() {
editor.classList.remove("hidden");
loginSection.classList.add("hidden");
loginEmailPassword(email.value, password.value).then(async (user) => {
const collection = app.currentUser.mongoClient("mongodb-atlas").db("fusion").collection("sentence");
async function refreshSentences() {
sentences.innerHTML = (await collection.find()).map(object =>
`<p><span class="tag is-primary">${object.userId}</span> ${object.content}</p>`
);
}
sentenceInput.addEventListener("keypress", async function (event) {
if (event.key === "Enter") {
try {
await collection.insertOne(
{
userId: app.currentUser.id,
content: sentenceInput.value
}
);
} catch {
alert("Sentence could not be sent.");
}
}
});
refreshSentences();
setInterval(refreshSentences, 10000);
}).catch(() => {
editor.classList.add("hidden");
loginSection.classList.remove("hidden");
});
}
</script>
</body>
</html>