From 222ab374d53a206332b1ab51c7c43f6d5dc7a31e Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Tue, 8 Aug 2017 22:09:05 -0400 Subject: yep --- components/UpdateItem.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 components/UpdateItem.js (limited to 'components/UpdateItem.js') diff --git a/components/UpdateItem.js b/components/UpdateItem.js new file mode 100644 index 0000000..8ca8b73 --- /dev/null +++ b/components/UpdateItem.js @@ -0,0 +1,82 @@ +import React, { Component } from 'react' +import { graphql, gql, compose } from 'react-apollo' +import { SINGLE_LINK_QUERY, UPDATE_LINK_MUTATION } from '../queries'; + +class UpdateLink extends Component { + + state = { + ...this.props.findItem.Item, + price: 0, + fullPrice: 0 + } + + saveToState = (e) => { + let { name, value, type } = e.target; + if (type === 'number') { + value = parseInt(value); + } + + this.setState({ [name]: value }); + } + + render() { + return ( +
+ +

Edit {this.props.id}

+ { this.state.loading ? 'LOADING...' : 'Ready!' } +
+ + + + + + + + + + + + + + +
+
+ ) + } + + _createLink = async (e) => { + e.preventDefault(); + // pull the values from state + const { description, title } = this.state + const { id } = this.props; + // create a mutation + // TODO: handle any errors + // turn loading on + this.setState({ loading: true }); + console.log(this.state); + const res = await this.props.updateItem({ + // pass in those variables from state + variables: { + ...this.state + } + }); + this.setState({ loading: false }); + } + +} + +const ComponentWithMutations = compose( + // First, query for getting the link + graphql(SINGLE_LINK_QUERY, { + name: 'findItem', + options: ({ id }) => ({ + variables: { id } + }) + }), + // Second, the mutation for updating the link + graphql(UPDATE_LINK_MUTATION, { name: 'updateItem' }) +)(UpdateLink); + + +export default ComponentWithMutations; -- cgit v1.3