-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.html
90 lines (65 loc) · 2.25 KB
/
index.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
<html>
<head>
<title>Vanilla Boilerplate</title>
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script src="https://unpkg.com/moralis/dist/moralis.js"></script>
</head>
<body>
<button id="btn-login">Moralis Login</button>
<button id="btn-logout">Logout</button>
<br>
<hr/>
<p>Create new group chat: </p>
<input type="text" placeholder="Group name" id="group-name-text"/>
<input type="button" value="Create" id="btn-new-group-chat"/>
<hr/>
<br>
<p>Join group chat</p>
<div id="chatRooms">
<ul id="roomList"></ul>
</div>
<hr/>
<script>
// connect to Moralis server
Moralis.initialize("APP ID");
Moralis.serverURL = "SERVER URL";
// add from here down
async function login() {
let user = Moralis.User.current();
if (!user) {
user = await Moralis.Web3.authenticate();
}
console.log("logged in user:", user);
}
async function logOut() {
await Moralis.User.logOut();
console.log("logged out");
}
async function newGroupChat(){
let chatTitle = document.getElementById("group-name-text").value;
const Chats = Moralis.Object.extend("Chats");
const chat = new Chats();
chat.set("title", chatTitle);
chat.save();
console.log("created chat with title " + chatTitle);
}
async function getGroupChats(){
const Chats = Moralis.Object.extend("Chats");
const query = new Moralis.Query(Chats);
const results = await query.find();
const roomList = document.getElementById("roomList");
for (let i = 0; i < results.length; i++) {
const object = results[i];
console.log(object.get('title'));
var listItem = document.createElement('li');
listItem.innerHTML = "<a href='chat.html?id="+object.id+"'>"+object.get('title')+"</a>";
roomList.appendChild(listItem);
}
}
getGroupChats();
document.getElementById("btn-login").onclick = login;
document.getElementById("btn-logout").onclick = logOut;
document.getElementById("btn-new-group-chat").onclick = newGroupChat;
</script>
</body>
</html>