-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCartAction.js
31 lines (28 loc) · 890 Bytes
/
CartAction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import React from 'react';
import styled from '@emotion/styled';
const Button = styled.button(props => ({
display: 'block',
width: '100%',
backgroundColor: props.isAddToCart ? '#196ec5' : '#c51939',
color: 'white',
padding: 10,
cursor: 'pointer',
justifySelf: 'flex-end',
'&:hover': {
backgroundColor: props.isAddToCart ? '#3c92ea' : '#ea3c54',
},
'&:active': {
backgroundColor: props.isAddToCart ? '#215488' : '#882121',
},
}));
export default ({ cart, product }) => {
const isInCart = cart.some(({ id }) => id === product.id);
const action = isInCart ? 'Remove from cart' : 'Add to cart';
return (
<form action="/update-cart" method="post">
<input type="hidden" name="id" value={product.id} />
<input type="hidden" name="delta" value={isInCart ? -1 : 1} />
<Button isAddToCart={!isInCart}>{action}</Button>
</form>
);
};