Connection.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import styled from 'styled-components'
  2. import gql from 'graphql-tag'
  3. import { Mutation } from 'react-apollo'
  4. import {INTERFACE_LIST} from './InterfaceList'
  5. const StyledConnection = styled.div`
  6. fieldset {
  7. display: grid;
  8. grid-template-columns: 1fr 2fr;
  9. }
  10. textarea {
  11. display: block;
  12. font-family: 'roboto_mono';
  13. }
  14. h1 {
  15. grid-column: span 2;
  16. }
  17. `
  18. const CONNECTION_COMMAND = gql`
  19. mutation CONNECTION_COMMAND($connectionId: ID!, $type: String!, $string: String!, $options: String) {
  20. connectionCommand(connectionId: $connectionId, type: $type, string: $string, options: $options)
  21. }
  22. `
  23. class Connection extends React.Component {
  24. state = {
  25. command: ''
  26. }
  27. changeInput = event => {
  28. this.setState({ [event.target.id]: event.target.value })
  29. }
  30. render() {
  31. const { id, device, interfaceName } = this.props.data
  32. return (
  33. <Mutation
  34. mutation={CONNECTION_COMMAND}
  35. variables={{
  36. connectionId: this.props.data.id,
  37. type: 'ask',
  38. string: this.state.command
  39. }}
  40. refetchQueries={[{query: INTERFACE_LIST}]}>
  41. {(connectionCommand, {data, error, loading}) => (
  42. <StyledConnection>
  43. <h1>Connection</h1>
  44. <fieldset>
  45. <label htmlFor='command'>Command</label>
  46. <input type='text' value={this.state.command} onChange={this.changeInput} id='command' placeholder='Command' />
  47. </fieldset>
  48. <button type='submit' onClick={connectionCommand} disabled={loading}>Send</button>
  49. <textarea id='response' value={data && data.connectionCommand} readOnly={true} />
  50. </StyledConnection>
  51. )}
  52. </Mutation>
  53. )
  54. }
  55. }
  56. export default Connection