Guest User

Untitled

a guest
Jun 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. import React, { PureComponent } from "react"
  2. import { gql } from 'apollo-boost';
  3. import { Query, Mutation } from 'react-apollo';
  4. import { FormGroup, ControlLabel, FormControl } from "react-bootstrap";
  5.  
  6. const QUERY_DIRECTOR = gql`
  7. query current_genre($uuid: UUID!) {
  8. movieByUuid(uuid: $uuid) {
  9. director
  10. }
  11. }`;
  12.  
  13. const UPDATE_MOVIE_DIRECTOR_MUTATION = gql`
  14. mutation updateMovieDirector($uuid: UUID!, $director: String!){
  15. updateMovieByUuid(input: {
  16. uuid: $uuid
  17. moviePatch: {director: $director}
  18. }) {
  19. clientMutationId
  20. }
  21. }
  22. `;
  23.  
  24. class FormDirector extends PureComponent {
  25. constructor(props) {
  26. super(props)
  27.  
  28. this.state = {
  29. director: ''
  30. }
  31. }
  32.  
  33. render() {
  34. const { readOnly, uuid } = this.props
  35. const { director } = this.state
  36.  
  37. return (
  38. <FormGroup controlId="formBasicText">
  39. <ControlLabel>Director</ControlLabel>
  40. <Query query={QUERY_DIRECTOR} variables={{ uuid }} pollInterval={readOnly ? 1000 : 0} >
  41. {({ loading, error, data }) => {
  42. if (loading) return "Loading...";
  43. if (error) return `Error! ${error.message}`;
  44.  
  45. readOnly && this.setState({ director: data.movieByUuid ? data.movieByUuid.director : '' })
  46.  
  47. return (
  48. <Mutation mutation={UPDATE_MOVIE_DIRECTOR_MUTATION}>
  49. {mutation => (
  50. <FormControl
  51. type="text"
  52. value={director}
  53. readOnly={readOnly}
  54. onChange={e => this.setState({ director: e.target.value })}
  55. onBlur={e => mutation({ variables: { uuid, director } })}
  56. />
  57. )}
  58. </Mutation>
  59. )
  60. }}
  61. </Query>
  62. </FormGroup>
  63. )
  64. }
  65. }
  66.  
  67. export default FormDirector
Add Comment
Please, Sign In to add comment