Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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