Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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