Rev 2529 | Rev 2531 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useState, useCallback, useEffect } from "react";import { useDropzone } from "react-dropzone";import ShareModal from "../../dashboard/components/share-modal/ShareModal";import { shareModalTypes } from "../../redux/share-modal/shareModal.types";const areEqual = (prevProps, nextProps) => {return prevProps.settedFile === nextProps.settedFile ? true : false;};const DropzoneComponent = React.memo((props) => {const { modalType, onUploaded, settedFile, recomendationText } = props;const [errors, setErrors] = useState([]);const [files, setFiles] = useState([]);const onDropRejected = useCallback((rejectedFiles) => {rejectedFiles.map((fileRejection) => {switch (fileRejection.errors[0].code) {case "too-many-files":setErrors([...errors, "solo puedes agregar 1 archivo"]);break;case "file-invalid-type":setErrors([...errors, "por favor seleccione un archivo valido"]);break;default:setErrors(errors);break;}});}, []);useEffect(() => {if (settedFile) setFiles([settedFile]);}, [settedFile]);const acceptedMimeTypes = () => {switch (modalType) {case shareModalTypes.IMAGE:return "image/jpeg, image/png, image/jpg";case shareModalTypes.FILE:return "application/pdf";case shareModalTypes.VIDEO:return "video/mp4, video/mpeg, video/webm";case shareModalTypes.CHAT:return "video/mp4, video/mpeg, video/webm, application/pdf, image/jpeg, image/png, image/jpg";default:return null;}};const {getRootProps,getInputProps,acceptedFiles,fileRejections,} = useDropzone({accept: acceptedMimeTypes(),multiple: false,validator: (file) => {// You can access width/height propertiesif (file.width > 720) {return {code: "Tamaño de imagen superior al sugerido",message: `Ancho de imagen superior a los 720px`,}}return null},onDrop: (acceptedFiles) => {onUploaded(acceptedFiles[0]);setFiles(acceptedFiles.map((file) => file));},onDropRejected,onDropAccepted: () => {setErrors([]);},maxFiles: 1,});const thumbStyle = {display: "inline-flex",borderRadius: "2px",border: "1px solid #eaeaea",marginBottom: "8px",marginRight: "8px",width: "150px",height: "150px",padding: "4px",boxSizing: "border-box",position: "relative",zIndex: "100",};const thumbInnerStyle = {display: "flex",minWidth: 0,overflow: "hidden",marginRight: "1.5rem",};const imgStyle = {display: "block",width: "auto",height: "100%",objectFit: "contain",};const onDeleteFileHandler = (index) => {const newFiles = files.filter((_, id) => id !== index);onUploaded("");setFiles(newFiles);};const CloseButtonContainer = {width: "20px",height: "20px",position: "absolute",top: "5px",right: "0px",cursor: "pointer",zIndex: "200",};const filePreviewTest = (file, id) => {console.log(file)console.log(id)console.log(shareModalTypes)switch (modalType) {case shareModalTypes.IMAGE:return (<div style={thumbStyle} key={file.name}><div style={thumbInnerStyle}><img src={URL.createObjectURL(file)} style={imgStyle} /></div><divstyle={CloseButtonContainer}onClick={() => onDeleteFileHandler(id)}><imgsrc="/css/icons/x-circle-fill.svg"alt="close-button"style={{ width: "100%", height: "100%" }}/></div></div>);case shareModalTypes.FILE:return (<divstyle={{ ...thumbStyle, width: "auto", height: "auto" }}key={file.name}><div style={thumbInnerStyle}><objectdata={URL.createObjectURL(file)}type="application/pdf"width="400"height="200"></object></div><divstyle={CloseButtonContainer}onClick={() => onDeleteFileHandler(id)}><imgsrc="/css/icons/x-circle-fill.svg"alt="close-button"style={{ width: "100%", height: "100%" }}/></div></div>);case shareModalTypes.VIDEO:return (<divstyle={{ ...thumbStyle, width: "auto", height: "auto" }}key={file.name}><div style={thumbInnerStyle}><videosrc={URL.createObjectURL(file)}width="400"height="300"controlsautoPlaymuted></video></div><divstyle={CloseButtonContainer}onClick={() => onDeleteFileHandler(id)}><imgsrc="/css/icons/x-circle-fill.svg"alt="close-button"style={{ width: "100%", height: "100%" }}/></div></div>);case shareModalTypes.CHAT:switch (file.type) {case "video/mp4":case "video/mpeg":case "video/webm":return (<divstyle={{ ...thumbStyle, width: "auto", height: "auto" }}key={file.name}><div style={thumbInnerStyle}><videosrc={URL.createObjectURL(file)}width="400"height="300"controlsautoPlaymuted></video></div><divstyle={CloseButtonContainer}onClick={() => onDeleteFileHandler(id)}><imgsrc="/css/icons/x-circle-fill.svg"alt="close-button"style={{ width: "100%", height: "100%" }}/></div></div>);case "image/jpeg":case "image/png":case "image/jpg":return (<div style={thumbStyle} key={file.name}><div style={thumbInnerStyle}><img src={URL.createObjectURL(file)} style={imgStyle} /></div><divstyle={CloseButtonContainer}onClick={() => onDeleteFileHandler(id)}><imgsrc="/css/icons/x-circle-fill.svg"alt="close-button"style={{ width: "100%", height: "100%" }}/></div></div>);case "application/pdf":return (<divstyle={{ ...thumbStyle, width: "auto", height: "auto" }}key={file.name}><div style={thumbInnerStyle}><objectdata={URL.createObjectURL(file)}type="application/pdf"width="400"height="200"></object></div><divstyle={CloseButtonContainer}onClick={() => onDeleteFileHandler(id)}><imgsrc="/css/icons/x-circle-fill.svg"alt="close-button"style={{ width: "100%", height: "100%" }}/></div></div>);default:break;}break;default:break;}};const thumbsContainerStyle = {display: "flex",flexDirection: "row",flexWrap: "wrap",marginTop: 16,position: "relative",justifyContent: "center",};const baseStyle = {display: "flex",flexDirection: "column",alignItems: "center",padding: "2rem 0",borderWidth: 2,borderRadius: 2,borderColor: "#eeeeee",borderStyle: "dashed",backgroundColor: "#fafafa",color: "#bdbdbd",outline: "none",transition: "border .24s ease-in-out",marginTop: "1rem",cursor: "pointer",};return (<div>{!files.length&&<div {...getRootProps({ className: "dropzone", style: baseStyle })}><input {...getInputProps()} /><p>Arrastra el archivo aqui, o haga click para seleccionar</p>{recomendationText}</div>}<aside><div style={thumbsContainerStyle}>{files.map((file, id) => filePreviewTest(file, id))}</div>{errors.map((error, index) => (<FormErrorFeedback key={index}>{error}</FormErrorFeedback>))}</aside></div>);}, areEqual);export default DropzoneComponent;