import React, { Component } from 'react'; import { Query, Mutation } from 'react-apollo'; import PropTypes from 'prop-types'; import { SINGLE_ITEM_QUERY, UPDATE_ITEM_MUTATION } from '../queries/queries.graphql'; import Form from './styles/Form'; import Error from './ErrorMessage'; 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); } console.log('Saving to state'); 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}