InstrumentInstance.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import gql from 'graphql-tag'
  2. import { Query, Mutation } from 'react-apollo'
  3. const CREATE_INSTRUMENT_INSTANCE = gql`
  4. mutation CREATE_INSTRUMENT_INSTANCE($name: String!, $description: String!, $interfaces: [String]!) {
  5. createInstrument(name: $name, description: $description, interfaces: $interfaces) {
  6. id
  7. }
  8. }
  9. `
  10. class InstrumentInstance extends React.Component {
  11. state = {
  12. id: this.props.instrumentInstance ? this.props.instrumentInstance.id : null,
  13. instrument: null,
  14. identifier: '',
  15. interface: '',
  16. label: '',
  17. location: ''
  18. }
  19. toState = event => {
  20. this.setState({ [event.target.name]: event.target.value })
  21. }
  22. addComment = event => {
  23. event.preventDefault()
  24. const newState = { ...this.state }
  25. newState.changes.push(this.state.change)
  26. newState.change = ''
  27. this.setState(newState)
  28. }
  29. render() {
  30. return (
  31. <Mutation mutation={CREATE_PROJECT_VERSION} variables={this.state}>
  32. {(createProjectVersion, { data, error, loading }) => (
  33. <form onSubmit={async event => {
  34. event.preventDefault()
  35. const { data } = await createProjectVersion()
  36. this.state.id = data.createProjectVersion.id
  37. }}>
  38. {!this.props.title && <h1>Project Version</h1>}
  39. <fieldset id='project-generic'>
  40. <label htmlFor='name'>Project name</label>
  41. <input type='text' name='name' id='name' placeholder='Project version name' value={this.state.name} onChange={this.toState} />
  42. <label htmlFor='date'>Date</label>
  43. <input type='date' name='date' id='date' placeholder='Project date' value={this.state.date} onChange={this.toState} />
  44. <label htmlFor='change'>Comments</label>
  45. <input type='text' name='change' id='change' placeholder='Project change' value={this.state.change} onChange={this.toState} /><button onClick={this.addComment}>Add</button>
  46. {this.state.changes.map((change, index) => <p key={index}>{change}</p>)}
  47. {this.props.project || (
  48. <Query query={QUERY_PROJECTS}>
  49. {({ data, error, loading }) => {
  50. if (loading) return <p>Loading projects...</p>
  51. if (error) return <p>Error: {error.message}</p>
  52. if (!data || !data.projects.length) return <p>No projects found.</p>
  53. if (!this.state.project) this.setState({ project: data.projects[0].id })
  54. return (
  55. <label htmlFor="version">
  56. <select name="version" id="version">onChange={this.toState}>
  57. {data.projects.map(project =>
  58. <option key={project.id} value={project.id}>{project.name}</option>)
  59. }
  60. </select>
  61. </label>
  62. )
  63. }}
  64. </Query>
  65. )}
  66. </fieldset>
  67. <button type='submit'>Save</button>
  68. </form>
  69. )}
  70. </Mutation>
  71. )
  72. }
  73. }
  74. export default InstrumentInstance