Guest User

Untitled

a guest
Apr 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. import React from 'react';
  2. import { Mutation } from "react-apollo";
  3. import '../../styles/Todo.css';
  4.  
  5. import {
  6. QUERY_TODO,
  7. MUTATION_TODO_ADD
  8. } from '../../graphQueries/todoQueries';
  9.  
  10. export default class TodoInput extends React.Component {
  11.  
  12. constructor() {
  13. super();
  14. this.state = {
  15. textboxValue: ''
  16. }
  17. }
  18.  
  19. handleTextboxValueChange = (e) => {
  20. this.setState({
  21. ...this.state,
  22. textboxValue: e.target.value
  23. });
  24. }
  25.  
  26. handleTextboxKeyPress = (e, addTodo) => {
  27. if (e.key === 'Enter') {
  28. const newTask = this.state.textboxValue;
  29. const userId = this.props.userId;
  30. addTodo({
  31. variables: {
  32. objects: [{
  33. task: newTask,
  34. user_id: userId,
  35. completed: false
  36. }]
  37. },
  38. update: (store, { data: { insert_todo }}) => {
  39. const data = store.readQuery({ query: QUERY_TODO })
  40. const insertedTodo = insert_todo.returning;
  41. data.todo.splice(0, 0, insertedTodo[0])
  42. store.writeQuery({
  43. query: QUERY_TODO,
  44. data
  45. })
  46. this.setState({
  47. ...this.state,
  48. textboxValue: ''
  49. });
  50. }
  51. })
  52. }
  53. }
  54.  
  55. render() {
  56. return (
  57. <Mutation mutation={MUTATION_TODO_ADD}>
  58. {(addTodo, { data, loading, called, error }) => {
  59. return (
  60. <div className="parentContainer">
  61. <input className="input" placeholder="Add a todo" value={this.state.textboxValue} onChange={this.handleTextboxValueChange} onKeyPress={e => {
  62. this.handleTextboxKeyPress(e, addTodo);
  63. }}/>
  64. <br />
  65. </div>
  66. )
  67. }}
  68. </Mutation>
  69. )
  70. }
  71. }
Add Comment
Please, Sign In to add comment