Skip to content

Commit

Permalink
fixed header styling
Browse files Browse the repository at this point in the history
  • Loading branch information
lizzie authored and lizzie committed Nov 16, 2024
1 parent 297bb63 commit 5f2bb80
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 93 deletions.
29 changes: 20 additions & 9 deletions client/src/components/main/chat/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}

.ruled-paper {
background-size: 100% 3em; /* Adjusts spacing between horizontal lines */
background-size: 100% 3em;
background-color: #FFFCF5;
flex-grow: 1;
width: 100%;
Expand All @@ -32,11 +32,21 @@
padding: 20px;
}

.community-title {
margin: 0 auto; /* Horizontally center */
background: #6D89B0;
text-align: start;
padding: 10px;
border-radius: 5px;
color: white;
font-size: 24px;
font-weight: bold;
}

.chat-title {
padding: 20px;
font-size: 24px;
font-weight: bold;

}

.chat-header {
Expand All @@ -62,9 +72,9 @@
.chat-text-user, .chat-text-me {
display: inline-block;
font-size: 24px;
max-width: 100%; /* Ensure the width doesn’t stretch too far */
white-space: nowrap; /* Prevents text from wrapping within a single message */
margin-bottom: 5px; /* Adds space between messages */
max-width: 100%;
white-space: nowrap;
margin-bottom: 5px;
}

.chat-text-user {
Expand All @@ -76,19 +86,20 @@
}

.chat-text-me {
float: right; /* Aligns the chat bubble to the right */
float: right;
padding: 3px 15px;
background: #DFAEA1;
color: #fff !important;
border-radius: 20px 20px 3px 20px;
margin-bottom: 10px; /* Optional: Adds space below the bubble */
clear: both; /* Ensures no other floated elements affect the layout */
border-radius: 30px 20px 3px 30px;
margin-bottom: 10px;
clear: both;
}

.username {
background: #6D89B0;
color: white;
font-size: 24px;
max-width: 975px;
font-weight: bold;
}

Expand Down
108 changes: 24 additions & 84 deletions client/src/components/main/chat/index.tsx
Original file line number Diff line number Diff line change
@@ -1,100 +1,40 @@
import React, { useEffect, useState } from 'react';
import './index.css';
import { FaCaretUp, FaCaretDown } from 'react-icons/fa';
import { useLocation } from 'react-router-dom';
import { query, collection, orderBy, onSnapshot, addDoc, getDoc, doc } from 'firebase/firestore';
import { db } from '../../../firebaseConfig';
import Message from './Message';
import useUserContext from '../../../hooks/useUserContext';
import useChat from '../../../hooks/useChat';

const ChatPage = () => {
const { pathname } = useLocation();
const { user } = useUserContext();
const [currentMessage, setCurrentMessage] = useState<string>('');
const [messages, setMessages] = useState<{ message: string; username: string; sendTo: string }[]>(
[],
);
const [send, setSend] = useState<string>('');
const messageContainer = document.querySelector('.scrollable-container');

const handleSendTo = (e: React.ChangeEvent<HTMLInputElement>) => {
setSend(e.target.value);
};

const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setCurrentMessage(e.target.value);
};

const saveMessagesToUserAccount = async (message: string, sendTo: string) => {
try {
if (user && user.username) {
await addDoc(collection(db, 'messages'), {
username: user.username,
message,
sendTo,
timestamp: new Date(),
});
setCurrentMessage(''); // Clear the input after sending
} else {
console.error('User not authenticated or username missing');
}
} catch (error) {
console.error('Error saving message:', error);
}
};

useEffect(() => {
console.log('Messages useEffect triggered');

if (!user?.username) return;

// Query to retrieve messages where the current user is either the sender (username) or receiver (sendTo)
const q = query(collection(db, 'messages'), orderBy('timestamp', 'asc'));

const unsubscribe = onSnapshot(q, querySnapshot => {
const loadedMessages = querySnapshot.docs
// eslint-disable-next-line @typescript-eslint/no-shadow
.map(doc => ({
message: doc.data().message,
username: doc.data().username,
sendTo: doc.data().sendTo,
timestamp: doc.data().timestamp,
}))
.filter(
msg =>
(msg.username === user.username && msg.sendTo === send) ||
(msg.sendTo === user.username && msg.username === send),
);
setMessages(loadedMessages);
});

// Cleanup the snapshot listener on component unmount
// eslint-disable-next-line consistent-return
return () => unsubscribe();
}, [user, send]);

const scrollUp = () => {
messageContainer?.scrollBy(0, -100);
};

const scrollDown = () => {
messageContainer?.scrollBy(0, 100);
};
const {
currentMessage,
messages,
send,
handleSendTo,
handleInputChange,
scrollUp,
saveMessagesToUserAccount,
scrollDown,
} = useChat();

return (
<div className='chat-container'>
<div className='chat-header d-flex'>
<span className='chat-title'>chatting now: </span>
{pathname.includes('community') ? (
<span>community</span>
<span className='chat-title'>
chatting now: <span className='community-title'>community</span>
</span>
) : (
<input
className='username'
id='searchBar'
placeholder={'email'}
type='text'
onChange={handleSendTo}
/>
<>
<span className='chat-title'>chatting now: </span>
<input
className='username'
id='searchBar'
placeholder={'email'}
type='text'
onChange={handleSendTo}
/>
</>
)}
</div>
<div className='ruled-paper scrollable-container'>
Expand Down
92 changes: 92 additions & 0 deletions client/src/hooks/useChat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { useEffect, useState } from 'react';
import { addDoc, collection, onSnapshot, orderBy, query } from 'firebase/firestore';
import useUserContext from './useUserContext';
import { db } from '../firebaseConfig';

const useChat = () => {
const { user } = useUserContext();
const [currentMessage, setCurrentMessage] = useState<string>('');
const [messages, setMessages] = useState<{ message: string; username: string; sendTo: string }[]>(
[],
);
const [send, setSend] = useState<string>('');
const messageContainer = document.querySelector('.scrollable-container');

const handleSendTo = (e: React.ChangeEvent<HTMLInputElement>) => {
setSend(e.target.value);
};

const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setCurrentMessage(e.target.value);
};

const saveMessagesToUserAccount = async (message: string, sendTo: string) => {
try {
if (user && user.username) {
await addDoc(collection(db, 'messages'), {
username: user.username,
message,
sendTo,
timestamp: new Date(),
});
setCurrentMessage(''); // Clear the input after sending
} else {
console.error('User not authenticated or username missing');
}
} catch (error) {
console.error('Error saving message:', error);
}
};

useEffect(() => {
console.log('Messages useEffect triggered');

if (!user?.username) return;

// Query to retrieve messages where the current user is either the sender (username) or receiver (sendTo)
const q = query(collection(db, 'messages'), orderBy('timestamp', 'asc'));

const unsubscribe = onSnapshot(q, querySnapshot => {
const loadedMessages = querySnapshot.docs
// eslint-disable-next-line @typescript-eslint/no-shadow
.map(doc => ({
message: doc.data().message,
username: doc.data().username,
sendTo: doc.data().sendTo,
timestamp: doc.data().timestamp,
}))
.filter(
msg =>
(msg.username === user.username && msg.sendTo === send) ||
(msg.sendTo === user.username && msg.username === send),
);
setMessages(loadedMessages);
});

// Cleanup the snapshot listener on component unmount
// eslint-disable-next-line consistent-return
return () => unsubscribe();
}, [user, send]);

const scrollUp = () => {
messageContainer?.scrollBy(0, -100);
};

const scrollDown = () => {
messageContainer?.scrollBy(0, 100);
};

return {
user,
currentMessage,
messages,
send,
handleSendTo,
handleInputChange,
scrollUp,
saveMessagesToUserAccount,
scrollDown,
};
};

export default useChat;

0 comments on commit 5f2bb80

Please sign in to comment.