Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3644 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useState } from 'react';
2
import { Box, IconButton } from '@mui/material';
3
import { AttachFile, EmojiEmotions, SendRounded } from '@mui/icons-material';
4
import EmojiPicker from 'emoji-picker-react';
5
 
6
import { useModal } from '@shared/hooks';
7
 
8
import { FileModal, Input, Menu } from '@shared/components';
9
 
10
export const ChatForm = ({ onSubmit }) => {
11
  const [message, setMessage] = useState('');
12
  const [file, setFile] = useState(null);
13
 
14
  const { showModal, closeModal } = useModal();
15
 
16
  const handleInputChange = ({ target }) => {
17
    setMessage(target.value);
18
  };
19
 
20
  const onEmojiClick = (emojiObject) => {
21
    setMessage((prevInput) => prevInput + emojiObject.emoji);
22
  };
23
 
24
  const attachFile = () => {
25
    showModal(
26
      'Elegir archivo',
27
      <FileModal
28
        onSubmit={(data) => {
29
          setFile(data);
30
          closeModal();
31
        }}
32
      />
33
    );
34
  };
35
 
36
  const handleSubmit = () => {
37
    if (!message.trim()) return;
38
    onSubmit({ message, file });
39
    setMessage('');
40
    setFile(null);
41
  };
42
 
43
  const handleKeyDown = (e) => {
44
    if (e.key === 'Enter') {
45
      handleSubmit();
46
    }
47
  };
48
 
49
  return (
50
    <Box style={{ display: 'flex', alignItems: 'center', gap: 1, width: '100%' }}>
51
      <Menu icon={<EmojiEmotions />}>
52
        <Menu.Item>
53
          <EmojiPicker onEmojiClick={onEmojiClick} />
54
        </Menu.Item>
55
      </Menu>
56
 
57
      <IconButton onClick={attachFile}>
58
        <AttachFile />
59
      </IconButton>
60
      <Input
61
        name='message'
62
        placeholder='Escribe un mensaje'
63
        onChange={handleInputChange}
64
        value={message}
65
        onKeyDown={handleKeyDown}
66
        endAdornment={
67
          <IconButton type='submit' sx={{ padding: 0.4 }} onClick={handleSubmit}>
68
            <SendRounded />
69
          </IconButton>
70
        }
71
      />
72
    </Box>
73
  );
74
};