Interface.js 2.4 KB

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