Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7317 | | Comparar con el anterior | Ultima modificación | Ver Log |

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