Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4212 | Rev 4352 | 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 (
4351 stevensc 50
        <div className="chat__input-container">
3711 stevensc 51
            <form onSubmit={handleSubmit(handleSend)}>
4351 stevensc 52
                <input
53
                    type="file"
54
                    name="file"
55
                    hidden
56
                    ref={fileInputEl}
57
                    accept={permittedFiles}
58
                    onChange={handleUploadFile}
59
                />
60
                <button
61
                    type="button"
62
                    className='inputIcon ti-clip icon uploadFile text-gray'
63
                    onClick={() => fileInputEl.current.click()}
64
                />
65
                <input
66
                    type="text"
67
                    name="message"
68
                    className="border-radius"
69
                    ref={register({ required: true })}
70
                    placeholder={backendVars.labelWriteMessage}
71
                />
72
                <button type="submit" className="send_btn">
73
                    <i className="fas fa-chevron-right" />
74
                </button>
1748 stevensc 75
            </form>
3710 stevensc 76
            {selectedFile &&
2657 stevensc 77
                <FileModal
78
                    file={selectedFile}
79
                    onCancel={removeSelectedFile}
80
                    onSend={handleSendFile}
81
                />
82
            }
3710 stevensc 83
        </div >
1748 stevensc 84
    )
85
}
86
export default MessageBox