Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. interface ValidateFormatInterface{
  4.     file?: string;
  5.     enableFormats?: string[];
  6. }
  7.  
  8. export const  ValidateFormat = ( { file, enableFormats } : ValidateFormatInterface ) => {
  9.    
  10.     let validFormat = true;
  11.     let error = false;
  12.     let description;
  13.  
  14.     if(!Array.isArray(enableFormats)){
  15.         error = true
  16.         description = "The second Argument passed must be  an array"
  17.        
  18.     }else{
  19.         enableFormats.map(  format =>{
  20.             if ( format !=  file.split('.')[1]){
  21.                  validFormat = false;
  22.                  description = "invalid format to load"
  23.             }
  24.        } )
  25.     }
  26.  
  27.     return {
  28.          validFormat,
  29.          error,
  30.          description
  31.     };
  32. }
  33.  
  34.  
  35. ==========================================
  36.  
  37.  
  38. import React, { useState } from 'react'
  39. import { ValidateFormat } from '../utils/Utils'
  40. import PropTypes  from 'prop-types';
  41.  
  42. interface ImageInterface {
  43.     image:string;
  44.     show:boolean;
  45. }
  46.  
  47. const HubImage = ( props:ImageInterface ) => {
  48.  
  49.  
  50.     const [ image, setImage] = useState<string>(props.image);
  51.     const [ showSpinner,  setShowSpinner ] = useState(false)
  52.  
  53.    
  54.  
  55.     const  encodeImageFileAsURL = (element : any) => {
  56.        
  57.         var file = element.files[0];
  58.        
  59.         let format : string[] =  ['jpeg', 'jpg', 'png', 'gif', 'bmp']
  60.         let validFormat = ValidateFormat(file.name, format  ) // linea del error de tipo
  61.         console.log(validFormat)
  62.        
  63.         var reader : FileReader = new FileReader();
  64.         reader.onloadend =  () => {
  65.             if (reader.readyState == 2) {
  66.                 setShowSpinner(false)
  67.                 let result: any = reader.result
  68.                 setImage(result);
  69.             }
  70.         }
  71.  
  72.         if (reader.readyState == 0)  setShowSpinner(true)
  73.         reader.readAsDataURL(file);
  74.        
  75.     }
  76.  
  77.     const uploadImage =  ( e:  React.ChangeEvent<HTMLInputElement> ): any =>  encodeImageFileAsURL(e.target);    
  78.    
  79.     return(
  80.         <div>
  81.             {
  82.                 showSpinner ? <img src="/static/assets/oval.svg"/> : ''
  83.             }
  84.             <img src={image}/>
  85.             <input type="file"  name="file" onChange={uploadImage}  />
  86.         </div>
  87.     )
  88. }
  89.  
  90.  
  91. HubImage.defaultProps = {
  92.     image:'https://via.placeholder.com/200x150?text=Load Image',  
  93.     show:false
  94. }
  95.  
  96. HubImage.propTypes = {
  97.     images : PropTypes.string,
  98.     show:PropTypes.bool
  99. }
  100.  
  101. export default HubImage;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement