Last active
February 11, 2019 20:57
-
-
Save kkemple/67b902d35b46201425d2842edea4ba62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import Amplify, { graphqlOperation } from "aws-amplify"; | |
import { withAuthenticator, Connect } from "aws-amplify-react"; | |
import "./App.css"; | |
import awsConfig from "./aws-exports"; | |
import * as mutations from "./graphql/mutations"; | |
import * as queries from "./graphql/queries"; | |
import * as subscriptions from "./graphql/subscriptions"; | |
Amplify.configure(awsConfig); | |
const ListView = ({ todos }) => ( | |
<div> | |
<h2>All Todos</h2> | |
<ul> | |
{todos.map(todo => ( | |
<li key={todo.id}> | |
{todo.name} - {todo.description} - ({todo.id}) | |
</li> | |
))} | |
</ul> | |
</div> | |
); | |
class AddTodo extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
name: "", | |
description: "" | |
}; | |
} | |
handleChange(name, ev) { | |
this.setState({ [name]: ev.target.value }); | |
} | |
async submit() { | |
const { onCreate } = this.props; | |
var input = { | |
name: this.state.name, | |
description: this.state.description | |
}; | |
this.setState({ name: "", description: "" }); | |
await onCreate({ input }); | |
} | |
render() { | |
return ( | |
<div> | |
<input | |
name="name" | |
placeholder="name" | |
onChange={ev => { | |
this.handleChange("name", ev); | |
}} | |
style={{ | |
padding: "8px 16px", | |
margin: "5px" | |
}} | |
/> | |
<input | |
name="description" | |
placeholder="description" | |
onChange={ev => { | |
this.handleChange("description", ev); | |
}} | |
style={{ | |
padding: "8px 16px", | |
margin: "5px" | |
}} | |
/> | |
<button | |
style={{ | |
padding: "8px 16px", | |
margin: "5px" | |
}} | |
onClick={this.submit.bind(this)} | |
> | |
Add | |
</button> | |
</div> | |
); | |
} | |
} | |
class App extends Component { | |
render() { | |
return ( | |
<div className="App"> | |
<h2>Add Todo</h2> | |
<Connect mutation={graphqlOperation(mutations.createTodo)}> | |
{({ mutation }) => <AddTodo onCreate={mutation} />} | |
</Connect> | |
<Connect | |
query={graphqlOperation(queries.listTodos)} | |
subscription={graphqlOperation(subscriptions.onCreateTodo)} | |
onSubscriptionMsg={(prev, { onCreateTodo }) => { | |
return { | |
listTodos: { | |
items: [...prev.listTodos.items, onCreateTodo] | |
} | |
}; | |
}} | |
> | |
{({ data: { listTodos }, loading, error }) => { | |
if (error) return <h3>Error</h3>; | |
if (loading || !listTodos) return <h3>Loading...</h3>; | |
return listTodos.items.length ? ( | |
<ListView todos={listTodos ? listTodos.items : []} /> | |
) : ( | |
<h3>No todos yet...</h3> | |
); | |
}} | |
</Connect> | |
</div> | |
); | |
} | |
} | |
export default withAuthenticator(App, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment