From b1600adec47a04f60e1da07d94a3ed3906ff5aee Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 14 Jun 2018 16:44:21 -0400 Subject: starter files --- .../frontend/components/UpdateItem.js | 105 +++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 finished-application/frontend/components/UpdateItem.js (limited to 'finished-application/frontend/components/UpdateItem.js') diff --git a/finished-application/frontend/components/UpdateItem.js b/finished-application/frontend/components/UpdateItem.js new file mode 100644 index 0000000..08ba254 --- /dev/null +++ b/finished-application/frontend/components/UpdateItem.js @@ -0,0 +1,105 @@ +import React, { Component } from 'react'; +import { Query, Mutation } from 'react-apollo'; +import PropTypes from 'prop-types'; +import gql from 'graphql-tag'; +import { SINGLE_ITEM_QUERY } from './SingleItem'; +import Form from './styles/Form'; +import Error from './ErrorMessage'; + +const UPDATE_ITEM_MUTATION = gql` + mutation updateItem($id: ID!, $title: String, $description: String, $price: Int) { + updateItem(id: $id, description: $description, title: $title, price: $price) { + id + } + } +`; + +class UpdateItem extends Component { + static propTypes = { + id: PropTypes.string.isRequired, + }; + state = { + item: {}, + }; + + saveToState = e => { + let { name, value, type } = e.target; + if (type === 'number') { + value = parseInt(value); + } + const item = { ...this.state.item }; + item[name] = value; + this.setState({ item }); + }; + + updateItem = async (e, updateItemMutation) => { + console.log('Updating Item'); + e.preventDefault(); + + const res = await updateItemMutation({ + // pass in those variables from state + variables: { + id: this.props.id, + ...this.state.item, + }, + }); + console.log(res); + }; + + render() { + return ( + + {({ data: { items }, loading }) => { + if (loading) return

Loading...

; + if (!items || !items.length) return

Item Not Found

; + const [item] = items; + return ( + + {(updateItemMutation, { error }) => ( +
this.updateItem(e, updateItemMutation)}> + +

Edit {item.title}

+
+ + +