7317 |
stevensc |
1 |
import React, { useRef, useState } from 'react'
|
|
|
2 |
import { axios } from '../../../utils'
|
|
|
3 |
import { addNotification } from '../../../redux/notification/notification.actions'
|
|
|
4 |
import { useDispatch, useSelector } from 'react-redux'
|
|
|
5 |
import SendFileModal from '../../modals/FileModal'
|
|
|
6 |
import AttachFileIcon from '@mui/icons-material/AttachFile'
|
|
|
7 |
import SentimentVerySatisfiedRoundedIcon from '@mui/icons-material/SentimentVerySatisfiedRounded'
|
|
|
8 |
|
|
|
9 |
import Emojione from '../emojione/Emojione'
|
|
|
10 |
|
|
|
11 |
const TextBox = ({
|
|
|
12 |
uploadUrl = '',
|
|
|
13 |
isNotSeen = false,
|
|
|
14 |
markRead = () => false,
|
|
|
15 |
onSend,
|
|
|
16 |
}) => {
|
|
|
17 |
const [isShowFileModal, setIsShowFileModal] = useState(false)
|
|
|
18 |
const [showEmojiTab, setShowEmojiTab] = useState(false)
|
|
|
19 |
const [loading, setLoading] = useState(false)
|
|
|
20 |
const textAreaEl = useRef(null)
|
|
|
21 |
const dispatch = useDispatch()
|
|
|
22 |
const labels = useSelector(({ intl }) => intl.labels)
|
|
|
23 |
|
|
|
24 |
const handleShowEmojiTab = () => {
|
|
|
25 |
setShowEmojiTab(!showEmojiTab)
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
const toggleFileModal = () => {
|
|
|
29 |
setIsShowFileModal(!isShowFileModal)
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
const handleClickEmoji = (e) => {
|
|
|
33 |
const shortname = e.currentTarget.dataset.shortname
|
|
|
34 |
const currentText = textAreaEl.current.value
|
|
|
35 |
const cursorPosition = textAreaEl.current.selectionStart
|
|
|
36 |
const textBehind = currentText.substring(0, cursorPosition)
|
|
|
37 |
const textForward = currentText.substring(cursorPosition)
|
|
|
38 |
const unicode = emojione.shortnameToUnicode(shortname)
|
|
|
39 |
textAreaEl.current.value = `${textBehind}${unicode}${textForward}`
|
|
|
40 |
textAreaEl.current.focus()
|
|
|
41 |
textAreaEl.current.setSelectionRange(
|
|
|
42 |
cursorPosition + unicode.length,
|
|
|
43 |
cursorPosition + unicode.length
|
|
|
44 |
)
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
const onKeyDown = async (e) => {
|
|
|
48 |
if (e.key !== 'Enter') return false
|
|
|
49 |
e.preventDefault()
|
|
|
50 |
const message = e.target.value
|
|
|
51 |
await onSend(message)
|
|
|
52 |
e.target.value = ''
|
|
|
53 |
setShowEmojiTab(false)
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
const sendFile = (file) => {
|
|
|
57 |
setLoading(true)
|
|
|
58 |
const formData = new FormData()
|
|
|
59 |
formData.append('file', file)
|
|
|
60 |
|
|
|
61 |
axios
|
|
|
62 |
.post(uploadUrl, formData)
|
|
|
63 |
.then(({ data: response }) => {
|
|
|
64 |
const { success, data } = response
|
|
|
65 |
if (!success) {
|
|
|
66 |
const errorMessage =
|
|
|
67 |
typeof data === 'string' ? data : 'Ha ocurrido un error'
|
|
|
68 |
dispatch(addNotification({ style: 'success', msg: errorMessage }))
|
|
|
69 |
return
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
toggleFileModal()
|
|
|
73 |
})
|
|
|
74 |
.finally(() => setLoading(false))
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
return (
|
|
|
78 |
<>
|
|
|
79 |
<div className="chat-text_box">
|
|
|
80 |
<button className="btn" onClick={toggleFileModal}>
|
|
|
81 |
<AttachFileIcon />
|
|
|
82 |
</button>
|
|
|
83 |
<button className="btn" onClick={handleShowEmojiTab}>
|
|
|
84 |
<SentimentVerySatisfiedRoundedIcon />
|
|
|
85 |
</button>
|
|
|
86 |
<textarea
|
|
|
87 |
placeholder={labels.write_a_message}
|
|
|
88 |
onKeyDown={onKeyDown}
|
|
|
89 |
ref={textAreaEl}
|
|
|
90 |
onFocus={() => isNotSeen && markRead()}
|
|
|
91 |
/>
|
|
|
92 |
<div
|
|
|
93 |
className="emojione-container"
|
|
|
94 |
style={{ display: showEmojiTab ? 'block' : 'none' }}
|
|
|
95 |
>
|
|
|
96 |
<Emojione onClickEmoji={handleClickEmoji} />
|
|
|
97 |
</div>
|
|
|
98 |
</div>
|
|
|
99 |
<SendFileModal
|
|
|
100 |
isShow={isShowFileModal}
|
|
|
101 |
onHide={toggleFileModal}
|
|
|
102 |
onComplete={sendFile}
|
|
|
103 |
loading={loading}
|
|
|
104 |
/>
|
|
|
105 |
</>
|
|
|
106 |
)
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
export default TextBox
|