fixed previous messages not loading
This commit is contained in:
@@ -42,7 +42,10 @@ const MessageForm = ({ contact }: MessageFormProps) => {
|
||||
if (!data.message) {
|
||||
return;
|
||||
}
|
||||
// Assuming sendMessage is imported
|
||||
|
||||
// for (let i = 0; i <= 200; i++) {
|
||||
// sendMessage(i, contact);
|
||||
// }
|
||||
sendMessage(data.message.trim(), contact);
|
||||
reset({ message: '' });
|
||||
};
|
||||
|
||||
@@ -66,7 +66,10 @@ function MessagesArea({
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!socket) return;
|
||||
if (!socket) {
|
||||
console.log('Socket not initialized');
|
||||
return;
|
||||
}
|
||||
|
||||
const currentContainer = containerRef.current;
|
||||
if (currentContainer) {
|
||||
|
||||
@@ -3,7 +3,6 @@ import Socket = SocketIOClient.Socket;
|
||||
let socket: Socket | null = null;
|
||||
|
||||
function initializeSocket(token: string): Socket | null {
|
||||
// Only initialize if we don't already have a socket
|
||||
if (!socket && token) {
|
||||
socket = io({
|
||||
auth: {
|
||||
@@ -20,7 +19,7 @@ function initializeSocket(token: string): Socket | null {
|
||||
return socket;
|
||||
}
|
||||
|
||||
function sendMessage(message: string, recipient: string | null) {
|
||||
function sendMessage(message: string, recipient: string) {
|
||||
if (!socket) {
|
||||
console.error('Socket not initialized');
|
||||
return;
|
||||
@@ -37,46 +36,4 @@ function sendMessage(message: string, recipient: string | null) {
|
||||
});
|
||||
}
|
||||
|
||||
type Contact = {
|
||||
contact: string;
|
||||
read: boolean;
|
||||
};
|
||||
|
||||
function sendContact(data: Contact) {
|
||||
if (!socket) {
|
||||
console.error('Socket not initialized');
|
||||
return;
|
||||
}
|
||||
|
||||
const { contact, read } = data;
|
||||
socket.emit('add contact', { contact: contact, read: read });
|
||||
console.log('Sent contact: ', contact, 'status: ', read);
|
||||
}
|
||||
|
||||
function sendRequestHistoricalMessages(recipient: string) {
|
||||
if (!socket) {
|
||||
console.error('Socket not initialized');
|
||||
return;
|
||||
}
|
||||
|
||||
socket.emit('historical messages', { recipient: recipient });
|
||||
console.log('Requested historical messages for: ', recipient);
|
||||
}
|
||||
|
||||
function sendRequestContactsList() {
|
||||
if (!socket) {
|
||||
console.error('Socket not initialized');
|
||||
return;
|
||||
}
|
||||
|
||||
socket.emit('get contacts list');
|
||||
console.log('Requested contact list');
|
||||
}
|
||||
export {
|
||||
initializeSocket,
|
||||
sendMessage,
|
||||
sendContact,
|
||||
sendRequestHistoricalMessages,
|
||||
sendRequestContactsList,
|
||||
socket,
|
||||
};
|
||||
export { initializeSocket, sendMessage, socket };
|
||||
|
||||
@@ -195,7 +195,7 @@ app.get("/api/chat/messages/:contact", authorizeUser, async (req, res) => {
|
||||
limit,
|
||||
cursor,
|
||||
);
|
||||
if (messages && messages.length < limit) {
|
||||
if (messages && messages.length === 0) {
|
||||
return res.status(404).json({ message: "No more messages found" });
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
const { Server } = require("socket.io");
|
||||
const {
|
||||
insertMessage,
|
||||
getMessages,
|
||||
insertContact,
|
||||
getContacts,
|
||||
} = require("../db/db");
|
||||
const { insertMessage } = require("../db/db");
|
||||
const filter = require("../utils/filter");
|
||||
const { verifyJwtToken } = require("../auth/jwt");
|
||||
const console = require("node:console");
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
function filter(text) {
|
||||
if (text) {
|
||||
if (text.length < 4 || text.length > 20) {
|
||||
return null;
|
||||
}
|
||||
return text.replace(/[^a-zA-Z0-9]/g, "");
|
||||
if (typeof text !== "string") {
|
||||
return null;
|
||||
}
|
||||
if (text.length < 4 || text.length > 20) {
|
||||
return null;
|
||||
}
|
||||
return text.replace(/[^a-zA-Z0-9]/g, "");
|
||||
}
|
||||
|
||||
module.exports = filter;
|
||||
|
||||
Reference in New Issue
Block a user