Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3666 | Rev 3670 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3476 stevensc 1
import React, { useCallback, useEffect, useState } from 'react';
2
import { useDropzone } from 'react-dropzone';
3
import { IconButton, styled, Typography } from '@mui/material';
4
import { Delete } from '@mui/icons-material';
5
 
6
const DragAndDropContainer = styled('div')`
7
  display: flex;
8
  flex-direction: column;
9
  align-items: center;
10
  padding: 2rem 0;
11
  border: 2px dashed #eee;
12
  border-radius: 2;
13
  background-color: #fafafa;
14
  color: #bdbdbd;
15
  outline: none;
16
  transition: border 0.24s ease-in-out;
17
  text-align: center;
18
  cursor: pointer;
19
`;
20
 
21
const PreviewContainer = styled('div')`
22
  display: flex;
23
  position: relative;
24
  justify-content: center;
25
 
26
  img,
27
  video,
28
  object {
29
    width: 80%;
30
    max-height: 200px;
31
    max-width: 350px;
32
    object-fit: contain;
33
    display: block;
34
    margin: 0 auto;
35
  }
36
`;
37
 
38
const CloseButton = styled(IconButton)`
39
  position: absolute;
40
  background-color: #000;
41
  color: #fff;
42
  right: 1rem;
43
  svg {
44
    font-size: 1rem;
45
  }
46
`;
47
 
48
const FILE_TYPES = {
49
  image: 'image/jpeg, image/png, image/jpg',
50
  file: 'application/pdf, application/vnd.openxmlformats-officedocument.presentationml.presentation',
3639 stevensc 51
  video: 'video/mp4, video/mpeg, video/webm, video/quicktime',
52
  all: 'image/jpeg, image/png, image/jpg, application/pdf, application/vnd.openxmlformats-officedocument.presentationml.presentation, video/mp4, video/mpeg, video/webm, video/quicktime'
3476 stevensc 53
};
54
 
55
export function FilePicker({
56
  type = 'image',
57
  multiple = false,
58
  maxFiles = 1,
59
  description = 'Arrastra el archivo aqui, o haga click para seleccionar',
60
  defaultFiles = null,
61
  onChange = () => {}
62
}) {
63
  const [errors, setErrors] = useState([]);
64
  const [files, setFiles] = useState([]);
65
 
66
  const onDrop = useCallback((acceptedFiles, fileRejections) => {
3669 stevensc 67
    onChange(acceptedFiles);
3642 stevensc 68
    setFiles(acceptedFiles);
3476 stevensc 69
    if (fileRejections.length > 0) {
70
      setErrors(fileRejections.map((file) => file.errors[0].message));
71
    } else {
72
      setErrors([]);
73
    }
74
  }, []);
75
 
76
  const { getRootProps, getInputProps } = useDropzone({
77
    accept: FILE_TYPES[type] ?? FILE_TYPES.image,
78
    multiple,
79
    maxFiles,
80
    onDrop
81
  });
82
 
83
  const removeFile = () => {
84
    setFiles([]);
85
    setErrors([]);
86
  };
87
 
88
  const filePreviewTest = (file) => {
3642 stevensc 89
    const preview = URL.createObjectURL(file);
90
    const type = file.type.split('/')[0];
3641 stevensc 91
 
3642 stevensc 92
    switch (type) {
3476 stevensc 93
      case 'image':
3642 stevensc 94
        return <img src={preview} />;
3476 stevensc 95
      case 'video':
3642 stevensc 96
        return <video src={preview} width='400' height='300' controls muted />;
3658 stevensc 97
      case 'application':
98
        return <object data={preview} type={file.type} width='400' height='200' />;
3476 stevensc 99
      case 'file':
3642 stevensc 100
        return <object data={preview} type={file.type} width='400' height='200' />;
3639 stevensc 101
      case 'audio':
3642 stevensc 102
        return <audio src={preview} controls muted />;
3639 stevensc 103
 
3476 stevensc 104
      default:
105
        break;
106
    }
107
  };
108
 
109
  useEffect(() => {
110
    if (defaultFiles) setFiles(defaultFiles);
111
  }, [defaultFiles]);
112
 
113
  if (files.length) {
114
    return (
115
      <PreviewContainer>
116
        {files.map((file) => filePreviewTest(file))}
117
        <CloseButton onClick={removeFile}>
118
          <Delete />
119
        </CloseButton>
120
      </PreviewContainer>
121
    );
122
  }
123
 
124
  return (
125
    <>
126
      <DragAndDropContainer {...getRootProps()}>
127
        <input {...getInputProps()} />
128
        <p>{description}</p>
129
      </DragAndDropContainer>
130
 
131
      {errors.map((error, index) => (
132
        <Typography key={index} variant='caption' color='red'>
133
          {error}
134
        </Typography>
135
      ))}
136
    </>
137
  );
138
}