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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
import React, { Component } from 'react';
import { graphql, gql } from 'react-apollo';
import { CREATE_ITEM_MUTATION } from '../queries';
import ErrorMessage from './ErrorMessage';
import Form from './styles/Form';
import PropTypes from 'prop-types';
class CreateItem extends Component {
static propTypes = {
createItemMutation: PropTypes.func.isRequired,
};
state = {
item: {
title: '',
description: '',
image: '',
largeImage: '',
price: 0,
},
loading: false,
error: {
message: null,
},
};
handleChange = e => {
const { name, value, type } = e.target;
const updatedItem = {
...this.state.item,
[name]: type === 'number' ? parseFloat(value) : value,
};
this.setState({ item: updatedItem });
};
uploadFile = async e => {
this.setState({ loading: true });
const files = e.currentTarget.files;
const data = new FormData();
data.append('file', files[0]);
data.append('upload_preset', 'sickfits');
// use the file endpoint
const res = await fetch('https://api.cloudinary.com/v1_1/wesbos/image/upload', {
method: 'POST',
body: data,
});
const file = await res.json();
this.setState({ image: file.secure_url, largeImage: file.eager[0].secure_url, loading: false });
};
createItem = async e => {
e.preventDefault();
// TODO: handle any errors
// turn loading on
this.setState({ loading: true });
try {
const res = await this.props.createItemMutation({
// pass in those variables from state
variables: {
...this.state.item,
},
});
} catch (error) {
this.setState({ error });
}
this.setState({ loading: false });
};
render() {
return (
<div>
<Form onSubmit={this.createItem}>
{this.state.loading ? 'LOADING...' : null}
<ErrorMessage error={this.state.error} onButtonClick={() => this.setState({ error: {} })} />
<p>
Image
<input onChange={this.uploadFile} type="file" accept=".png, .jpg, .jpeg" />
{this.state.image ? <img src={this.state.image} width="100" alt={this.state.title} /> : null}
</p>
<p>
Title
<input
value={this.state.title}
onChange={this.handleChange}
type="text"
name="title"
id="title"
placeholder="Title"
/>
</p>
<label>
Price
<input
type="number"
id="price"
name="price"
min="0"
value={this.state.price}
onChange={this.handleChange}
/>
</label>
<textarea
id="description"
name="description"
value={this.state.description}
onChange={this.handleChange}
placeholder="The desc for this item"
/>
<button disabled={this.state.loading} type="submit">
Submit
</button>
</Form>
</div>
);
}
}
export default graphql(CREATE_ITEM_MUTATION, {
name: 'createItemMutation',
options: {
refetchQueries: ['AllItemsQuery'],
},
})(CreateItem);
export { CreateItem };
|