|
| 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}; |
0 commit comments