-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
137 lines (113 loc) · 5.8 KB
/
script.js
File metadata and controls
137 lines (113 loc) · 5.8 KB
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
132
133
134
135
136
137
const loggedOutLinks = document.querySelectorAll('.logged-out');
const loggedInLinks = document.querySelectorAll('.logged-in');
const accountDetails = document.querySelector('.account-details');
let handle = ``
let image = ``
let xyzname = ``
let veryvery = ``
const POSTS_PER_PAGE = 5;
let lastVisiblePost = null;
let morePostsAvailable = true;
function debounce(func, wait, immediate) {
let timeout;
return function() {
const context = this, args = arguments;
const later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
const callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
const debouncedGetPosts = debounce(getPosts, 500);
getPosts();
window.addEventListener('scroll', function() {
const scrollDistance = window.scrollY + window.innerHeight;
const totalHeight = document.body.scrollHeight;
if (scrollDistance / totalHeight > 0.8 && morePostsAvailable) {
debouncedGetPosts();
}
});
function getPosts() {
const postsRef = firebase.firestore().collection('status');
let query = postsRef
.orderBy('timestamp', 'desc')
.limit(POSTS_PER_PAGE);
if (lastVisiblePost) {
query = query.startAfter(lastVisiblePost);
}
query.onSnapshot(function(snapshot) {
// Check if there are more posts available to load
if (snapshot.size < POSTS_PER_PAGE) {
morePostsAvailable = false;
}
var posts = snapshot.docs.map(function(post) {
return post.data();
});
posts.forEach(function(post) {
addPostToPage(post);
});
lastVisiblePost = snapshot.docs[snapshot.docs.length - 1];
});
}
function addPostToPage(post) {
var postElement = `
<br>
<div style="border: 3px solid; position: realative; right: 0;" id="${post.id}">
<div style="position: relative; top: 6px; left: 10px; width: 90%;">
<img src="${post.image}" style="position: relative; top: 10px; left: 12px; width: 32px; height: 32px; border-radius: 50%;">
<p onclick="location.href='/u/?u=${post.handle}';" style="width: 10%; cursor: pointer; position: relative; top: -40px; left: 51px;">${post.username}</p>
<p onclick="location.href='/u/?u=${post.handle}';" style="width: 10%; color: gray; font-size: 10px; position: relative; top: -49px; left: 52px; cursor: pointer;">${post.handle}</p>${post.very}
<p style="width: 8%; color: gray; position: relative; top: -90px; left: 78%;">${post.date}</p>
</div>
<div style="position: relative;">
<pre style="width: 86.6%; cursor: default; position: relative; left: 10px; white-space: pre-wrap;">${post.content}</pre>
</div>
<div class="logged-in" style="position: relative; overflow: hidden;" id="fancy-icons">
<div style="float: left; display: block; text-align: center; padding: 14px; text-decoration: none; cursor: pointer;" onclick="location.href='/status/?p=${post.id}';"><img src="https://firebasestorage.googleapis.com/v0/b/shnitters.appspot.com/o/reply.png?alt=media&token=5124acf4-60f7-4f53-bc6e-4803b534352d&format=webp" style="height: 18px; width: 18px;"> Reply</div>
<div id="love-count-${post.id}" style="float: left; display: block; text-align: center; padding: 14px; text-decoration: none; cursor: pointer;" onclick="addLove('${post.id}')"><img src="https://firebasestorage.googleapis.com/v0/b/shnitters.appspot.com/o/like.png?alt=media&token=b212b954-864b-4e5f-9258-3f234863c3d6&format=webp" style="height: 18px; width: 18px;"> ${post.likes} Love</div>
<div style="float: left; display: block; text-align: center; padding: 14px; text-decoration: none; cursor: pointer;" onclick="location.href='/status/?p=${post.id}';"><img src="https://firebasestorage.googleapis.com/v0/b/shnitters.appspot.com/o/comment.png?alt=media&token=aef99c51-74c5-4482-9233-ce7524c3dfbc&format=webp" style="height: 18px; width: 18px;"> Conversation</div>
</div>
</div>
`;
document.getElementById('feed').innerHTML += postElement;
const userObj = firebase.auth().currentUser
if(userObj != null){
if (post.authorId === userObj.uid) {
var editButton = `<button onclick="showEditpostForm(${post.id});">Edit</button>`
postElement.innerHTML += editButton;
}
}
};
const setupUI = (user) => {
if(user){
store.collection('users').doc(user.uid).get().then(doc => {
handle = `${doc.data().handle}`;
image = `${doc.data().pfp}`;
xyzname = `${doc.data().username}`;
veryvery = `${doc.data().very}`;
payloader = `${doc.data().payload}`;
const html = `
<div><img src="${doc.data().pfp}" title="Profile Image" style="height: 48px; width: 48px; border-radius: 50%;"></div>
<div>Logged in as ${doc.data().username}</div>${doc.data().very}
<div><p style="color: gray;">Email: ${user.email}</p></div>
`;
accountDetails.innerHTML = html;
})
loggedInLinks.forEach(item => item.style.display = 'block');
loggedOutLinks.forEach(item => item.style.display = 'none');
} else {
accountDetails.innerHTML = ''
loggedInLinks.forEach(item => item.style.display = 'none');
loggedOutLinks.forEach(item => item.style.display = 'block');
}
}
document.addEventListener('DOMContentLoaded', function() {
var modals = document.querySelectorAll('.modal');
M.Modal.init(modals);
var items = document.querySelectorAll('.collapsible');
M.Collapsible.init(items);
});