Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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