From cd0b07b3adb36fb5ec098f9e511ab45d774fee7b Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Tue, 30 Jan 2018 12:41:01 -0500 Subject: Move to Prisma Backend --- frontend/components/CreateItem.js | 128 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 frontend/components/CreateItem.js (limited to 'frontend/components/CreateItem.js') diff --git a/frontend/components/CreateItem.js b/frontend/components/CreateItem.js new file mode 100644 index 0000000..27b62e5 --- /dev/null +++ b/frontend/components/CreateItem.js @@ -0,0 +1,128 @@ +import React, { Component } from 'react'; +import { graphql, gql } from 'react-apollo'; +import { ALL_ITEMS_QUERY, CREATE_ITEM_MUTATION } from '../queries'; +import ErrorMessage from './ErrorMessage'; +import makeImage from '../lib/image'; +import { fileEndpoint } from '../config'; + +class CreateLink extends Component { + state = { + description: '', + title: '', + image: '', + price: 0, + fullPrice: 0, + loading: false, + error: { + message: '', + }, + }; + + componentWillReceiveProps(nextProps) { + console.log(nextProps); + } + + uploadFile = async e => { + this.setState({ loading: true }); + const files = e.currentTarget.files; + + const data = new FormData(); + data.append('data', files[0]); + + // use the file endpoint + const res = await fetch(fileEndpoint, { + method: 'POST', + body: data, + }); + const file = await res.json(); + this.setState({ image: file.id, loading: false }); + }; + + _createLink = async e => { + e.preventDefault(); + // pull the values from state + const { description, title, image, price, fullPrice } = this.state; + // create a mutation + // TODO: handle any errors + // turn loading on + this.setState({ loading: true }); + try { + const res = await this.props.createItemMutation({ + // pass in those variables from state + variables: { + description, + title, + price: parseInt(price), + fullPrice, + imageId: image, + }, + }); + } catch (error) { + this.setState({ error }); + console.log(error); + } + this.setState({ loading: false }); + }; + + render() { + return ( +
+ {this.state.loading ? 'LOADING...' : 'Ready!'} + + this.setState({ error: {} })} /> +
+

+ Image + +

+

+ Title + this.setState({ title: e.target.value })} + type="text" + placeholder="Title" + /> +

+ +