From ff7fc88e36bb66a47327fa2c7e8513b222e539ef Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Wed, 27 Jun 2018 13:02:25 -0400 Subject: Video 21 --- .../18/frontend/components/CreateItem.js | 109 +++++++++++++++++++++ stepped-solutions/18/frontend/components/Items.js | 51 ++++++++++ 2 files changed, 160 insertions(+) create mode 100755 stepped-solutions/18/frontend/components/CreateItem.js create mode 100755 stepped-solutions/18/frontend/components/Items.js (limited to 'stepped-solutions/18/frontend/components') diff --git a/stepped-solutions/18/frontend/components/CreateItem.js b/stepped-solutions/18/frontend/components/CreateItem.js new file mode 100755 index 0000000..cf389da --- /dev/null +++ b/stepped-solutions/18/frontend/components/CreateItem.js @@ -0,0 +1,109 @@ +import React, { Component } from 'react'; +import { Mutation } from 'react-apollo'; +import gql from 'graphql-tag'; +import Router from 'next/router'; +import Form from './styles/Form'; +import formatMoney from '../lib/formatMoney'; +import Error from './ErrorMessage'; + +const CREATE_ITEM_MUTATION = gql` + mutation CREATE_ITEM_MUTATION( + $title: String! + $description: String! + $price: Int! + $image: String + $largeImage: String + ) { + createItem( + title: $title + description: $description + price: $price + image: $image + largeImage: $largeImage + ) { + id + } + } +`; + +class CreateItem extends Component { + state = { + title: 'Cool Shoes', + description: 'I love those shoes', + image: 'dog.jpg', + largeImage: 'large-dog.jpg', + price: 1000, + }; + handleChange = e => { + const { name, type, value } = e.target; + const val = type === 'number' ? parseFloat(value) : value; + this.setState({ [name]: val }); + }; + render() { + return ( + + {(createItem, { loading, error }) => ( +
{ + // Stop the form from submitting + e.preventDefault(); + // call the mutation + const res = await createItem(); + // change them to the single item page + console.log(res); + Router.push({ + pathname: '/item', + query: { id: res.data.createItem.id }, + }); + }} + > + +
+ + + + +