summaryrefslogtreecommitdiffstats
path: root/stepped-solutions/18/frontend
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-06-27 13:02:25 -0400
committerWes Bos <wesbos@gmail.com>2018-06-27 13:02:25 -0400
commitff7fc88e36bb66a47327fa2c7e8513b222e539ef (patch)
treeecfc2901b53d8e068252b058a07d1a4e63d42816 /stepped-solutions/18/frontend
parent4e52a98a0f78d2bedfdd31a1955639143cd26fc6 (diff)
Video 21
Diffstat (limited to 'stepped-solutions/18/frontend')
-rwxr-xr-xstepped-solutions/18/frontend/components/CreateItem.js109
-rwxr-xr-xstepped-solutions/18/frontend/components/Items.js51
-rwxr-xr-xstepped-solutions/18/frontend/pages/sell.js9
3 files changed, 169 insertions, 0 deletions
diff --git a/stepped-solutions/18/frontend/components/CreateItem.js b/stepped-solutions/18/frontend/components/CreateItem.js
new file mode 100755
index 0000000..cf389da
--- /dev/null
+++ b/stepped-solutions/18/frontend/components/CreateItem.js
@@ -0,0 +1,109 @@
+import React, { Component } from 'react';
+import { Mutation } from 'react-apollo';
+import gql from 'graphql-tag';
+import Router from 'next/router';
+import Form from './styles/Form';
+import formatMoney from '../lib/formatMoney';
+import Error from './ErrorMessage';
+
+const CREATE_ITEM_MUTATION = gql`
+ mutation CREATE_ITEM_MUTATION(
+ $title: String!
+ $description: String!
+ $price: Int!
+ $image: String
+ $largeImage: String
+ ) {
+ createItem(
+ title: $title
+ description: $description
+ price: $price
+ image: $image
+ largeImage: $largeImage
+ ) {
+ id
+ }
+ }
+`;
+
+class CreateItem extends Component {
+ state = {
+ title: 'Cool Shoes',
+ description: 'I love those shoes',
+ image: 'dog.jpg',
+ largeImage: 'large-dog.jpg',
+ price: 1000,
+ };
+ handleChange = e => {
+ const { name, type, value } = e.target;
+ const val = type === 'number' ? parseFloat(value) : value;
+ this.setState({ [name]: val });
+ };
+ render() {
+ return (
+ <Mutation mutation={CREATE_ITEM_MUTATION} variables={this.state}>
+ {(createItem, { loading, error }) => (
+ <Form
+ onSubmit={async e => {
+ // Stop the form from submitting
+ e.preventDefault();
+ // call the mutation
+ const res = await createItem();
+ // change them to the single item page
+ console.log(res);
+ Router.push({
+ pathname: '/item',
+ query: { id: res.data.createItem.id },
+ });
+ }}
+ >
+ <Error error={error} />
+ <fieldset disabled={loading} aria-busy={loading}>
+ <label htmlFor="title">
+ Title
+ <input
+ type="text"
+ id="title"
+ name="title"
+ placeholder="Title"
+ required
+ value={this.state.title}
+ onChange={this.handleChange}
+ />
+ </label>
+
+ <label htmlFor="price">
+ Price
+ <input
+ type="number"
+ id="price"
+ name="price"
+ placeholder="Price"
+ required
+ value={this.state.price}
+ onChange={this.handleChange}
+ />
+ </label>
+
+ <label htmlFor="description">
+ Description
+ <textarea
+ id="description"
+ name="description"
+ placeholder="Enter A Description"
+ required
+ value={this.state.description}
+ onChange={this.handleChange}
+ />
+ </label>
+ <button type="submit">Submit</button>
+ </fieldset>
+ </Form>
+ )}
+ </Mutation>
+ );
+ }
+}
+
+export default CreateItem;
+export { CREATE_ITEM_MUTATION };
diff --git a/stepped-solutions/18/frontend/components/Items.js b/stepped-solutions/18/frontend/components/Items.js
new file mode 100755
index 0000000..5ff4055
--- /dev/null
+++ b/stepped-solutions/18/frontend/components/Items.js
@@ -0,0 +1,51 @@
+import React, { Component } from 'react';
+import { Query } from 'react-apollo';
+import gql from 'graphql-tag';
+import styled from 'styled-components';
+import Item from './Item';
+
+const ALL_ITEMS_QUERY = gql`
+ query ALL_ITEMS_QUERY {
+ items {
+ id
+ title
+ price
+ description
+ image
+ largeImage
+ }
+ }
+`;
+
+const Center = styled.div`
+ text-align: center;
+`;
+
+const ItemsList = styled.div`
+ display: grid;
+ grid-template-columns: 1fr 1fr;
+ grid-gap: 60px;
+ max-width: ${props => props.theme.maxWidth};
+ margin: 0 auto;
+`;
+
+class Items extends Component {
+ render() {
+ return (
+ <Center>
+ <Query query={ALL_ITEMS_QUERY}>
+ {({ data, error, loading }) => {
+ if (loading) return <p>Loading...</p>;
+ if (error) return <p>Error: {error.message}</p>;
+ return (
+ <ItemsList>{data.items.map(item => <Item item={item} key={item.id} />)}</ItemsList>
+ );
+ }}
+ </Query>
+ </Center>
+ );
+ }
+}
+
+export default Items;
+export { ALL_ITEMS_QUERY };
diff --git a/stepped-solutions/18/frontend/pages/sell.js b/stepped-solutions/18/frontend/pages/sell.js
new file mode 100755
index 0000000..0e75da4
--- /dev/null
+++ b/stepped-solutions/18/frontend/pages/sell.js
@@ -0,0 +1,9 @@
+import CreateItem from '../components/CreateItem';
+
+const Sell = props => (
+ <div>
+ <CreateItem />
+ </div>
+);
+
+export default Sell;