Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5345 | Rev 6007 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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