Guest User

Untitled

a guest
Sep 26th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import Router from 'next/router';
  3. import { Mutation, Query } from 'react-apollo';
  4. import gql from 'graphql-tag';
  5. import { adopt } from 'react-adopt';
  6. import Form from './styles/Form';
  7. import FormatMoney from '../lib/formatMoney';
  8. import Error from './ErrorMessage.js';
  9.  
  10. const SINGLE_ITEM_QUERY = gql`
  11. query SINGLE_ITEM_QUERY($id: ID!) {
  12. item(where: { id: $id }) {
  13. id
  14. title
  15. description
  16. price
  17. }
  18. }
  19. `;
  20.  
  21. const UPDATE_ITEM_MUTATION = gql`
  22. mutation UPDATE_ITEM_MUTATION(
  23. $id: ID!
  24. $title: String
  25. $description: String
  26. $price: Int
  27. ) {
  28. updateItem(
  29. id: $id
  30. title: $title
  31. description: $description
  32. price: $price
  33. ) {
  34. id
  35. title
  36. description
  37. price
  38. }
  39. }
  40. `;
  41.  
  42. const Composed = adopt({
  43. singleItem: ({ that, render }) => <Query query={SINGLE_ITEM_QUERY} variables={{ id: that.props.id }}>{ render }</Query>,
  44. updateItem: ({ that, render }) => <Mutation mutation={UPDATE_ITEM_MUTATION} variables={that.state.itemData}>{ render }</Mutation>
  45. });
  46.  
  47. class UpdateItem extends Component {
  48. state = {
  49. updateItemError: null,
  50. updateItemLoading: false,
  51. itemData: {}
  52. };
  53.  
  54. handleChange = e => {
  55. const { name, type, value } = e.target;
  56. const val = type === 'number' ? parseFloat(value) : value;
  57.  
  58. var itemData = { ...this.state.itemData };
  59. itemData[name] = val;
  60. this.setState({ itemData });
  61. };
  62.  
  63. updateItem = async (e, updateItemMutation) => {
  64. e.preventDefault();
  65. let err = false;
  66.  
  67. this.setState({ updateItemLoading: true });
  68.  
  69. const res = await updateItemMutation({
  70. variables: {
  71. id: this.props.id,
  72. ...this.state.itemData,
  73. }
  74. }).catch((error) => {
  75. err = true;
  76. });
  77.  
  78. this.setState({ updateItemLoading: false });
  79. return err;
  80. };
  81.  
  82. render() {
  83. return (
  84. <Composed that={this}>
  85. {({ singleItem, updateItem }) => {
  86. if (singleItem.loading) return <p>Loading...</p>;
  87. if (!singleItem.data.item) return <p>No Item Found for ID {this.props.id}!</p>;
  88.  
  89. return (
  90. <Form onSubmit={async e => {
  91. const res = await this.updateItem(e, updateItem);
  92. if (res) this.setState({ updateItemError: { 'message': 'Something went wrong!' }});
  93. }}>
  94. <Error error={this.state.updateItemError} />
  95. <fieldset disabled={this.state.updateItemLoading} aria-busy={this.state.updateItemLoading}>
  96. <label htmlFor="title">
  97. Title
  98. <input
  99. type="text"
  100. id="title"
  101. name="title"
  102. placeholder="Title"
  103. defaultValue={singleItem.data.item.title}
  104. onChange={this.handleChange}
  105. required
  106. />
  107. </label>
  108. <label htmlFor="price">
  109. Price
  110. <input
  111. type="number"
  112. id="price"
  113. name="price"
  114. placeholder="Price"
  115. defaultValue={singleItem.data.item.price}
  116. onChange={this.handleChange}
  117. required
  118. />
  119. </label>
  120. <label htmlFor="description">
  121. Description
  122. <textarea
  123. id="description"
  124. name="description"
  125. placeholder="Enter A Description"
  126. defaultValue={singleItem.data.item.description}
  127. onChange={this.handleChange}
  128. // required
  129. />
  130. </label>
  131. <button type="submit">Sav{this.state.updateItemLoading ? 'ing' : 'e'}</button>
  132. </fieldset>
  133. </Form>
  134. );
  135. }}
  136. </Composed>
  137. );
  138. }
  139. }
  140.  
  141. export default UpdateItem;
  142. export { UPDATE_ITEM_MUTATION };
Add Comment
Please, Sign In to add comment