Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
5916 stevensc 1
import React, { useRef, useState } from 'react'
2
import { axios } from '../../utils'
3
import { useDispatch } from 'react-redux'
4
import { addNotification } from '../../redux/notification/notification.actions'
5
 
5344 stevensc 6
import AttachFileIcon from '@mui/icons-material/AttachFile'
7
import SentimentVerySatisfiedRoundedIcon from '@mui/icons-material/SentimentVerySatisfiedRounded'
8
import Emojione from '../../chat/chat/personal-chat/emojione/Emojione'
6007 stevensc 9
import SendFileModal from '../../chat/chat/personal-chat/send-file-modal/SendFileModal'
5344 stevensc 10
 
11
const TextBox = ({
12
  uploadUrl = '',
13
  isNotSeen = false,
14
  markRead = () => false,
5916 stevensc 15
  onSend,
5344 stevensc 16
}) => {
5916 stevensc 17
  const [isShowFileModal, setIsShowFileModal] = useState(false)
5344 stevensc 18
  const [showEmojiTab, setShowEmojiTab] = useState(false)
5916 stevensc 19
  const [loading, setLoading] = useState(false)
5344 stevensc 20
  const textAreaEl = useRef(null)
21
 
5916 stevensc 22
  const dispatch = useDispatch()
5344 stevensc 23
 
5916 stevensc 24
  const handleShowEmojiTab = () => {
25
    setShowEmojiTab(!showEmojiTab)
26
  }
5344 stevensc 27
 
5916 stevensc 28
  const toggleFileModal = () => {
29
    setIsShowFileModal(!isShowFileModal)
30
  }
31
 
5344 stevensc 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
 
5916 stevensc 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
 
5344 stevensc 77
  return (
5916 stevensc 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={CHAT_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
    </>
5344 stevensc 106
  )
107
}
108
 
109
export default TextBox