Connection.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import styled from 'styled-components'
  2. import gql from 'graphql-tag'
  3. import { Mutation, Query } 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. const CONNECTION_QUERY = gql`
  24. query CONNECTION_QUERY($id: ID!) {
  25. connection(id: $id) {
  26. workerInfo {
  27. pid
  28. killed
  29. exitCode
  30. signalCode
  31. }
  32. }
  33. }
  34. `
  35. class Connection extends React.Component {
  36. state = {
  37. command: ''
  38. }
  39. changeInput = event => {
  40. this.setState({ [event.target.id]: event.target.value })
  41. }
  42. render() {
  43. const { id, device, interfaceName } = this.props.data
  44. console.log(id, device, interfaceName)
  45. return (
  46. <Mutation
  47. mutation={CONNECTION_COMMAND}
  48. variables={{
  49. connectionId: id,
  50. type: 'ask',
  51. string: this.state.command
  52. }}
  53. refetchQueries={[{ query: INTERFACE_LIST }]}
  54. fetchPolicy='no-cache'>
  55. {(connectionCommand, { data, error, loading }) => (
  56. <StyledConnection>
  57. <h1>Connection</h1>
  58. <fieldset>
  59. <label htmlFor='command'>Command</label>
  60. <input type='text' value={this.state.command} onChange={this.changeInput} id='command' placeholder='Command' />
  61. </fieldset>
  62. <button type='submit' onClick={connectionCommand} disabled={loading}>Send</button>
  63. <textarea id='response' value={data && data.connectionCommand} readOnly={true} />
  64. <textarea id='error' value={error} readOnly={true} />
  65. <Query query={CONNECTION_QUERY} variables={{ id }}>
  66. {({ data, error, loading }) => {
  67. if (loading) return null
  68. if (error) return null
  69. console.log(data)
  70. const { connection: { workerInfo } } = data
  71. return (
  72. <>
  73. <p>pid: {workerInfo.pid}</p>
  74. <p>killed: {workerInfo.killed ? 'yes' : 'no'}</p>
  75. </>
  76. )
  77. }}
  78. </Query>
  79. </StyledConnection>
  80. )}
  81. </Mutation>
  82. )
  83. }
  84. }
  85. export default Connection