forked from OurProjectsCombined/chat-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed frontend functionality for get conversations, added send messag…
…es & get messages
- Loading branch information
Keagan Aromin
authored and
Keagan Aromin
committed
Jan 12, 2025
1 parent
27965df
commit b8eea68
Showing
9 changed files
with
157 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const MessageSkeleton = () => { | ||
return ( | ||
<> | ||
<div className='flex gap-3 items-center'> | ||
<div className='skeleton w-10 h-10 rounded-full shrink-0'></div> | ||
<div className='flex flex-col gap-1'> | ||
<div className='skeleton h-4 w-40'></div> | ||
<div className='skeleton h-4 w-40'></div> | ||
</div> | ||
</div> | ||
<div className='flex gap-3 items-center justify-end'> | ||
<div className='flex flex-col gap-1'> | ||
<div className='skeleton h-4 w-40'></div> | ||
</div> | ||
<div className='skeleton w-10 h-10 rounded-full shrink-0'></div> | ||
</div> | ||
</> | ||
); | ||
}; | ||
export default MessageSkeleton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useState, useEffect } from "react"; | ||
import useConversation from "../zustand/useConversation" | ||
import toast from "react-hot-toast"; | ||
|
||
const useGetMessages = () => { | ||
const [loading, setLoading] = useState(false) | ||
const {messages, setMessages, selectedConversation} = useConversation(); | ||
|
||
useEffect(() => { | ||
const getMessages = async () => { | ||
setLoading(true) | ||
try { | ||
const res = await fetch(`/api/messages/${selectedConversation._id}`) //GET | ||
const data = await res.json(); | ||
if(data.error) throw new Error(data.error) | ||
setMessages(data) | ||
} catch (error) { | ||
toast.error(error.message) | ||
} finally { | ||
setLoading(false) | ||
} | ||
} | ||
|
||
if (selectedConversation?._id) getMessages() | ||
}, [selectedConversation?._id, setMessages]) | ||
return {messages, loading} | ||
} | ||
|
||
export default useGetMessages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useState } from 'react' | ||
import useConversation from '../zustand/useConversation.js' | ||
import toast from 'react-hot-toast' | ||
|
||
const useSendMessage = () => { | ||
const [loading, setLoading] = useState(false) | ||
const {messages, setMessages, selectedConversation} = useConversation(); | ||
|
||
const sendMessage = async (message) => { | ||
setLoading(true) | ||
try { | ||
const res = await fetch(`/api/messages/send/${selectedConversation._id}`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({message}) | ||
}) | ||
const data = await res.json() | ||
if (data.error) throw new Error(data.error) | ||
|
||
//... (spread opreator) expands elements so that you can add next one without nesting array | ||
setMessages([...messages, data]) | ||
} catch (error) { | ||
toast.error(error.message) | ||
} finally { | ||
setLoading(false) | ||
} | ||
} | ||
return {sendMessage, loading}; | ||
} | ||
|
||
export default useSendMessage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function extractTime(dateString) { | ||
const date = new Date(dateString); | ||
const hours = padZero(date.getHours()); | ||
const minutes = padZero(date.getMinutes()); | ||
return `${hours}:${minutes}`; | ||
} | ||
|
||
// Helper function to pad single-digit numbers with a leading zero | ||
function padZero(number) { | ||
return number.toString().padStart(2, "0"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters