Rev 5186 | Rev 5188 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
/* eslint-disable camelcase */
/* eslint-disable react/prop-types */
import React, { useEffect, useState } from 'react'
import { useDispatch } from 'react-redux'
import { addNotification } from '../redux/notification/notification.actions'
import { axios } from '../utils'
import ChatMail from './inmail/inmail'
import Sidebar from './inmail/sidebar'
const defaultPages = { current: 1, last: 1 }
const Inmail = () => {
const [messages, setMessages] = useState([])
const [pages, setPages] = useState(defaultPages)
const [selectedConversation, setSelectedConversation] = useState(null)
const [loading, setLoading] = useState(false)
const dispatch = useDispatch()
const getMessages = async ({ messages_link = '', page = defaultPages.current }) => {
try {
setLoading(true)
const { data: response } = await axios.get(`${messages_link}?page=' + ${page}`)
if (!response.success) {
const errorMessage = response.data
setPages(defaultPages)
dispatch(addNotification({ style: 'danger', msg: errorMessage.message }))
setLoading(false)
return
}
if (page > 1) {
setPages((prevPages) => ({ ...prevPages, last: response.pagination.last }))
setMessages(prev => [...prev, response.data])
return
}
setPages((prevPages) => ({ ...prevPages, last: response.pagination.last }))
setMessages(response.data)
setLoading(false)
} catch (error) {
const errorMessage = new Error(error)
dispatch(addNotification({ style: 'danger', msg: errorMessage.message }))
}
}
const handleSend = async (sendUrl = '', message = {}) => {
try {
const formData = new FormData()
Object.entries(message).forEach(([key, value]) => formData.append(key, value))
const { data: response } = await axios.post(sendUrl, formData)
setMessages(prev => [response.data, ...prev])
} catch (error) {
console.log('>>: err0r > ', error)
}
}
const changeConversation = (conversation) => setSelectedConversation(conversation)
const getMoreMessages = () => {
if (pages.current + 1 > pages.last) {
return null
}
setPages((prevPages) => ({ ...prevPages, current: prevPages.current + 1 }))
}
useEffect(() => {
setPages(defaultPages)
getMessages(selectedConversation?.messages_link)
}, [selectedConversation])
useEffect(() => {
let timer
if (!loading && selectedConversation) {
timer = setTimeout(() => {
getMessages(selectedConversation.messages_link)
}, 2000)
}
return () => {
clearTimeout(timer)
}
}, [loading, selectedConversation])
useEffect(() => {
getMessages({ messages_link: selectedConversation?.messages_link, page: pages.current })
}, [pages.current])
return (
<div className="messages-sec container">
<div className="row">
<div className={`col-12 col-md-4 ${selectedConversation && 'msgs-hide'}`}>
<Sidebar
setConversation={changeConversation}
selectedConversation={selectedConversation}
/>
</div>
<div className={`col-12 col-md-8 ${!selectedConversation && 'msgs-hide'}`}>
<ChatMail
messages={messages}
selectedConversation={selectedConversation}
setConversation={changeConversation}
getMoreMessages={() => getMoreMessages()}
onSend={handleSend}
/>
</div>
</div>
</div>
)
}
export default Inmail