-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (102 loc) · 3.98 KB
/
index.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//Requirements
require('dotenv').config();
const { link } = require('@blockmason/link-sdk');
const fetch = require('node-fetch');
const project = link({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET
}, {
fetch
});
//When Document Ready
document.addEventListener("DOMContentLoaded", async function (event) {
feed = document.getElementById('feed');
textArea = document.getElementById("textarea");
profileImage = document.getElementById("profile-image");
profileUsername = document.getElementById("user-name");
profilePosts = document.getElementById("number-of-posts");
document.getElementById("submitMessage").onclick = function () { submitText() };
// For this demo we set the user to user 0
currentUser = 0;
messageArray = [];
currentUserDisplayName = '';
// Get All Messages
async function getMessages() {
let allMessages = await project.get('/events/Message');
return allMessages;
}
// Format Messages into message array and Print
async function formatMessages() {
messagesJSON = await getMessages();
messagesJSON.data.forEach(message => {
messageArray.push(message);
})
printMessages();
}
// Post a new message
async function postMessage(newMessage) {
messageObject = { message: newMessage }
messageArray.push(messageObject);
removeMessages();
printMessages();
await project.post('/postMessage', { message: newMessage });
}
// Set Profile
async function setProfile(idOfProfile) {
// Set some profile settings for the demo
let profilePost = {
"id": idOfProfile,
"displayName": "Mason Link",
"avatarUrl": 'https://blockmason.link/wp-content/uploads/2019/04/download.jpg'
}
await project.post('/setProfile', profilePost);
}
// Get the profile data based on ID
async function getProfile(userID) {
let profileData = await project.get('/getProfile', {
"id": userID
});
return profileData;
}
// Print profile data to profile
async function printProfile() {
let profileData = await getProfile(currentUser);
currentUserDisplayName = profileData.displayName;
let profileDisplayName = document.createTextNode(currentUserDisplayName);
profileImage.style.cssText = "background-image: url(" + profileData.avatarUrl + ")";
profileUsername.appendChild(profileDisplayName);
}
// Format for message element
function printMessages() {
//Update the number of posts
profilePosts.innerText = ("Number of Posts: " + messageArray.length);
messageArray.forEach(async message => {
let messageUser = document.createTextNode(currentUserDisplayName);
let messageText = document.createTextNode('"' + message.message + '" — ');
let divElement = document.createElement("DIV");
let pElement = document.createElement("P");
let messagesFormated = divElement.appendChild(pElement);
messagesFormated.appendChild(messageText);
messagesFormated.appendChild(messageUser);
feed.appendChild(messagesFormated)
});
}
// Clear existing messages
function removeMessages() {
console.log('Cleared All Messages')
while (feed.firstChild) {
feed.removeChild(feed.firstChild);
}
}
// The function to submit text from textArea
function submitText() {
if (textArea.value.trim() != "") {
messageObject = textArea.value.trim();
postMessage(messageObject);
textArea.value = "";
}
}
//Initialization
await printProfile(currentUser);
formatMessages();
});