Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
3704 stevensc 1
/* eslint-disable react/prop-types */
2655 stevensc 2
import React, { useRef, useState } from 'react'
3
import FileModal from '../../mobile-chat/mobile-chat/chat/fileModal/FileModal';
4
import { axios } from '../../utils';
2771 stevensc 5
import { addNotification } from '../../redux/notification/notification.actions';
6
import { useDispatch } from 'react-redux';
1748 stevensc 7
 
2655 stevensc 8
const permittedFiles = "video/mp4, video/mpeg, video/webm, application/pdf, image/jpeg, image/png, image/jpg";
1748 stevensc 9
 
3044 stevensc 10
const MessageBox = ({ onSend, backendVars, sendLink, setMsgs }) => {
2655 stevensc 11
 
12
    const fileInputEl = useRef(null);
1748 stevensc 13
    const [text, setText] = useState('')
2655 stevensc 14
    const [selectedFile, setSelectedFile] = useState("");
2773 stevensc 15
    const dispatch = useDispatch()
1748 stevensc 16
 
2655 stevensc 17
    const handleUploadFile = (e) => {
18
        const file = e.target.files[0];
19
        if (file) {
20
            setSelectedFile(file);
21
        }
22
    };
23
 
24
    const removeSelectedFile = () => {
25
        setSelectedFile("");
26
    };
27
 
28
    const handleSendFile = () => {
29
        const formData = new FormData();
3041 stevensc 30
        formData.append("filename", selectedFile);
3043 stevensc 31
        formData.append("message", 'File');
2655 stevensc 32
 
33
        axios.post(sendLink, formData)
2773 stevensc 34
            .then(({ data }) => {
35
                if (!data.success) {
36
                    return dispatch(addNotification({
2772 stevensc 37
                        style: "danger",
38
                        msg: "Ha ocurrido un error"
2771 stevensc 39
                    }))
2655 stevensc 40
                }
3044 stevensc 41
 
42
                setMsgs(prev => [data.data, ...prev])
2771 stevensc 43
                setSelectedFile("");
2655 stevensc 44
            });
45
    };
46
 
1748 stevensc 47
    return (
2749 stevensc 48
        <div className="message-send-area border-gray border-radius">
1748 stevensc 49
            <form id="form-send-message" name="form-send-message"
50
                onSubmit={e => {
51
                    e.preventDefault()
52
                    onSend(text)
53
                    setText('')
54
                }}
55
            >
56
                <div className="mf-field">
57
                    <input
2655 stevensc 58
                        type="file"
59
                        name="file"
60
                        id="file"
61
                        ref={(e) => {
62
                            fileInputEl.current = e;
63
                        }}
64
                        accept={permittedFiles}
65
                        hidden
66
                        onChange={handleUploadFile}
67
                    />
68
                    <button
69
                        type="button"
3704 stevensc 70
                        className='inputIcon ti-clip icon uploadFile text-gray'
2655 stevensc 71
                        id="uploadFile"
72
                        onClick={() => fileInputEl.current.click()}
73
                    />
74
                    <input
1748 stevensc 75
                        type="text"
76
                        name="message"
77
                        id="message"
1883 steven 78
                        className="border-radius"
1748 stevensc 79
                        value={text}
80
                        placeholder={backendVars.labelWriteMessage}
81
                        onChange={e => setText(e.target.value || '')}
82
                    />
1883 steven 83
                    <button type="submit" className="btn btn-secondary p-1 inmail-submit-btn ">
3704 stevensc 84
                        <i className="fas fa-chevron-right" />
1748 stevensc 85
                    </button>
86
                </div>
87
            </form>
2657 stevensc 88
            {
89
                selectedFile
90
                &&
91
                <FileModal
92
                    file={selectedFile}
93
                    onCancel={removeSelectedFile}
94
                    onSend={handleSendFile}
95
                />
96
            }
1748 stevensc 97
        </div>
98
    )
99
}
100
export default MessageBox