Skip to content

Commit 93462fc

Browse files
Wolf, FyodorFedyaNyet
authored andcommitted
added item creation.
1 parent 145e8c6 commit 93462fc

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import React, { Component } from 'react'
2+
import {Mutation} from 'react-apollo';
3+
import gql from 'graphql-tag';
4+
import Router from 'next/router';
5+
import Form from './styles/Form';
6+
import Error from './ErrorMessage';
7+
8+
const CREATE_ITEM_MUTATION = gql`
9+
mutation CREATE_ITEM_MUTATION(
10+
$title: String!
11+
$description: String!
12+
$price: Int!
13+
$image: String
14+
$largeImage: String
15+
) {
16+
createItem(
17+
title: $title
18+
description: $description
19+
image: $image
20+
largeImage: $largeImage
21+
price: $price
22+
) {
23+
id
24+
}
25+
}
26+
`;
27+
28+
class CreateItem extends Component {
29+
state = {
30+
title:"",
31+
price:'',
32+
image: '',
33+
largeImage: '',
34+
description:""
35+
}
36+
handleChange = (e) => {
37+
const {name, type, value} = e.target;
38+
const val = type === 'number' ? parseFloat(value) : value;
39+
this.setState({[name]: val});
40+
}
41+
render() {
42+
return (
43+
<Mutation mutation={CREATE_ITEM_MUTATION} variables={this.state}>
44+
{(createItem, {loading, error, called, data}) => (
45+
<Form
46+
onSubmit={ async e => {
47+
e.preventDefault();
48+
const res = await createItem();
49+
Router.push({
50+
pathname: '/item',
51+
query: {id: res.data.createItem.id}
52+
})
53+
}}
54+
>
55+
<Error error={error}/>
56+
<fieldset disabled={loading} aria-busy={loading}>
57+
<label htmlFor="title">
58+
Title
59+
<input
60+
type="text"
61+
id="title"
62+
input="title"
63+
name="title"
64+
placeholder="Title"
65+
required
66+
value={this.state.title}
67+
onChange={this.handleChange}
68+
/>
69+
</label>
70+
<label htmlFor="price">
71+
Price
72+
<input
73+
type="number"
74+
id="price"
75+
input="price"
76+
name="price"
77+
placeholder="price"
78+
required
79+
value={this.state.price}
80+
onChange={this.handleChange}
81+
/>
82+
</label>
83+
<label htmlFor="description">
84+
Description
85+
<textarea
86+
id="description"
87+
input="description"
88+
name="description"
89+
placeholder="Enter a description"
90+
required
91+
value={this.state.description}
92+
onChange={this.handleChange}
93+
/>
94+
</label>
95+
<button type="submit" >Submit</button>
96+
</fieldset>
97+
</Form>
98+
)}
99+
</Mutation>
100+
)
101+
}
102+
}
103+
104+
export default CreateItem;
105+
export {CREATE_ITEM_MUTATION};

sick-fits/frontend/components/Items.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const ItemList = styled.div`
2929
margin: 0 auto;
3030
`;
3131

32-
export default class Items extends Component {
32+
class Items extends Component {
3333
render() {
3434
return (
3535
<Center>
@@ -52,3 +52,6 @@ export default class Items extends Component {
5252
)
5353
}
5454
}
55+
56+
export default Items;
57+
export {ALL_ITEMS_QUERY};

sick-fits/frontend/pages/sell.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import Link from 'next/link';
2+
import CreateItem from '../components/CreateItem';
23

34
const Sell = () => {
45
return <div>
5-
<p>Sell</p>
6+
<CreateItem/>
67
</div>;
78
}
89

0 commit comments

Comments
 (0)