Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cardComponenent): adds a large cardComponent for single object view #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const Article = ({ id }) => {
const articles = useArticlesSelector();
const article = articles.find(item => item.slug === id);

return article ? <ArticleCard article={article} /> : null;
return article ? <ArticleCard article={article} size="L" /> : null;
};

Article.propTypes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import Card from '@material-ui/core/Card';
import CardActions from '@material-ui/core/CardActions';
import CardContent from '@material-ui/core/CardContent';
import CardMedia from '@material-ui/core/CardMedia';
import Grid from '@material-ui/core/Grid';
import Typography from '@material-ui/core/Typography';

import { makeStyles } from '@material-ui/core/styles';
import { addToCart } from '../../cart/cart.actions';
import { useCart } from '../../cart/cart.context';

import { CardWrapper } from './cardWrapper.component';

const useStyles = makeStyles({
card: {
height: '100%',
Expand All @@ -28,39 +29,56 @@ const useStyles = makeStyles({
},
});

export function ArticleCard({ article }) {
const { name, year, image, slug } = article;
const cardElementsBySize = {
S: {
hasViewButton: true,
hasDescription: false,
},
L: {
hasViewButton: false,
hasDescription: true,
},
};

export function ArticleCard({ article, size }) {
const { name, year, image, slug, description } = article;
const { hasViewButton, hasDescription } = cardElementsBySize[size];
const Wrapper = CardWrapper[size];

const classes = useStyles();
const [, dispatch] = useCart();

const dispatchAddToCart = () => dispatch(addToCart(article));

return (
<Grid item xs={12} sm={6} md={4}>
<Wrapper>
<Card className={classes.card}>
<CardMedia className={classes.cardMedia} image={image} title={name} />
<CardContent className={classes.cardContent}>
<Typography gutterBottom variant="h5" component="h2">
{name}
</Typography>
<Typography>{year}</Typography>
{hasDescription && <Typography>{description}</Typography>}
</CardContent>
<CardActions>
<Button onClick={dispatchAddToCart} size="small" color="secondary" variant="outlined">
Add to Cart
</Button>
<Button
size="small"
component={Link}
to={`/articles/${slug}`}
color="primary"
variant="outlined"
>
View
</Button>
{hasViewButton && (
<Button
size="small"
component={Link}
to={`/articles/${slug}`}
color="primary"
variant="outlined"
>
View
</Button>
)}
</CardActions>
</Card>
</Grid>
</Wrapper>
);
}

Expand All @@ -70,6 +88,8 @@ ArticleCard.propTypes = {
year: PropTypes.string.isRequired,
id: PropTypes.string.isRequired,
image: PropTypes.string.isRequired,
slug: PropTypes.string.isRequired,
slug: PropTypes.string,
description: PropTypes.string,
}).isRequired,
size: PropTypes.string.isRequired,
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function ArticlesList() {
return (
<Grid container spacing={4}>
{articles.map(article => (
<ArticleCard key={article.id} article={article} />
<ArticleCard key={article.id} article={article} size="S" />
))}
</Grid>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import PropTypes from 'prop-types';

import Grid from '@material-ui/core/Grid';

const LargeCardWrapper = ({ children }) => (
<Grid item xs={12}>
{children}
</Grid>
);

LargeCardWrapper.propTypes = {
children: PropTypes.element.isRequired,
};

const SmallCardWrapper = ({ children }) => (
<Grid item xs={12} sm={6} md={4}>
{children}
</Grid>
);

SmallCardWrapper.propTypes = {
children: PropTypes.element.isRequired,
};

export const CardWrapper = {
S: SmallCardWrapper,
L: LargeCardWrapper,
};