Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
3193 stevensc 1
/* eslint-disable react/prop-types */
1 www 2
import React, { useState, useCallback, useEffect } from "react";
3
import { useDropzone } from "react-dropzone";
4
import { shareModalTypes } from "../../redux/share-modal/shareModal.types";
3193 stevensc 5
import FormErrorFeedback from "../form-error-feedback/FormErrorFeedback";
1 www 6
const areEqual = (prevProps, nextProps) => {
7
  return prevProps.settedFile === nextProps.settedFile ? true : false;
8
};
9
 
3193 stevensc 10
const DropzoneComponent = (props) => {
1 www 11
  const { modalType, onUploaded, settedFile, recomendationText } = props;
12
 
13
  const [errors, setErrors] = useState([]);
14
  const [files, setFiles] = useState([]);
15
 
16
  const onDropRejected = useCallback((rejectedFiles) => {
17
    rejectedFiles.map((fileRejection) => {
18
      switch (fileRejection.errors[0].code) {
19
        case "too-many-files":
20
          setErrors([...errors, "solo puedes agregar 1 archivo"]);
21
          break;
22
        case "file-invalid-type":
23
          setErrors([...errors, "por favor seleccione un archivo valido"]);
24
          break;
25
        default:
26
          setErrors(errors);
27
          break;
28
      }
29
    });
30
  }, []);
31
 
32
  useEffect(() => {
33
    if (settedFile) setFiles([settedFile]);
34
  }, [settedFile]);
35
 
36
  const acceptedMimeTypes = () => {
37
    switch (modalType) {
38
      case shareModalTypes.IMAGE:
39
        return "image/jpeg, image/png, image/jpg";
40
      case shareModalTypes.FILE:
41
        return "application/pdf";
42
      case shareModalTypes.VIDEO:
43
        return "video/mp4, video/mpeg, video/webm";
44
      case shareModalTypes.CHAT:
45
        return "video/mp4, video/mpeg, video/webm, application/pdf, image/jpeg, image/png, image/jpg";
46
      default:
47
        return null;
48
    }
49
  };
2661 stevensc 50
 
1 www 51
  const {
52
    getRootProps,
53
    getInputProps,
54
    acceptedFiles,
55
    fileRejections,
56
  } = useDropzone({
57
    accept: acceptedMimeTypes(),
58
    multiple: false,
59
    onDrop: (acceptedFiles) => {
60
      onUploaded(acceptedFiles[0]);
61
      setFiles(acceptedFiles.map((file) => file));
62
    },
63
    onDropRejected,
64
    onDropAccepted: () => {
65
      setErrors([]);
66
    },
67
    maxFiles: 1,
68
  });
69
  const thumbStyle = {
70
    display: "inline-flex",
71
    borderRadius: "2px",
72
    border: "1px solid #eaeaea",
73
    marginBottom: "8px",
74
    marginRight: "8px",
75
    width: "150px",
76
    height: "150px",
77
    padding: "4px",
78
    boxSizing: "border-box",
79
    position: "relative",
80
    zIndex: "100",
81
  };
82
  const thumbInnerStyle = {
83
    display: "flex",
84
    minWidth: 0,
85
    overflow: "hidden",
86
    marginRight: "1.5rem",
87
  };
3257 stevensc 88
  const fileInnerStyle = {
89
    display: "flex",
90
    overflow: "hidden",
91
    margin: "auto",
92
  };
1 www 93
  const imgStyle = {
94
    display: "block",
95
    width: "auto",
96
    height: "100%",
97
    objectFit: "contain",
98
  };
99
 
100
  const onDeleteFileHandler = (index) => {
101
    const newFiles = files.filter((_, id) => id !== index);
102
    onUploaded("");
103
    setFiles(newFiles);
104
  };
105
 
106
  const CloseButtonContainer = {
107
    width: "20px",
108
    height: "20px",
109
    position: "absolute",
110
    top: "5px",
111
    right: "0px",
112
    cursor: "pointer",
113
    zIndex: "200",
114
  };
115
 
116
  const filePreviewTest = (file, id) => {
117
    switch (modalType) {
118
      case shareModalTypes.IMAGE:
119
        return (
120
          <div style={thumbStyle} key={file.name}>
121
            <div style={thumbInnerStyle}>
122
              <img src={URL.createObjectURL(file)} style={imgStyle} />
123
            </div>
124
            <div
125
              style={CloseButtonContainer}
126
              onClick={() => onDeleteFileHandler(id)}
127
            >
128
              <img
129
                src="/css/icons/x-circle-fill.svg"
130
                alt="close-button"
131
                style={{ width: "100%", height: "100%" }}
132
              />
133
            </div>
134
          </div>
135
        );
136
      case shareModalTypes.FILE:
137
        return (
3257 stevensc 138
          <div style={{
139
            ...thumbStyle,
140
            width: "90%",
141
            height: "auto",
142
            margin: 'auto',
143
            maxWidth: '90%'
144
          }}
1 www 145
            key={file.name}
146
          >
3257 stevensc 147
            <div style={fileInnerStyle}>
1 www 148
              <object
149
                data={URL.createObjectURL(file)}
150
                type="application/pdf"
3256 stevensc 151
              />
1 www 152
            </div>
153
            <div
154
              style={CloseButtonContainer}
155
              onClick={() => onDeleteFileHandler(id)}
156
            >
157
              <img
158
                src="/css/icons/x-circle-fill.svg"
159
                alt="close-button"
160
                style={{ width: "100%", height: "100%" }}
161
              />
162
            </div>
163
          </div>
164
        );
165
      case shareModalTypes.VIDEO:
166
        return (
167
          <div
3256 stevensc 168
            style={{ ...thumbStyle, width: "auto", height: "auto", maxWidth: '100%' }}
1 www 169
            key={file.name}
170
          >
171
            <div style={thumbInnerStyle}>
172
              <video
173
                src={URL.createObjectURL(file)}
174
                width="400"
175
                height="300"
176
                controls
177
                autoPlay
178
                muted
179
              ></video>
180
            </div>
181
            <div
182
              style={CloseButtonContainer}
183
              onClick={() => onDeleteFileHandler(id)}
184
            >
185
              <img
186
                src="/css/icons/x-circle-fill.svg"
187
                alt="close-button"
188
                style={{ width: "100%", height: "100%" }}
189
              />
190
            </div>
191
          </div>
192
        );
193
      case shareModalTypes.CHAT:
194
        switch (file.type) {
195
          case "video/mp4":
196
          case "video/mpeg":
197
          case "video/webm":
198
            return (
199
              <div
2662 stevensc 200
                style={{ ...thumbStyle, width: "90%", height: "auto", margin: 'auto' }}
1 www 201
                key={file.name}
202
              >
203
                <div style={thumbInnerStyle}>
204
                  <video
205
                    src={URL.createObjectURL(file)}
206
                    width="400"
207
                    height="300"
208
                    controls
209
                    autoPlay
210
                    muted
211
                  ></video>
212
                </div>
213
                <div
214
                  style={CloseButtonContainer}
215
                  onClick={() => onDeleteFileHandler(id)}
216
                >
217
                  <img
218
                    src="/css/icons/x-circle-fill.svg"
219
                    alt="close-button"
220
                    style={{ width: "100%", height: "100%" }}
221
                  />
222
                </div>
223
              </div>
224
            );
225
          case "image/jpeg":
226
          case "image/png":
227
          case "image/jpg":
228
            return (
229
              <div style={thumbStyle} key={file.name}>
230
                <div style={thumbInnerStyle}>
231
                  <img src={URL.createObjectURL(file)} style={imgStyle} />
232
                </div>
233
                <div
234
                  style={CloseButtonContainer}
235
                  onClick={() => onDeleteFileHandler(id)}
236
                >
237
                  <img
238
                    src="/css/icons/x-circle-fill.svg"
239
                    alt="close-button"
240
                    style={{ width: "100%", height: "100%" }}
241
                  />
242
                </div>
243
              </div>
244
            );
245
          case "application/pdf":
246
            return (
247
              <div
2662 stevensc 248
                style={{ ...thumbStyle, width: "90%", height: "auto", margin: 'auto' }}
1 www 249
                key={file.name}
250
              >
251
                <div style={thumbInnerStyle}>
3258 stevensc 252
                  <iframe
253
                    id="pdfviewer"
254
                    src={`http://docs.google.com/gview?embedded=true&url=${URL.createObjectURL(file)}`}
255
                    frameBorder="0"
256
                    width="100%"
257
                    height="200">
258
                  </iframe>
1 www 259
                </div>
260
                <div
261
                  style={CloseButtonContainer}
262
                  onClick={() => onDeleteFileHandler(id)}
263
                >
264
                  <img
265
                    src="/css/icons/x-circle-fill.svg"
266
                    alt="close-button"
267
                    style={{ width: "100%", height: "100%" }}
268
                  />
269
                </div>
270
              </div>
271
            );
272
          default:
273
            break;
274
        }
275
        break;
276
      default:
277
        break;
278
    }
279
  };
280
 
281
  const thumbsContainerStyle = {
282
    display: "flex",
283
    flexDirection: "row",
284
    flexWrap: "wrap",
285
    marginTop: 16,
286
    position: "relative",
287
    justifyContent: "center",
288
  };
289
 
290
  const baseStyle = {
291
    display: "flex",
292
    flexDirection: "column",
293
    alignItems: "center",
294
    padding: "2rem 0",
295
    borderWidth: 2,
296
    borderRadius: 2,
297
    borderColor: "#eeeeee",
298
    borderStyle: "dashed",
299
    backgroundColor: "#fafafa",
300
    color: "#bdbdbd",
301
    outline: "none",
302
    transition: "border .24s ease-in-out",
303
    marginTop: "1rem",
304
    cursor: "pointer",
305
  };
306
 
307
  return (
308
    <div>
1282 stevensc 309
      {
1283 stevensc 310
        !files.length
1282 stevensc 311
        &&
1 www 312
        <div {...getRootProps({ className: "dropzone", style: baseStyle })}>
313
          <input {...getInputProps()} />
314
          <p>Arrastra el archivo aqui, o haga click para seleccionar</p>
315
          {recomendationText}
316
        </div>
1282 stevensc 317
      }
1 www 318
      <aside>
319
        <div style={thumbsContainerStyle}>
1282 stevensc 320
          {
321
            files.map((file, id) => filePreviewTest(file, id))
322
          }
1 www 323
        </div>
324
        {errors.map((error, index) => (
325
          <FormErrorFeedback key={index}>{error}</FormErrorFeedback>
326
        ))}
327
      </aside>
328
    </div>
329
  );
3193 stevensc 330
};
1 www 331
 
3193 stevensc 332
export default React.memo(DropzoneComponent, areEqual);