summaryrefslogtreecommitdiffstats
path: root/frontend/components/Item.js
blob: 42a6c8735299bd3ab5e1a85b5a4f7201b5de8bac (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import React from 'react';
import styled from 'styled-components';
import slugify from 'slugify';
import { compose } from 'react-apollo';
import { Link } from '../routes';
import AddToCart from './AddToCart';
import makeImage from '../lib/image';
import formatMoney from '../lib/formatMoney';
import { removeItemMutation } from '../enhancers/enhancers';

const Item = styled.div`
  background: #f3f3f3;
  padding: 5px;
  img {
    width: 100%;
  }
`;

class ItemComponent extends React.Component {
  removeItem = () => {
    this.props.removeItem({ variables: { id: this.props.item.id } });
  };
  render() {
    const item = this.props.item;
    return (
      <Item key={item.id}>
        {item.image ? <img src={item.image} alt={item.title} /> : null}
        <h3>
          <Link
            route="item"
            params={{
              slug: slugify(item.title),
              itemId: item.id,
            }}
          >
            <a>{item.title}</a>
          </Link>
        </h3>

        <p>{item.description}</p>

        <Link
          href={{
            pathname: '/admin/update',
            query: { id: item.id },
          }}
        >
          <a>Edit ✏️</a>
        </Link>
        <AddToCart id={item.id} />
        <button onClick={this.removeItem}>&times; Delete item</button>
      </Item>
    );
  }
}

export default compose(removeItemMutation)(ItemComponent);