Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3704 | Rev 3711 | 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("");
3710 stevensc 15
    const { handleSubmit, register, errors } = 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
 
3710 stevensc 44
    const handleSend = ({ message }) => onSend(message)
45
 
1748 stevensc 46
    return (
2749 stevensc 47
        <div className="message-send-area border-gray border-radius">
1748 stevensc 48
            <form id="form-send-message" name="form-send-message"
3710 stevensc 49
                onSubmit={handleSubmit(handleSend)}
1748 stevensc 50
            >
51
                <div className="mf-field">
52
                    <input
2655 stevensc 53
                        type="file"
54
                        name="file"
3710 stevensc 55
                        hidden
56
                        ref={(e) => fileInputEl.current = e}
2655 stevensc 57
                        accept={permittedFiles}
58
                        onChange={handleUploadFile}
59
                    />
60
                    <button
61
                        type="button"
3704 stevensc 62
                        className='inputIcon ti-clip icon uploadFile text-gray'
2655 stevensc 63
                        id="uploadFile"
64
                        onClick={() => fileInputEl.current.click()}
65
                    />
66
                    <input
1748 stevensc 67
                        type="text"
68
                        name="message"
1883 steven 69
                        className="border-radius"
3710 stevensc 70
                        ref={register({ required: 'Este campo es requerido' })}
1748 stevensc 71
                        placeholder={backendVars.labelWriteMessage}
72
                    />
3710 stevensc 73
                    {errors.message && <p>{errors.message.message}</p>}
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