Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6753 stevensc 1
import React, { useState, useCallback, useEffect } from 'react'
2
import { useDropzone } from 'react-dropzone'
3
import { shareModalTypes } from '../../redux/share-modal/shareModal.types'
4
import IconButton from '@mui/material/IconButton'
5
import CloseIcon from '@mui/icons-material/Close'
6
import styled from 'styled-components'
7
 
8
import FormErrorFeedback from '../UI/FormErrorFeedback'
9
 
10
const DragAndDropContainer = styled.div`
11
  display: flex;
12
  flex-direction: column;
13
  align-items: center;
14
  padding: 2rem 0;
15
  border: 2px dashed #eee;
16
  border-radius: 2;
17
  background-color: #fafafa;
18
  color: #bdbdbd;
19
  outline: none;
20
  transition: border 0.24s ease-in-out;
21
  margin-top: 1rem;
22
  cursor: pointer;
23
`
24
 
25
const PreviewContainer = styled.div`
26
  display: flex;
27
  margin-top: 16px;
28
  position: relative;
29
  justify-content: center;
30
 
31
  img {
32
    width: auto;
33
    height: 100%;
34
    max-height: 200px;
35
    max-width: 350px;
36
    object-fit: contain;
37
  }
38
`
39
 
40
const CloseButton = styled(IconButton)`
41
  position: absolute;
42
  background-color: #000 !important;
43
  color: #fff;
44
 
45
  &:hover {
46
    color: #fff !important;
47
  }
48
`
49
 
50
const areEqual = (prevProps, nextProps) => {
51
  return prevProps.settedFile === nextProps.settedFile
52
}
53
 
54
const DropzoneComponent = ({
55
  modalType,
56
  onUploaded,
57
  settedFile,
58
  recomendationText,
59
}) => {
60
  const [errors, setErrors] = useState([])
61
  const [files, setFiles] = useState([])
62
 
63
  const acceptedMimeTypes = () => {
64
    switch (modalType) {
65
      case shareModalTypes.IMAGE:
66
        return 'image/jpeg, image/png, image/jpg'
67
      case shareModalTypes.FILE:
68
        return 'application/pdf, application/vnd.openxmlformats-officedocument.presentationml.presentation'
69
      case shareModalTypes.VIDEO:
70
        return 'video/mp4, video/mpeg, video/webm'
71
      case shareModalTypes.CHAT:
72
        return 'video/mp4, video/mpeg, video/webm, application/pdf, image/jpeg, image/png, image/jpg'
73
      default:
74
        return null
75
    }
76
  }
77
 
78
  const onDropRejected = useCallback((rejectedFiles) => {
79
    rejectedFiles.map((fileRejection) => {
80
      switch (fileRejection.errors[0].code) {
81
        case 'too-many-files':
82
          setErrors([...errors, 'solo puedes agregar 1 archivo'])
83
          break
84
        case 'file-invalid-type':
85
          setErrors([...errors, 'por favor seleccione un archivo valido'])
86
          break
87
        default:
88
          setErrors(errors)
89
          break
90
      }
91
    })
92
  }, [])
93
 
94
  const { getRootProps, getInputProps } = useDropzone({
95
    accept: acceptedMimeTypes(),
96
    multiple: false,
97
    onDrop: (acceptedFiles) => {
98
      onUploaded(acceptedFiles[0])
99
      setFiles(acceptedFiles.map((file) => file))
100
    },
101
    onDropRejected,
102
    onDropAccepted: () => {
103
      setErrors([])
104
    },
105
    maxFiles: 1,
106
  })
107
 
108
  const onDeleteFileHandler = (index) => {
109
    onUploaded('')
110
    setFiles([])
111
  }
112
 
113
  useEffect(() => {
114
    if (!settedFile) return
115
    setFiles([settedFile])
116
  }, [settedFile])
117
 
118
  const filePreviewTest = (file) => {
119
    switch (modalType) {
120
      case shareModalTypes.IMAGE:
121
        return <img src={URL.createObjectURL(file)} />
122
      case shareModalTypes.VIDEO:
123
        return (
124
          <video
125
            src={URL.createObjectURL(file)}
126
            width="400"
127
            height="300"
128
            controls
129
            autoPlay
130
            muted
131
          />
132
        )
133
      case shareModalTypes.CHAT:
134
        switch (file.type) {
135
          case 'video/mp4':
136
          case 'video/mpeg':
137
          case 'video/webm':
138
            return (
139
              <video
140
                src={URL.createObjectURL(file)}
141
                width="400"
142
                height="300"
143
                controls
144
                autoPlay
145
                muted
146
              />
147
            )
148
          case 'image/jpeg':
149
          case 'image/png':
150
          case 'image/jpg':
151
            return <img src={URL.createObjectURL(file)} />
152
          case 'application/pdf':
153
            return (
154
              <object
155
                data={URL.createObjectURL(file)}
156
                type="application/pdf"
157
                width="400"
158
                height="200"
159
              />
160
            )
161
          default:
162
            break
163
        }
164
        break
165
      case shareModalTypes.FILE:
166
        switch (file.type) {
167
          case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
168
            return (
169
              <iframe
170
                src={`https://view.officeapps.live.com/op/embed.aspx?src=${encodeURIComponent(
171
                  file
172
                )}`}
173
                width="100%"
174
                height="600px"
175
              />
176
            )
177
          case 'application/pdf':
178
            return (
179
              <object data={URL.createObjectURL(file)} type="application/pdf" />
180
            )
181
          default:
182
            break
183
        }
184
    }
185
  }
186
 
187
  return (
188
    <>
189
      {!files.length ? (
190
        <DragAndDropContainer {...getRootProps({ className: 'dropzone' })}>
191
          <input {...getInputProps()} />
192
          <p>Arrastra el archivo aqui, o haga click para seleccionar</p>
193
          {recomendationText}
194
        </DragAndDropContainer>
195
      ) : (
196
        <PreviewContainer>
197
          {files.map((file) => filePreviewTest(file))}
198
          <CloseButton className="close" onClick={() => onDeleteFileHandler()}>
199
            <CloseIcon />
200
          </CloseButton>
201
        </PreviewContainer>
202
      )}
203
      {errors.map((error, index) => (
204
        <FormErrorFeedback key={index}>{error}</FormErrorFeedback>
205
      ))}
206
    </>
207
  )
208
}
209
 
210
export default React.memo(DropzoneComponent, areEqual)