Connection.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import styled from 'styled-components'
  2. import gql from 'graphql-tag'
  3. import { Mutation, Query } from 'react-apollo'
  4. import { INTERFACES_FULL } 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(
  20. $connectionId: ID!
  21. $type: String!
  22. $string: String!
  23. $options: String
  24. ) {
  25. connectionCommand(
  26. connectionId: $connectionId
  27. type: $type
  28. string: $string
  29. options: $options
  30. )
  31. }
  32. `
  33. const CONNECTION_QUERY = gql`
  34. query CONNECTION_QUERY($id: ID!) {
  35. connection(id: $id) {
  36. workerInfo {
  37. pid
  38. killed
  39. exitCode
  40. signalCode
  41. }
  42. }
  43. }
  44. `
  45. class Connection extends React.Component {
  46. state = {
  47. command: ''
  48. }
  49. changeInput = event => {
  50. this.setState({ [event.target.id]: event.target.value })
  51. }
  52. render () {
  53. const { id, device, interfaceName } = this.props.data
  54. console.log(id, device, interfaceName)
  55. return (
  56. <Mutation
  57. mutation={CONNECTION_COMMAND}
  58. variables={{
  59. connectionId: id,
  60. type: 'ask',
  61. string: this.state.command
  62. }}
  63. refetchQueries={[{ query: INTERFACES_FULL }]}
  64. fetchPolicy='no-cache'
  65. >
  66. {(connectionCommand, { data, error, loading }) => (
  67. <StyledConnection>
  68. <h1>Connection</h1>
  69. <fieldset>
  70. <label htmlFor='command'>Command</label>
  71. <input
  72. type='text'
  73. value={this.state.command}
  74. onChange={this.changeInput}
  75. id='command'
  76. placeholder='Command'
  77. />
  78. </fieldset>
  79. <button
  80. type='submit'
  81. onClick={connectionCommand}
  82. disabled={loading}
  83. >
  84. Send
  85. </button>
  86. <textarea
  87. id='response'
  88. value={data && data.connectionCommand}
  89. readOnly
  90. />
  91. <textarea id='error' value={error} readOnly />
  92. <Query query={CONNECTION_QUERY} variables={{ id }}>
  93. {({ data, error, loading }) => {
  94. if (loading) return null
  95. if (error) return null
  96. console.log(data)
  97. const {
  98. connection: { workerInfo }
  99. } = data
  100. return (
  101. <>
  102. <p>pid: {workerInfo.pid}</p>
  103. <p>killed: {workerInfo.killed ? 'yes' : 'no'}</p>
  104. </>
  105. )
  106. }}
  107. </Query>
  108. </StyledConnection>
  109. )}
  110. </Mutation>
  111. )
  112. }
  113. }
  114. export default Connection