Connection.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 SEND_COMMAND = gql`
  19. mutation SEND_COMMAND($connectionId: ID!, $command: ConnectionCommand!) {
  20. sendCommand(connectionId: $connectionId, command: $command)
  21. }
  22. `
  23. const CONNECTION_QUERY = gql`
  24. query CONNECTION_QUERY($id: ID!) {
  25. connection(id: $id) {
  26. id
  27. workerProcess {
  28. pid
  29. killed
  30. signalCode
  31. exitCode
  32. spawnfile
  33. spawnargs
  34. }
  35. }
  36. }
  37. `
  38. class Connection extends React.Component {
  39. state = {
  40. data: '',
  41. type: 'ask',
  42. typeCustom: ''
  43. }
  44. changeInput = event => {
  45. this.setState({ [event.target.name]: event.target.value })
  46. }
  47. render () {
  48. const { id } = this.props.data
  49. return (
  50. <Mutation
  51. mutation={SEND_COMMAND}
  52. variables={{
  53. connectionId: id,
  54. command: {
  55. type:
  56. this.state.type === 'custom'
  57. ? this.state.typeCustom
  58. : this.state.type,
  59. data: this.state.data
  60. }
  61. }}
  62. refetchQueries={[{ query: INTERFACES_FULL }]}
  63. fetchPolicy='no-cache'
  64. >
  65. {(sendCommand, { data, error, loading }) => {
  66. return (
  67. <StyledConnection>
  68. <h1>Connection</h1>
  69. <fieldset>
  70. <label htmlFor='data'>Command</label>
  71. <input
  72. type='text'
  73. value={this.state.data}
  74. onChange={this.changeInput}
  75. id='data'
  76. name='data'
  77. disabled={this.state.type === 'read'}
  78. placeholder='Command'
  79. />
  80. </fieldset>
  81. <h2>Command type</h2>
  82. <label htmlFor='type-read'>read</label>
  83. <input
  84. id='type-read'
  85. name='type'
  86. type='radio'
  87. value='read'
  88. checked={this.state.type === 'read'}
  89. onChange={this.changeInput}
  90. />
  91. <label htmlFor='type-ask'>ask</label>
  92. <input
  93. id='type-ask'
  94. name='type'
  95. type='radio'
  96. value='ask'
  97. checked={this.state.type === 'ask'}
  98. onChange={this.changeInput}
  99. />
  100. <label htmlFor='type-write'>write</label>
  101. <input
  102. id='type-write'
  103. name='type'
  104. type='radio'
  105. value='write'
  106. checked={this.state.type === 'write'}
  107. onChange={this.changeInput}
  108. />
  109. <label htmlFor='type-custom'>Custom...</label>
  110. <input
  111. id='type-custom'
  112. name='type'
  113. type='radio'
  114. value='custom'
  115. checked={this.state.type === 'custom'}
  116. onChange={this.changeInput}
  117. />
  118. <input
  119. id='typeCustom'
  120. name='typeCustom'
  121. type='text'
  122. name='typeCustom'
  123. value={this.state.typeCustom}
  124. disabled={this.state.type !== 'custom'}
  125. onChange={this.changeInput}
  126. />
  127. <button type='submit' onClick={sendCommand} disabled={loading}>
  128. Send
  129. </button>
  130. <textarea
  131. id='response'
  132. value={data && data.sendCommand}
  133. readOnly
  134. />
  135. <textarea id='error' value={error} readOnly />
  136. <Query query={CONNECTION_QUERY} variables={{ id }}>
  137. {({ data, error, loading }) => {
  138. if (loading) return null
  139. if (error) return null
  140. const {
  141. connection: { workerProcess }
  142. } = data
  143. return (
  144. <>
  145. <p>pid: {workerProcess.pid}</p>
  146. <p>killed: {workerProcess.killed ? 'yes' : 'no'}</p>
  147. <p>exitCode: {workerProcess.exitCode}</p>
  148. <p>signalCode: {workerProcess.signalCode}</p>
  149. <p>spawnfile: {workerProcess.spawnfile}</p>
  150. <p>spawnargs: {workerProcess.spawnargs.join(', ')}</p>
  151. </>
  152. )
  153. }}
  154. </Query>
  155. </StyledConnection>
  156. )
  157. }}
  158. </Mutation>
  159. )
  160. }
  161. }
  162. export default Connection