added InitializeContact function

This commit is contained in:
slawk0
2024-10-27 20:01:29 +01:00
parent e92f07433d
commit 73dfeb6e3e
5 changed files with 21 additions and 9 deletions

View File

@@ -1,12 +1,16 @@
import { sendRequestContactsList, socket } from '../../socket/socket.tsx';
import { useEffect, useState } from 'react';
type InitializeContactsProps = {
InitializeContact: (contact: string) => void;
};
type ContactsList = {
username: string;
usernamecontact: string;
read: boolean;
};
function ContactsList() {
function ContactsList({ InitializeContact }: InitializeContactsProps) {
const [ContactsList, setContactsList] = useState<ContactsList[]>([]);
useEffect(() => {
if (!socket) {
@@ -14,6 +18,7 @@ function ContactsList() {
return;
}
sendRequestContactsList();
socket.on('get contacts list', (contactsList: ContactsList[]) => {
console.log('Received contacts list: ', contactsList);
setContactsList(contactsList);
@@ -37,7 +42,9 @@ function ContactsList() {
return (
<div className="flex-grow overflow-y-auto w-64">
<ul className="m-2 flex-grow-1 ">{contactsList}</ul>
<ul className="m-2 flex-grow-1 " onClick={InitializeContact}>
{contactsList}
</ul>
</div>
);
}

View File

@@ -21,7 +21,6 @@ function messageForm({ contact }: MessaFormProps) {
if (!data.message) {
return;
}
console.log('sended message: ' + data.message + ' recipient: ', contact);
sendMessage(data.message, contact);
reset({ message: '' });
};

View File

@@ -48,7 +48,7 @@ function Chat() {
{/*Contact input*/}
<ContactForm InitializeContact={InitializeContact} />
{/*Contact list*/}
<ContactsList />
<ContactsList InitializeContact={InitializeContact} />
<hr />
<UserProfile />
</div>

View File

@@ -41,6 +41,12 @@ function sendMessage(message: string, recipient: string | null) {
recipient: recipient,
timestamp: timestamp,
});
console.log('sent message: ', {
message: message,
recipient: recipient,
timestamp: timestamp,
});
}
type Contact = {

View File

@@ -43,10 +43,10 @@ async function createTables() {
try {
await client.query(`
CREATE TABLE IF NOT EXISTS messages (
sender VARCHAR(128),
sender VARCHAR(128) NOT NULL,
recipient VARCHAR(128) NOT NULL,
message VARCHAR(500),
timestamp VARCHAR(100),
message VARCHAR(500) NOT NULL,
timestamp VARCHAR(100) NOT NULL,
message_id SERIAL PRIMARY KEY
);
`);
@@ -56,8 +56,8 @@ async function createTables() {
try {
await client.query(`
CREATE TABLE IF NOT EXISTS contacts (
username VARCHAR(20),
usernameContact VARCHAR(20),
username VARCHAR(20) NOT NULL,
usernameContact VARCHAR(20) NOT NULL,
read BOOLEAN NOT NULL
);
`);