Interface.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { Query } from 'react-apollo'
  2. import Gallery from './Gallery'
  3. import Port from './Port'
  4. import Connection from './Connection'
  5. import InterfaceOption from './InterfaceOption'
  6. import { INTERFACES } from './InterfaceList'
  7. const InterfaceFormFields = props => {
  8. const { state, onChange } = props
  9. const toState = event => {
  10. let interfaces = state
  11. const ifaceSelected = event.target.checked
  12. const index = state.findIndex(iface => iface === event.target.value)
  13. if (ifaceSelected && index < 0) {
  14. interfaces = [...state, event.target.value]
  15. }
  16. if (!ifaceSelected && index >= 0) {
  17. interfaces = [...state.slice(0, index), ...state.slice(index + 1)]
  18. }
  19. onChange(interfaces)
  20. }
  21. return (
  22. <Query query={INTERFACES}>
  23. {({ data, loading, error }) => {
  24. if (loading) return <p>Loading interfaces</p>
  25. if (error) return <p>Error loading interfaces: {error.message}</p>
  26. const { interfaces } = data
  27. if (!interfaces.length) return <p>No interfaces found</p>
  28. return (
  29. <fieldset>
  30. {interfaces.map(iface => (
  31. <label key={iface.name} htmlFor='interface'>
  32. {iface.name}
  33. <input
  34. type='checkbox'
  35. name='interface'
  36. id='interface'
  37. value={iface.name}
  38. checked={state.includes(iface.name)}
  39. onChange={toState}
  40. />
  41. </label>
  42. ))}
  43. </fieldset>
  44. )
  45. }}
  46. </Query>
  47. )
  48. }
  49. const Interface = props => {
  50. const {
  51. workerScript,
  52. interfaceName,
  53. options,
  54. ports,
  55. connections
  56. } = props.data
  57. return (
  58. <div>
  59. <h2>{interfaceName}</h2>
  60. <p>Script:</p>
  61. <p>{workerScript.path}</p>
  62. <Gallery
  63. title='ports'
  64. items={ports.map(port => (
  65. <Port key={port.device} data={port} />
  66. ))}
  67. />
  68. <Gallery
  69. title='connections'
  70. items={connections.map(connection => (
  71. <Connection key={connection.id} data={connection} />
  72. ))}
  73. />
  74. <Gallery
  75. title='options'
  76. items={options.map(option => (
  77. <InterfaceOption key={option.name} data={option} />
  78. ))}
  79. />
  80. </div>
  81. )
  82. }
  83. export default Interface
  84. export { InterfaceFormFields }