Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 2662 | Rev 3256 | 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
  };
88
  const imgStyle = {
89
    display: "block",
90
    width: "auto",
91
    height: "100%",
92
    objectFit: "contain",
93
  };
94
 
95
  const onDeleteFileHandler = (index) => {
96
    const newFiles = files.filter((_, id) => id !== index);
97
    onUploaded("");
98
    setFiles(newFiles);
99
  };
100
 
101
  const CloseButtonContainer = {
102
    width: "20px",
103
    height: "20px",
104
    position: "absolute",
105
    top: "5px",
106
    right: "0px",
107
    cursor: "pointer",
108
    zIndex: "200",
109
  };
110
 
111
  const filePreviewTest = (file, id) => {
1282 stevensc 112
    console.log(file)
113
    console.log(id)
114
    console.log(shareModalTypes)
1 www 115
    switch (modalType) {
116
      case shareModalTypes.IMAGE:
117
        return (
118
          <div style={thumbStyle} key={file.name}>
119
            <div style={thumbInnerStyle}>
120
              <img src={URL.createObjectURL(file)} style={imgStyle} />
121
            </div>
122
            <div
123
              style={CloseButtonContainer}
124
              onClick={() => onDeleteFileHandler(id)}
125
            >
126
              <img
127
                src="/css/icons/x-circle-fill.svg"
128
                alt="close-button"
129
                style={{ width: "100%", height: "100%" }}
130
              />
131
            </div>
132
          </div>
133
        );
134
      case shareModalTypes.FILE:
135
        return (
136
          <div
2661 stevensc 137
            style={{ ...thumbStyle, width: "90%", height: "auto", margin: 'auto' }}
1 www 138
            key={file.name}
139
          >
140
            <div style={thumbInnerStyle}>
141
              <object
142
                data={URL.createObjectURL(file)}
143
                type="application/pdf"
144
                width="400"
145
                height="200"
146
              ></object>
147
            </div>
148
            <div
149
              style={CloseButtonContainer}
150
              onClick={() => onDeleteFileHandler(id)}
151
            >
152
              <img
153
                src="/css/icons/x-circle-fill.svg"
154
                alt="close-button"
155
                style={{ width: "100%", height: "100%" }}
156
              />
157
            </div>
158
          </div>
159
        );
160
      case shareModalTypes.VIDEO:
161
        return (
162
          <div
163
            style={{ ...thumbStyle, width: "auto", height: "auto" }}
164
            key={file.name}
165
          >
166
            <div style={thumbInnerStyle}>
167
              <video
168
                src={URL.createObjectURL(file)}
169
                width="400"
170
                height="300"
171
                controls
172
                autoPlay
173
                muted
174
              ></video>
175
            </div>
176
            <div
177
              style={CloseButtonContainer}
178
              onClick={() => onDeleteFileHandler(id)}
179
            >
180
              <img
181
                src="/css/icons/x-circle-fill.svg"
182
                alt="close-button"
183
                style={{ width: "100%", height: "100%" }}
184
              />
185
            </div>
186
          </div>
187
        );
188
      case shareModalTypes.CHAT:
189
        switch (file.type) {
190
          case "video/mp4":
191
          case "video/mpeg":
192
          case "video/webm":
193
            return (
194
              <div
2662 stevensc 195
                style={{ ...thumbStyle, width: "90%", height: "auto", margin: 'auto' }}
1 www 196
                key={file.name}
197
              >
198
                <div style={thumbInnerStyle}>
199
                  <video
200
                    src={URL.createObjectURL(file)}
201
                    width="400"
202
                    height="300"
203
                    controls
204
                    autoPlay
205
                    muted
206
                  ></video>
207
                </div>
208
                <div
209
                  style={CloseButtonContainer}
210
                  onClick={() => onDeleteFileHandler(id)}
211
                >
212
                  <img
213
                    src="/css/icons/x-circle-fill.svg"
214
                    alt="close-button"
215
                    style={{ width: "100%", height: "100%" }}
216
                  />
217
                </div>
218
              </div>
219
            );
220
          case "image/jpeg":
221
          case "image/png":
222
          case "image/jpg":
223
            return (
224
              <div style={thumbStyle} key={file.name}>
225
                <div style={thumbInnerStyle}>
226
                  <img src={URL.createObjectURL(file)} style={imgStyle} />
227
                </div>
228
                <div
229
                  style={CloseButtonContainer}
230
                  onClick={() => onDeleteFileHandler(id)}
231
                >
232
                  <img
233
                    src="/css/icons/x-circle-fill.svg"
234
                    alt="close-button"
235
                    style={{ width: "100%", height: "100%" }}
236
                  />
237
                </div>
238
              </div>
239
            );
240
          case "application/pdf":
241
            return (
242
              <div
2662 stevensc 243
                style={{ ...thumbStyle, width: "90%", height: "auto", margin: 'auto' }}
1 www 244
                key={file.name}
245
              >
246
                <div style={thumbInnerStyle}>
247
                  <object
248
                    data={URL.createObjectURL(file)}
249
                    type="application/pdf"
250
                    width="400"
251
                    height="200"
252
                  ></object>
253
                </div>
254
                <div
255
                  style={CloseButtonContainer}
256
                  onClick={() => onDeleteFileHandler(id)}
257
                >
258
                  <img
259
                    src="/css/icons/x-circle-fill.svg"
260
                    alt="close-button"
261
                    style={{ width: "100%", height: "100%" }}
262
                  />
263
                </div>
264
              </div>
265
            );
266
          default:
267
            break;
268
        }
269
        break;
270
      default:
271
        break;
272
    }
273
  };
274
 
275
  const thumbsContainerStyle = {
276
    display: "flex",
277
    flexDirection: "row",
278
    flexWrap: "wrap",
279
    marginTop: 16,
280
    position: "relative",
281
    justifyContent: "center",
282
  };
283
 
284
  const baseStyle = {
285
    display: "flex",
286
    flexDirection: "column",
287
    alignItems: "center",
288
    padding: "2rem 0",
289
    borderWidth: 2,
290
    borderRadius: 2,
291
    borderColor: "#eeeeee",
292
    borderStyle: "dashed",
293
    backgroundColor: "#fafafa",
294
    color: "#bdbdbd",
295
    outline: "none",
296
    transition: "border .24s ease-in-out",
297
    marginTop: "1rem",
298
    cursor: "pointer",
299
  };
300
 
301
  return (
302
    <div>
1282 stevensc 303
      {
1283 stevensc 304
        !files.length
1282 stevensc 305
        &&
1 www 306
        <div {...getRootProps({ className: "dropzone", style: baseStyle })}>
307
          <input {...getInputProps()} />
308
          <p>Arrastra el archivo aqui, o haga click para seleccionar</p>
309
          {recomendationText}
310
        </div>
1282 stevensc 311
      }
1 www 312
      <aside>
313
        <div style={thumbsContainerStyle}>
1282 stevensc 314
          {
315
            files.map((file, id) => filePreviewTest(file, id))
316
          }
1 www 317
        </div>
318
        {errors.map((error, index) => (
319
          <FormErrorFeedback key={index}>{error}</FormErrorFeedback>
320
        ))}
321
      </aside>
322
    </div>
323
  );
3193 stevensc 324
};
1 www 325
 
3193 stevensc 326
export default React.memo(DropzoneComponent, areEqual);