Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

arreglos del chat #211

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 40 additions & 73 deletions uniEmpleos/src/pages/ChatPage/ChatPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ const ChatPage = () => {

useEffect(() => {
obtainMessages()
if (currentChat !== "") {
const intervalMensajesChatActual = setInterval(() => {
obtainMessages()
}, 5000)
return () => clearInterval(intervalMensajesChatActual)
}
}, [currentChat])

const scrollDown = () => {
Expand Down Expand Up @@ -97,7 +103,7 @@ const ChatPage = () => {
scrollDown()
obtainMessages()
}

const handleChat = (receptor, id) => {
setCurrentChat(receptor)
setIdCurrentChat(id)
Expand Down Expand Up @@ -126,24 +132,14 @@ const ChatPage = () => {

//Intervals
// Actualizar lista de chats

useEffect(() => {
obtainLastChats()
const intervalListadeChats = setInterval(() => {
obtainLastChats()
}, 10000)
}, 5000)
return () => clearInterval(intervalListadeChats)
}, [])

// Actualizar mensajes de chat actual

useEffect(() => {
const intervalMensajesChatActual = setInterval(() => {
obtainMessages()
}, 10000)
return () => clearInterval(intervalMensajesChatActual)
}, [])

return (
<div className={style.container}>
<Header userperson="student" />
Expand All @@ -156,41 +152,24 @@ const ChatPage = () => {
<div className={style.generalChatContainer}>
<div className={style.chatsContainer}>
{apiLastChats.data && apiLastChats.data.messages.length > 0 ? (
apiLastChats.data.messages.map((chat) => {
if (chat.last_message.length === 0) {
return null
} else {
const fileType = isImage(chat.last_message)
const pfpUrl = chat.user_photo ? API_URL + "/api/uploads/" + chat.user_photo : "/images/pfp.svg"
if (fileType) {
return (
<Chat
pfp={pfpUrl}
name={chat.user_name}
lastChat="Foto"
key={chat.postulation_id}
id_postulacion={chat.postulation_id.toString()}
onClick={() =>
handleChat(chat.user_id, chat.postulation_id)
}
/>
)
} else {
return (
<Chat
pfp={pfpUrl}
name={chat.user_name}
lastChat={chat.last_message}
key={chat.postulation_id}
id_postulacion={chat.postulation_id.toString()}
onClick={() =>
handleChat(chat.user_id, chat.postulation_id)
}
/>
)
}
}
})
apiLastChats.data.messages.map((chat) =>
chat.last_message.length === 0 ? null : (
<Chat
pfp={
chat.user_photo
? API_URL + "/api/uploads/" + chat.user_photo
: "/images/pfp.svg"
}
name={chat.user_name}
lastChat={
isImage(chat.last_message) ? "Foto" : chat.last_message
}
key={chat.postulation_id}
id_postulacion={chat.postulation_id.toString()}
onClick={() => handleChat(chat.user_id, chat.postulation_id)}
/>
)
)
) : (
<div className={style.noUsersMessage}>No hay chats recientes.</div>
)}
Expand All @@ -201,32 +180,20 @@ const ChatPage = () => {
const side = message.id_emisor === user.id_user ? "right" : "left"
number += 1
const fileType = isImage(message.mensaje)
const pfpUrlEmisor = message.emisor_foto ? API_URL + "/api/uploads/" + message.emisor_foto : "/images/pfp.svg"
if (fileType) {
return (
<Message
key={[message.id, message.id_emisor, number]}
pfp={pfpUrlEmisor}
name={message.emisor_nombre}
time={message.tiempo}
message=""
file={message.mensaje}
side={side}
/>
)
} else {
return (
<Message
key={[message.id, message.id_emisor, number]}
pfp={pfpUrlEmisor}
name={message.emisor_nombre}
time={message.tiempo}
message={message.mensaje}
file=""
side={side}
/>
)
}
const pfpUrlEmisor = message.emisor_foto
? API_URL + "/api/uploads/" + message.emisor_foto
: "/images/pfp.svg"
return (
<Message
key={[message.id, message.id_emisor, number]}
pfp={pfpUrlEmisor}
name={message.emisor_nombre}
time={message.tiempo}
message={fileType ? "" : message.mensaje}
file={fileType ? message.mensaje : ""}
side={side}
/>
)
})
) : (
<div className={style.noMessagesMessage}>No hay mensajes.</div>
Expand Down
Loading