-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchatUserViewTes.html
More file actions
218 lines (198 loc) · 6.92 KB
/
Copy pathchatUserViewTes.html
File metadata and controls
218 lines (198 loc) · 6.92 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<!DOCTYPE html>
<html>
<head>
<title>Chat History & User Chat</title>
<style>
body {
font-family: Arial, sans-serif;
}
#chatBox {
width: 400px;
height: 500px;
overflow-y: scroll;
border: 1px solid #ccc;
padding: 10px;
display: flex;
flex-direction: column;
}
.message {
padding: 5px;
margin-bottom: 10px;
border-radius: 5px;
max-width: 70%;
}
.ownMessage {
background-color: #dcf8c6;
align-self: flex-end;
}
.adminMessage {
background-color: #f1f1f1;
}
.message p {
margin: 0;
}
.message .sender {
font-weight: bold;
margin-right: 5px;
}
.message .timestamp {
font-size: 0.8em;
color: #888;
}
.readStatus {
cursor: pointer;
font-size: 0.8em;
color: #888;
}
</style>
</head>
<body>
<h1>Chat History & User Chat</h1>
<div id="chatBox"></div>
<input type="text" id="messageInput" placeholder="Type your message" />
<input type="number" id="senderId" placeholder="id" />
<input type="file" id="imageInput" accept="image/*" />
<button onclick="sendMessage()">Send</button>
<script src="https://cdn.socket.io/4.3.2/socket.io.min.js"></script>
<script>
const socket = io("http://103.171.85.30:4000");
socket.on("connect", function () {
console.log("Terhubung ke server socket.io");
});
socket.on("connect_error", function (error) {
console.error("Gagal terhubung ke server socket.io:", error);
});
let previousScrollHeight = 0;
const chatBox = document.getElementById("chatBox");
function markAsRead(messageId) {
socket.emit("messageRead", { messageId });
}
function fetchChatHistory(id) {
fetch(`https://keydentalcare.isepwebtim.my.id/chat/riwayat/${id}`)
.then((response) => response.json())
.then((chatData) => {
chatData.forEach((chat) => {
const messageDiv = document.createElement("div");
messageDiv.classList.add("message");
if (chat.senderId === 13) {
messageDiv.classList.add("adminMessage");
} else {
messageDiv.classList.add("ownMessage");
}
let messageContent = "";
console.log(chat.lampiran_gambar);
if (chat.lampiran_gambar) {
const imagePath = `https://keydentalcare.isepwebtim.my.id/img/${chat.lampiran_gambar}`;
messageContent += `<img src="${imagePath}" alt="Attachment" style="max-width: 100%;"/><p class="timestamp">${new Date(
chat.timestamp
).toLocaleString()}</p>
<p class="readStatus" data-messageId="${
chat.messageId
}" onclick="markAsRead(${chat.messageId})">${
chat.status === "terkirim" ? "✓✓" : "✓"
}</p>`;
} else if (chat.message) {
messageContent += `
<p><span class="sender">${chat.senderName}</span>: ${
chat.message
}</p>
<p class="timestamp">${new Date(
chat.timestamp
).toLocaleString()}</p>
<p class="readStatus" data-messageId="${
chat.messageId
}" onclick="markAsRead(${chat.messageId})">${
chat.status === "terkirim" ? "✓✓" : "✓"
}</p>
`;
}
messageDiv.innerHTML = messageContent;
chatBox.appendChild(messageDiv);
});
setTimeout(() => {
chatBox.scrollTop = chatBox.scrollHeight;
}, 100);
})
.catch((error) => {
console.error("Error fetching chat data:", error);
})
.finally(() => {
const diff = chatBox.scrollHeight - previousScrollHeight;
chatBox.scrollTop = chatBox.scrollHeight - diff;
});
}
window.addEventListener("load", () => {
fetchChatHistory(1);
setTimeout(() => {
chatBox.scrollTop = chatBox.scrollHeight;
}, 100);
chatBox.scrollTop = chatBox.scrollHeight;
});
function sendMessage() {
console.log("Trying to send a message...");
const message = document.getElementById("messageInput").value;
const senderId = document.getElementById("senderId").value;
const receiverId = 13;
const fileInput = document.getElementById("imageInput");
const file = fileInput.files[0];
const formData = new FormData();
formData.append("image", file);
fetch(`https://keydentalcare.isepwebtim.my.id/upload`, {
method: "POST",
body: formData,
})
.then((response) => response.json())
.then((data) => {
const { imagePath } = data;
socket.emit("sendMessage", {
senderId,
receiverId,
message,
imagePath,
});
setTimeout(() => {
chatBox.scrollTop = chatBox.scrollHeight;
}, 100);
})
.catch((error) => {
console.error("Error sending message:", error);
});
console.log("Trying to 2 send a message...");
}
socket.on("receiveMessage", (data) => {
const messageDiv = document.createElement("div");
messageDiv.classList.add("message");
if (data.senderId === 13) {
messageDiv.classList.add("adminMessage");
} else {
messageDiv.classList.add("ownMessage");
}
let messageContent = `
<p><span class="sender">${data.senderName}</span>: ${data.message}</p>
<p class="timestamp">${new Date(data.timestamp).toLocaleString()}</p>
<p class="readStatus" onclick="markAsRead(${data.messageId})">${
data.status === "terkirim" ? "✓✓" : "✓"
}</p>
`;
if (data.lampiran_gambar) {
const imagePath = `https://keydentalcare.isepwebtim.my.id/img/${data.lampiran_gambar}`;
messageContent += `<img src="${imagePath}" alt="Attachment" style="max-width: 100%;"/>`;
}
setTimeout(() => {
chatBox.scrollTop = chatBox.scrollHeight;
}, 100);
messageDiv.innerHTML = messageContent;
chatBox.appendChild(messageDiv);
});
socket.on("messageReadConfirmation", ({ messageId }) => {
const readStatusElements = document.querySelectorAll(".readStatus");
readStatusElements.forEach((element) => {
const id = element.getAttribute("data-messageId");
if (id === messageId.toString()) {
element.textContent = "✓✓";
}
});
});
</script>
</body>
</html>