Instrument.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import gql from 'graphql-tag'
  2. import { Query, Mutation } from 'react-apollo'
  3. import { adopt } from 'react-adopt'
  4. import InstrumentSubsystem, { InstrumentSubsystemFormFields, InstrumentSubsystemFields } from './InstrumentSubsystem'
  5. import File, { FileFormFields } from './File'
  6. import Gallery from './Gallery'
  7. import { INTERFACES_FULL } from './InterfaceList'
  8. const CREATE_INSTRUMENT = gql`
  9. mutation CREATE_INSTRUMENT($name: String!, $description: String!, $interfaces: [String]!) {
  10. createInstrument(name: $name, description: $description, interfaces: $interfaces) {
  11. id
  12. }
  13. }
  14. `
  15. const InstrumentFields = {
  16. id: null,
  17. name: '',
  18. description: '',
  19. documents: [],
  20. interfaces: [],
  21. commands: [],
  22. parameters: [],
  23. subsystems: []
  24. }
  25. const MockInterfaces = { interfaces: [{ name: 'serial' }, { name: 'usbtmc' }] }
  26. class InstrumentForm extends React.Component {
  27. state = {
  28. ...InstrumentFields,
  29. ...this.props.instrument
  30. }
  31. toState = event => {
  32. this.setState({ [event.target.name]: event.target.value })
  33. }
  34. handleInterface = event => {
  35. let interfaces = this.state.interfaces
  36. const ifaceSelected = event.target.checked
  37. const index = this.state.interfaces.findIndex(iface => iface === event.target.value)
  38. if (ifaceSelected && index < 0) interfaces = [
  39. ...this.state.interfaces,
  40. event.target.value
  41. ]
  42. if (!ifaceSelected && index >= 0) interfaces = [
  43. ...this.state.interfaces.slice(0, index),
  44. ...this.state.interfaces.slice(index + 1)
  45. ]
  46. this.setState({ interfaces })
  47. }
  48. render() {
  49. return (
  50. <form>
  51. <h1>Create new instrument.</h1>
  52. <fieldset>
  53. <label htmlFor='name'>Name</label>
  54. <input type='text' name='name' id='name' placeholder='Name'
  55. value={this.state.name}
  56. onChange={this.toState} />
  57. <label htmlFor='description'>Description</label>
  58. <textarea name='description' id='description' placeholder='Description'
  59. value={this.state.description}
  60. onChange={this.toState} />
  61. </fieldset>
  62. {this.state.documents.map(file =>
  63. <FileFormFields />
  64. )}
  65. <Query query={INTERFACES_FULL}>
  66. {({ data, error, loading }) => {
  67. if (loading) return <p>Loading interfaces.</p>
  68. //if (error) return <p>Error loading interfaces: {error.message}</p>
  69. //if (!data.length) return <p>No interfaces found.</p>
  70. return (
  71. <fieldset>
  72. {MockInterfaces.interfaces.map((iface, index) =>
  73. <div key={iface.name}>
  74. <label htmlFor={iface.name}>{iface.name}</label>
  75. <input type='checkbox' name='interface' id={iface.name}
  76. value={iface.name}
  77. checked={this.state.interfaces.includes(iface.name)}
  78. onChange={this.handleInterface} />
  79. </div>
  80. )}
  81. </fieldset>
  82. )
  83. }}
  84. </Query>
  85. {this.state.subsystems.map((subsystem, index) =>
  86. <InstrumentSubsystemFormFields key={index} state={subsystem} onChange={event => {
  87. const updatedSubsystem = { ...this.state.subsystems[index] }
  88. updatedSubsystem[event.target.name] = event.target.value
  89. this.setState({
  90. subsystems: [
  91. ...this.state.subsystems.slice(0, index),
  92. updatedSubsystem,
  93. ...this.state.subsystems.slice(index + 1)
  94. ]
  95. })
  96. }} />
  97. )}
  98. <button type='button' onClick={event => {
  99. this.setState({ subsystems: [...this.state.subsystems, InstrumentFields] })
  100. }}>Add subsystem</button>
  101. </form>
  102. )
  103. }
  104. }
  105. const Instrument = props => {
  106. const { instrument } = props
  107. return instrument ? (
  108. <div>
  109. <h1>{instrument.name}</h1>
  110. <p>{instrument.description}</p>
  111. <Gallery title='Documents' items={['Hallo']} />
  112. <Gallery title='Interfaces' items={['serial', 'usbtmc'].map(item => <div>{item}</div>)} />
  113. <Gallery title='Subsystems' items={instrument.subsystems.map(subsystem =>
  114. <InstrumentSubsystem subsystem={subsystem} />
  115. )} />
  116. </div>
  117. ) : (
  118. <p>Instrument not found.</p>
  119. )
  120. }
  121. export default Instrument
  122. export { InstrumentFields, InstrumentForm }