summaryrefslogtreecommitdiffstats
path: root/frontend/components/SingleItem.js
blob: b709ff1caa7b21bd94ebe7adc4b66a80e136d924 (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
import { Query } from 'react-apollo';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import Link from 'next/link';
import { SINGLE_ITEM_QUERY } from '../queries/queries.graphql';
import Error from './ErrorMessage';

const SingleItemStyles = styled.div`
  max-width: 1200px;
  margin: 2rem auto;
  box-shadow: ${props => props.theme.bs};
  img {
    width: 100%;
    object-fit: cover;
  }
  .details {
    margin: 3rem;
    font-size: 3rem;
  }
`;

const SingleItem = props => (
  <Query query={SINGLE_ITEM_QUERY} variables={{ id: props.id }}>
    {({ data, loading, error }) => {
      if (loading) return <p>Loading...</p>;
      if (error) return <Error error={error} />;
      const [item] = data.items;
      return (
        <SingleItemStyles data-test="SingleItem">
          <img src={item.largeImage || item.image} alt={item.title} />
          <div className="details">
            <h2>Viewing {item.title}</h2>
            <p>{item.description}</p>
            <Link
              href={{
                pathname: '/update',
                query: { id: item.id },
              }}
            >
              <a>Edit ✏️</a>
            </Link>
          </div>
        </SingleItemStyles>
      );
    }}
  </Query>
);

SingleItem.propTypes = {
  id: PropTypes.string.isRequired,
};

export default SingleItem;