From edce36468ad30dbab9ef7918ae6a96a415ab2ec5 Mon Sep 17 00:00:00 2001 From: Pranshu Patel Date: Mon, 22 Apr 2024 02:08:10 +0530 Subject: [PATCH] Refactor PostMessage component to improve code readability and error handling --- src/components/(guestbook)/PostMessage.tsx | 32 +++++++++------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/src/components/(guestbook)/PostMessage.tsx b/src/components/(guestbook)/PostMessage.tsx index 93c2f4f..a8a7102 100644 --- a/src/components/(guestbook)/PostMessage.tsx +++ b/src/components/(guestbook)/PostMessage.tsx @@ -13,25 +13,19 @@ const PostMessage: React.FC = ({ user }) => { const [postSuccess, setPostSuccess] = useState(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 (