Skip to content

Commit

Permalink
Refactor PostMessage component to improve code readability and error …
Browse files Browse the repository at this point in the history
…handling
  • Loading branch information
pranshu05 committed Apr 21, 2024
1 parent 60fab6d commit edce364
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/components/(guestbook)/PostMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,19 @@ const PostMessage: React.FC<PostMessageProps> = ({ user }) => {
const [postSuccess, setPostSuccess] = useState<boolean>(false);

const handlePostMessage = async () => {
newMessage.trim() === '' && (setShowWarning(true), setTimeout(() => setShowWarning(false), 2000));

const messagesCollection = collection(db, 'messages');

try {
await addDoc(messagesCollection, {
displayName: user.displayName,
photoURL: user.photoURL,
timestamp: serverTimestamp(),
message: newMessage,
});

setNewMessage('');
setPostSuccess(true);

setTimeout(() => setPostSuccess(false), 2000);
} catch (error) {
console.error('Error posting message:', error);
}
newMessage.trim() === '' ?
(setShowWarning(true), setTimeout(() => setShowWarning(false), 2000)) :
(
await addDoc(collection(db, 'messages'), {
displayName: user.displayName,
photoURL: user.photoURL,
timestamp: serverTimestamp(),
message: newMessage,
}),
setNewMessage(''),
setPostSuccess(true),
setTimeout(() => setPostSuccess(false), 2000)
);
};

return (
Expand Down

0 comments on commit edce364

Please sign in to comment.