Skip to content

Commit 76141f2

Browse files
committed
chat reload
1 parent a6c90d6 commit 76141f2

File tree

2 files changed

+47
-45
lines changed

2 files changed

+47
-45
lines changed

chat/assets/css/main-v1.0.0.1.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ message-area
266266
overflow: hidden;
267267
height: 100%;
268268
border-left: 1px solid #ccc;
269+
padding: 0px 15px;
269270
}
270271

271272
.chatbox .modal-dialog,

chat/main.js

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ const conversationBio = $('.conversation-bio');
66
const messageTextInput = $('.message-text-input');
77
const sendTextMessageBtn = $('.send-text-message');
88
const msgBody = $('.msg-body>ul');
9-
9+
var conversationRef;
1010

1111
const preloader = document.querySelector('#preloader');
1212
if (preloader) {
13-
window.addEventListener('load', () => {
14-
preloader.remove();
15-
});
13+
window.addEventListener('load', () => {
14+
preloader.remove();
15+
});
1616
}
1717

1818
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.13.0/firebase-app.js";
1919
import { getAuth, signInWithPopup, GoogleAuthProvider } from "https://www.gstatic.com/firebasejs/10.13.0/firebase-auth.js";
20-
import { getDatabase, ref, set, get, child, push } from "https://www.gstatic.com/firebasejs/10.13.0/firebase-database.js";
20+
import { getDatabase, ref, set, get, child, push, onValue } from "https://www.gstatic.com/firebasejs/10.13.0/firebase-database.js";
2121

2222
const firebaseConfig = {
2323
apiKey: "AIzaSyAOgC8M2o5Wz8yl3f1wGQ8p_rUWXujckpw",
@@ -116,7 +116,7 @@ function open_chat() {
116116
}
117117
open_chat();
118118

119-
$('body').on('click','.open-conversation',async function(e){
119+
$('body').on('click', '.open-conversation', async function (e) {
120120
e.preventDefault();
121121
let _this = $(this);
122122
let uid = $(this).data('uid');
@@ -129,7 +129,6 @@ $('body').on('click','.open-conversation',async function(e){
129129
const conversationRefPath1 = 'conversations/' + sender + '-' + uid;
130130
const conversationRefPath2 = 'conversations/' + uid + '-' + sender;
131131

132-
let conversationRef;
133132
let conversationSnapshot;
134133
try {
135134
// Check if the first conversation path exists
@@ -145,49 +144,47 @@ $('body').on('click','.open-conversation',async function(e){
145144
alert('Something went wrong. Please try again later.');
146145
}
147146
conversationSnapshot = conversationSnapshot.val();
148-
$.each(conversationSnapshot, (index, val) => {
149-
console.log({ index, val })
150-
let className = (val.sender === sender) ? 'sender' : 'repaly';
151-
let message = val.message;
152-
msgBody.append(`<li class="${className}">
153-
<p>${message}</p>
154-
<span class="time">10:35 am</span>
155-
</li>`);
147+
// await loadMessages(sender, conversationSnapshot);
148+
// msgBody[0].scrollIntoView({ behavior: 'fast', block: 'end' });
149+
150+
151+
onValue(conversationRef, async (snapshot) => {
152+
const conversationSnapshot = snapshot.val();
153+
await loadMessages(sender, conversationSnapshot);
154+
msgBody[0].scrollIntoView({ behavior: 'instant', block: 'end' });
155+
//scroll ul to Bottom
156+
// msgBody.scrollTo(msgBody[0].scrollHeight);
156157
})
157-
// conversationSnapshot.each((val,index) => {
158-
// console.log({index,val})
159-
// })
160158
})
161159

162160
sendTextMessageBtn.on('click', async function (e) {
163161
e.preventDefault();
164-
162+
165163
const uid = messageTextInput.data('uid');
166164
const sender = JSON.parse(window.localStorage.getItem('user')).uid;
167165
const message = messageTextInput.val();
168166

169167
// Define potential conversation references
170-
const conversationRefPath1 = 'conversations/' + sender + '-' + uid;
171-
const conversationRefPath2 = 'conversations/' + uid + '-' + sender;
172-
173-
let conversationRef;
168+
// const conversationRefPath1 = 'conversations/' + sender + '-' + uid;
169+
// const conversationRefPath2 = 'conversations/' + uid + '-' + sender;
170+
171+
// let conversationRef;
174172
let conversationRefPath;
175173
try {
176174
// Check if the first conversation path exists
177-
conversationRef = ref(database, conversationRefPath1);
178-
let conversationSnapshot = await get(conversationRef);
179-
conversationRefPath = conversationRefPath1;
180-
// If it does not exist, check the second path
181-
if (!conversationSnapshot.exists()) {
182-
conversationRef = ref(database, conversationRefPath2);
183-
conversationRefPath = conversationRefPath2;
184-
}
175+
// conversationRef = ref(database, conversationRefPath1);
176+
// let conversationSnapshot = await get(conversationRef);
177+
// conversationRefPath = conversationRefPath1;
178+
// // If it does not exist, check the second path
179+
// if (!conversationSnapshot.exists()) {
180+
// conversationRef = ref(database, conversationRefPath2);
181+
// conversationRefPath = conversationRefPath2;
182+
// }
185183

186184
var conversationRefKey = push(child(conversationRef, 'conversations')).key;
187-
conversationRef = ref(database, conversationRefPath+ '/' + conversationRefKey);
188-
185+
// conversationRef = ref(database, conversationRefPath + '/' + conversationRefKey);
189186

190-
set(conversationRef, {
187+
set(child(conversationRef, '/' + conversationRefKey) , {
191188
sender: uid,
192189
message: message,
193190
timestamp: Date.now()
@@ -229,7 +226,7 @@ sendTextMessageBtn.on('click', async function (e) {
229226
// var conversationRefKey = push(child(conversationRef, 'conversations')).key;
230227
// conversationRef = ref(database, 'conversations/' + sender + '-' + uid + '/' + conversationRefKey);
231228
// }
232-
229+
233230

234231
// set(conversationRef, {
235232
// sender: uid,
@@ -264,11 +261,11 @@ sendTextMessageBtn.on('click', async function (e) {
264261
// // if (conversationSnapshot.exists()) {
265262
// // console.log(conversationSnapshot.val());
266263
// // }
267-
264+
268265
// })
269266

270267

271-
$('body').on('click','#sign-out',function(e){
268+
$('body').on('click', '#sign-out', function (e) {
272269
e.preventDefault();
273270
window.localStorage.removeItem('user');
274271
window.location.reload();
@@ -287,14 +284,18 @@ async function getUsersList() {
287284
}
288285
}
289286

290-
async function loadConversations(uid) {
291-
const conversationRef = ref(database, 'conversations');
292-
const conversationSnapshot = await get(conversationRef);
293-
if (conversationSnapshot.exists()) {
294-
return conversationSnapshot.val();
295-
}
296-
return false;
297-
}
298287

299288

289+
async function loadMessages(sender, conversationSnapshot) {
290+
$.each(conversationSnapshot, (index, val) => {
291+
console.log({ index, val })
292+
let className = (val.sender === sender) ? 'sender' : 'repaly';
293+
let message = val.message;
294+
msgBody.append(`<li class="${className}">
295+
<p>${message}</p>
296+
<span class="time">10:35 am</span>
297+
</li>`);
298+
})
299+
}
300+
300301

0 commit comments

Comments
 (0)