Advertisement
Guest User

Untitled

a guest
Jun 30th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Fragment, useRef, useState } from 'react';
  2. import { Theme, createStyles, makeStyles } from '@material-ui/core/styles';
  3. import { useFetch } from '../hooks/useFetch';
  4. import { Helmet } from 'react-helmet';
  5. import Container from '@material-ui/core/Container';
  6. import Grid from '@material-ui/core/Grid';
  7. import Card from '@material-ui/core/Card';
  8. import CardActionArea from '@material-ui/core/CardActionArea';
  9. import CardContent from '@material-ui/core/CardContent';
  10. import CardMedia from '@material-ui/core/CardMedia';
  11. import Typography from '@material-ui/core/Typography';
  12. import { Link } from 'react-router-dom';
  13. import { useAuth } from '../providers/AuthProvider';
  14. import InsertPhoto from '@material-ui/icons/InsertPhoto';
  15. import Edit from '@material-ui/icons/Edit';
  16. import IconButton from '@material-ui/core/IconButton';
  17.  
  18. const useStyles = makeStyles((theme: Theme) =>
  19.   createStyles({
  20.     root: {
  21.       flexGrow: 1
  22.     },
  23.     card: {
  24.       maxWidth: 345
  25.     }
  26.   })
  27. );
  28.  
  29. const Products = () => {
  30.   const classes = useStyles();
  31.   const { data } = useFetch('/categories');
  32.   const { editMode } = useAuth();
  33.   const inputRef = useRef<HTMLInputElement>(null);
  34.  
  35.   const [thumbnail, setThumbnail] = useState({
  36.     id: null,
  37.     image: 'https://via.placeholder.com/200x100'
  38.   });
  39.  
  40.   const getImage = (id: number) =>
  41.     thumbnail.id === id
  42.       ? thumbnail.image
  43.       : 'https://via.placeholder.com/200x100';
  44.  
  45.   const handleClick = () => {
  46.     if (inputRef.current) inputRef.current.click();
  47.   };
  48.  
  49.   const handleChange = (e: React.ChangeEvent<HTMLInputElement>, id: number) => {
  50.     const errors = [];
  51.     const file = e.target.files![0];
  52.     const formData = new FormData();
  53.     const types = ['image/png', 'image/jpeg', 'image/gif'];
  54.     if (types.every(type => file.type !== type))
  55.       errors.push(`${file.type} wordt niet ondersteund`);
  56.     formData.append(file.name, file);
  57.  
  58.     fetch(`/category/${id}`, {
  59.       method: 'PUT',
  60.       body: formData
  61.     })
  62.       .then(res => res.json())
  63.       .then(response =>
  64.         setThumbnail({ id: response.id, image: response.thumbnail })
  65.       );
  66.   };
  67.  
  68.   return (
  69.     <>
  70.       <Helmet>
  71.         <title>Midl | Producten</title>
  72.       </Helmet>
  73.       <Container fixed>
  74.         <div className={classes.root}>
  75.           <Grid container spacing={1}>
  76.             {data &&
  77.               data.map(category => (
  78.                 <Grid item xs={3} key={category.id}>
  79.                   <Card className={classes.card}>
  80.                     <CardActionArea>
  81.                       <Link to={`/products/${category.id}`}>
  82.                         <CardMedia
  83.                           component="img"
  84.                           alt={category.name}
  85.                           height="140"
  86.                           image={category.thumbnail || getImage(category.id)}
  87.                           title={category.name}
  88.                         />
  89.                       </Link>
  90.                       <CardContent>
  91.                         <Typography gutterBottom variant="body2" component="p">
  92.                           {category.name}
  93.                         </Typography>
  94.                         {editMode && (
  95.                           <Fragment>
  96.                             <input
  97.                               accept="image/*"
  98.                               hidden
  99.                               type="file"
  100.                               ref={inputRef}
  101.                               onChange={e => handleChange(e, category.id)}
  102.                             />
  103.                             <IconButton onClick={handleClick}>
  104.                               <InsertPhoto />
  105.                             </IconButton>
  106.                             <IconButton>
  107.                               <Edit />
  108.                             </IconButton>
  109.                           </Fragment>
  110.                         )}
  111.                       </CardContent>
  112.                     </CardActionArea>
  113.                   </Card>
  114.                 </Grid>
  115.               ))}
  116.           </Grid>
  117.         </div>
  118.       </Container>
  119.     </>
  120.   );
  121. };
  122.  
  123. export default Products;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement