From 169b0953ad91f22bb3b2bf2d80d87ebfbe30d45d Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Thu, 22 Mar 2018 21:11:15 -0400 Subject: yay --- frontend/components/UpdateItem.js | 96 +++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 49 deletions(-) (limited to 'frontend/components/UpdateItem.js') diff --git a/frontend/components/UpdateItem.js b/frontend/components/UpdateItem.js index 4841006..4292d41 100644 --- a/frontend/components/UpdateItem.js +++ b/frontend/components/UpdateItem.js @@ -1,16 +1,15 @@ import React, { Component } from 'react'; -import { graphql, gql, compose } from 'react-apollo'; -import { SINGLE_ITEM_QUERY, UPDATE_LINK_MUTATION } from '../queries'; +import { Query, Mutation } from 'react-apollo'; +import { SINGLE_ITEM_QUERY, UPDATE_ITEM_MUTATION } from '../queries'; import Form from './styles/Form'; - -import { singleItemEnhancer } from '../enhancers/enhancers'; +import Error from './ErrorMessage'; +import Dump from './Dump'; class UpdateItem extends Component { state = { item: {}, }; - saveToState = e => { let { name, value, type } = e.target; if (type === 'number') { @@ -22,64 +21,63 @@ class UpdateItem extends Component { this.setState({ item }); }; - updateItem = async e => { + updateItem = async (e, updateItemMutation) => { + console.log('Updating Item'); e.preventDefault(); - // pull the values from state - const { description, title } = this.state; - const { id } = this.props; - // create a mutation - // TODO: handle any errors. If you send an extra field it should break -how do we display those errors if they are currently only visible via the network tab? - // turn loading on - this.setState({ loading: true }); - const res = await this.props.updateItem({ + + const res = await updateItemMutation({ // pass in those variables from state variables: { - ...this.props.findItem.items[0], + id: this.props.id, ...this.state.item, }, }); console.log(res); - this.setState({ loading: false }); }; render() { - if (this.props.findItem.loading) { - return

Loading...

; - } - - const item = this.props.findItem.items[0]; return ( -
-
-

Edit {item.title}

- {this.state.loading ? 'LOADING...' : 'Ready!'} -
- + + {({ data: { items } }) => { + if (!items || !items.length) return

Item Not Found

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

Edit {item.title}

+
+ -