|
| 1 | +import { CardActionArea } from '@material-ui/core'; |
| 2 | +import Card from '@material-ui/core/Card'; |
| 3 | +import CardActions from '@material-ui/core/CardActions'; |
| 4 | +import CardContent from '@material-ui/core/CardContent'; |
| 5 | +import CardMedia from '@material-ui/core/CardMedia'; |
| 6 | +import Collapse from '@material-ui/core/Collapse'; |
| 7 | +import IconButton from '@material-ui/core/IconButton'; |
| 8 | +import { makeStyles } from '@material-ui/core/styles'; |
| 9 | +import Typography from '@material-ui/core/Typography'; |
| 10 | +import ExpandMoreIcon from '@material-ui/icons/ExpandMore'; |
| 11 | +import clsx from 'clsx'; |
| 12 | +import NextLink from 'next/link'; |
| 13 | +import React from 'react'; |
| 14 | + |
| 15 | +import Book from '../models/BookModel'; |
| 16 | + |
| 17 | +const useStyles = makeStyles(theme => ({ |
| 18 | + card: { |
| 19 | + width: 400, |
| 20 | + margin: 5, |
| 21 | + display: 'flex', |
| 22 | + flexDirection: 'column' |
| 23 | + }, |
| 24 | + cardContent: { |
| 25 | + display: 'flex', |
| 26 | + flex: '1 0 auto', |
| 27 | + flexDirection: 'column', |
| 28 | + width: '375px', |
| 29 | + whiteSpace: 'nowrap', |
| 30 | + overflow: 'hidden', |
| 31 | + textOverflow: 'ellipsis', |
| 32 | + '& h1': { |
| 33 | + fontSize: '1.4rem', |
| 34 | + textTransform: 'uppercase' |
| 35 | + } |
| 36 | + }, |
| 37 | + cardMedia: { |
| 38 | + height: 0, |
| 39 | + paddingTop: '65%', |
| 40 | + backgroundRepeat: 'no-repeat', |
| 41 | + backgroundSize: 'auto', |
| 42 | + cursor: 'pointer' |
| 43 | + }, |
| 44 | + cardDescription: { |
| 45 | + width: 368, |
| 46 | + height: 190, |
| 47 | + overflow: 'auto', |
| 48 | + whiteSpace: 'normal' |
| 49 | + }, |
| 50 | + cardActions: { |
| 51 | + display: 'flex', |
| 52 | + alignItems: 'center', |
| 53 | + justifyContent: 'space-between' |
| 54 | + }, |
| 55 | + expand: { |
| 56 | + transform: 'rotate(0deg)', |
| 57 | + marginLeft: 'auto', |
| 58 | + transition: theme.transitions.create('transform', { |
| 59 | + duration: theme.transitions.duration.shortest |
| 60 | + }) |
| 61 | + }, |
| 62 | + expandOpen: { |
| 63 | + transform: 'rotate(180deg)' |
| 64 | + } |
| 65 | +})); |
| 66 | + |
| 67 | +const BookCard = ({ book }: { book: Book }) => { |
| 68 | + const classes = useStyles(); |
| 69 | + const [expanded, setExpanded] = React.useState(false); |
| 70 | + const handleExpandClick = () => { |
| 71 | + setExpanded(!expanded); |
| 72 | + }; |
| 73 | + return ( |
| 74 | + <Card className={classes.card}> |
| 75 | + {/* |
| 76 | + Show this as an example of how we would use SSR |
| 77 | + <NextLink href={`/book?id=${book.id}`} as={`/book/${book.slug}`}> |
| 78 | + */} |
| 79 | + <NextLink href={`/book?id=${book.id}`}> |
| 80 | + <CardActionArea> |
| 81 | + <CardMedia |
| 82 | + className={classes.cardMedia} |
| 83 | + image={book.cover || '/static/images/cards/book.png'} |
| 84 | + title={book.title} |
| 85 | + /> |
| 86 | + <CardContent className={classes.cardContent}> |
| 87 | + <Typography component="h1">{book.title}</Typography> |
| 88 | + <Typography component="p"> |
| 89 | + Author: |
| 90 | + {` ${book.authorDisplayName}`} |
| 91 | + </Typography> |
| 92 | + </CardContent> |
| 93 | + </CardActionArea> |
| 94 | + </NextLink> |
| 95 | + <CardActions className={classes.cardActions}> |
| 96 | + Summary: |
| 97 | + <IconButton |
| 98 | + className={clsx(classes.expand, { |
| 99 | + [classes.expandOpen]: expanded |
| 100 | + })} |
| 101 | + onClick={handleExpandClick} |
| 102 | + aria-expanded={expanded} |
| 103 | + aria-label="Show more" |
| 104 | + > |
| 105 | + <ExpandMoreIcon /> |
| 106 | + </IconButton>{' '} |
| 107 | + </CardActions> |
| 108 | + <Collapse in={expanded} timeout="auto" unmountOnExit> |
| 109 | + <CardContent className={classes.cardContent}> |
| 110 | + <Typography paragraph className={classes.cardDescription}> |
| 111 | + {book.description} |
| 112 | + </Typography> |
| 113 | + </CardContent> |
| 114 | + </Collapse> |
| 115 | + </Card> |
| 116 | + ); |
| 117 | +}; |
| 118 | + |
| 119 | +export default BookCard; |
0 commit comments