Connection.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. <textarea id='error' value={error} readOnly={true} />
  51. </StyledConnection>
  52. )}
  53. </Mutation>
  54. )
  55. }
  56. }
  57. export default Connection