Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3719 | | 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 { useDropzone } from 'react-dropzone';
3
import { Typography } from '@mui/material';
4
import Delete from '@mui/icons-material/Delete';
5
import { DragAndDropContainer, PreviewContainer, CloseButton } from './FilePicker.styles';
6
 
7
const FILE_TYPES = {
8
  image: 'image/jpeg, image/png, image/jpg',
9
  file: 'application/pdf, application/vnd.openxmlformats-officedocument.presentationml.presentation',
10
  video: 'video/mp4, video/mpeg, video/webm, video/quicktime',
11
  audio: 'audio/mpeg, audio/mp3, audio/wav, audio/ogg',
12
  all: 'image/jpeg, image/png, image/jpg, application/pdf, application/vnd.openxmlformats-officedocument.presentationml.presentation, video/mp4, video/mpeg, video/webm, video/quicktime, audio/mpeg, audio/mp3, audio/wav, audio/ogg'
13
};
14
 
15
export function FilePicker({
16
  type = 'image',
3736 stevensc 17
 
3719 stevensc 18
  description = 'Arrastra el archivo aqui, o haga click para seleccionar',
19
  onChange = () => {}
20
}) {
21
  const [files, setFiles] = useState([]);
22
  const [errors, setErrors] = useState([]);
23
 
24
  const { getRootProps, getInputProps } = useDropzone({
25
    accept: FILE_TYPES[type] ?? FILE_TYPES.image,
3736 stevensc 26
    multiple: false,
3719 stevensc 27
    onDropAccepted: (files) => {
28
      setFiles(files);
3736 stevensc 29
      onChange(files);
3719 stevensc 30
    },
31
    onDropRejected: (rejections) => {
32
      if (rejections.length === 0) return;
33
      const errors = rejections.map((rejection) => rejection.errors[0].message);
34
      setErrors(errors);
35
    }
36
  });
37
 
38
  const removeFile = () => {
39
    setFiles([]);
40
    setErrors([]);
41
  };
42
 
43
  const filePreviewTest = (preview) => {
44
    const src = URL.createObjectURL(preview);
45
    const fileType = preview.type.split('/')[0];
46
 
47
    switch (fileType) {
48
      case 'image':
49
        return <img src={src} alt={preview.name} />;
50
      case 'video':
51
        return <video src={src} width='400' height='300' controls muted />;
52
      case 'application':
53
        return <object data={src} type='application/pdf' width='400' height='200' />;
54
      case 'audio':
55
        return <audio src={src} controls muted />;
56
      default:
57
        return <img src={src} alt={preview.name} />;
58
    }
59
  };
60
 
61
  if (files.length > 0) {
62
    return (
63
      <PreviewContainer>
64
        {files.map((file) => filePreviewTest(file))}
65
        <CloseButton onClick={removeFile}>
66
          <Delete />
67
        </CloseButton>
68
      </PreviewContainer>
69
    );
70
  }
71
 
72
  return (
73
    <>
74
      <DragAndDropContainer {...getRootProps()}>
75
        <input {...getInputProps()} />
76
        <p>{description}</p>
77
      </DragAndDropContainer>
78
 
79
      {errors.map((error, index) => (
80
        <Typography key={index} variant='caption' color='red'>
81
          {error}
82
        </Typography>
83
      ))}
84
    </>
85
  );
86
}