Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3258 | Rev 3260 | 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}>
3259 stevensc 148
              <iframe
149
                id="pdfviewer"
150
                src={`http://docs.google.com/gview?embedded=true&url=${URL.createObjectURL(file)}`}
151
                frameBorder="0"
152
                width="100%"
153
                height="200">
154
              </iframe>
1 www 155
            </div>
156
            <div
157
              style={CloseButtonContainer}
158
              onClick={() => onDeleteFileHandler(id)}
159
            >
160
              <img
161
                src="/css/icons/x-circle-fill.svg"
162
                alt="close-button"
163
                style={{ width: "100%", height: "100%" }}
164
              />
165
            </div>
166
          </div>
167
        );
168
      case shareModalTypes.VIDEO:
169
        return (
170
          <div
3256 stevensc 171
            style={{ ...thumbStyle, width: "auto", height: "auto", maxWidth: '100%' }}
1 www 172
            key={file.name}
173
          >
174
            <div style={thumbInnerStyle}>
175
              <video
176
                src={URL.createObjectURL(file)}
177
                width="400"
178
                height="300"
179
                controls
180
                autoPlay
181
                muted
182
              ></video>
183
            </div>
184
            <div
185
              style={CloseButtonContainer}
186
              onClick={() => onDeleteFileHandler(id)}
187
            >
188
              <img
189
                src="/css/icons/x-circle-fill.svg"
190
                alt="close-button"
191
                style={{ width: "100%", height: "100%" }}
192
              />
193
            </div>
194
          </div>
195
        );
196
      case shareModalTypes.CHAT:
197
        switch (file.type) {
198
          case "video/mp4":
199
          case "video/mpeg":
200
          case "video/webm":
201
            return (
202
              <div
2662 stevensc 203
                style={{ ...thumbStyle, width: "90%", height: "auto", margin: 'auto' }}
1 www 204
                key={file.name}
205
              >
206
                <div style={thumbInnerStyle}>
207
                  <video
208
                    src={URL.createObjectURL(file)}
209
                    width="400"
210
                    height="300"
211
                    controls
212
                    autoPlay
213
                    muted
214
                  ></video>
215
                </div>
216
                <div
217
                  style={CloseButtonContainer}
218
                  onClick={() => onDeleteFileHandler(id)}
219
                >
220
                  <img
221
                    src="/css/icons/x-circle-fill.svg"
222
                    alt="close-button"
223
                    style={{ width: "100%", height: "100%" }}
224
                  />
225
                </div>
226
              </div>
227
            );
228
          case "image/jpeg":
229
          case "image/png":
230
          case "image/jpg":
231
            return (
232
              <div style={thumbStyle} key={file.name}>
233
                <div style={thumbInnerStyle}>
234
                  <img src={URL.createObjectURL(file)} style={imgStyle} />
235
                </div>
236
                <div
237
                  style={CloseButtonContainer}
238
                  onClick={() => onDeleteFileHandler(id)}
239
                >
240
                  <img
241
                    src="/css/icons/x-circle-fill.svg"
242
                    alt="close-button"
243
                    style={{ width: "100%", height: "100%" }}
244
                  />
245
                </div>
246
              </div>
247
            );
248
          case "application/pdf":
249
            return (
250
              <div
2662 stevensc 251
                style={{ ...thumbStyle, width: "90%", height: "auto", margin: 'auto' }}
1 www 252
                key={file.name}
253
              >
254
                <div style={thumbInnerStyle}>
3259 stevensc 255
                  <object
256
                    data={URL.createObjectURL(file)}
257
                    type="application/pdf"
258
                    width="400"
259
                    height="200"
260
                  ></object>
1 www 261
                </div>
262
                <div
263
                  style={CloseButtonContainer}
264
                  onClick={() => onDeleteFileHandler(id)}
265
                >
266
                  <img
267
                    src="/css/icons/x-circle-fill.svg"
268
                    alt="close-button"
269
                    style={{ width: "100%", height: "100%" }}
270
                  />
271
                </div>
272
              </div>
273
            );
274
          default:
275
            break;
276
        }
277
        break;
278
      default:
279
        break;
280
    }
281
  };
282
 
283
  const thumbsContainerStyle = {
284
    display: "flex",
285
    flexDirection: "row",
286
    flexWrap: "wrap",
287
    marginTop: 16,
288
    position: "relative",
289
    justifyContent: "center",
290
  };
291
 
292
  const baseStyle = {
293
    display: "flex",
294
    flexDirection: "column",
295
    alignItems: "center",
296
    padding: "2rem 0",
297
    borderWidth: 2,
298
    borderRadius: 2,
299
    borderColor: "#eeeeee",
300
    borderStyle: "dashed",
301
    backgroundColor: "#fafafa",
302
    color: "#bdbdbd",
303
    outline: "none",
304
    transition: "border .24s ease-in-out",
305
    marginTop: "1rem",
306
    cursor: "pointer",
307
  };
308
 
309
  return (
310
    <div>
1282 stevensc 311
      {
1283 stevensc 312
        !files.length
1282 stevensc 313
        &&
1 www 314
        <div {...getRootProps({ className: "dropzone", style: baseStyle })}>
315
          <input {...getInputProps()} />
316
          <p>Arrastra el archivo aqui, o haga click para seleccionar</p>
317
          {recomendationText}
318
        </div>
1282 stevensc 319
      }
1 www 320
      <aside>
321
        <div style={thumbsContainerStyle}>
1282 stevensc 322
          {
323
            files.map((file, id) => filePreviewTest(file, id))
324
          }
1 www 325
        </div>
326
        {errors.map((error, index) => (
327
          <FormErrorFeedback key={index}>{error}</FormErrorFeedback>
328
        ))}
329
      </aside>
330
    </div>
331
  );
3193 stevensc 332
};
1 www 333
 
3193 stevensc 334
export default React.memo(DropzoneComponent, areEqual);