ProjectForm.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import styled from 'styled-components'
  2. import gql from 'graphql-tag'
  3. import { Mutation, Query } from 'react-apollo'
  4. import { FileFields, FileState, uploadFile } from './FileUpload'
  5. import { ProjectVersionFields, ProjectVersionState } from './ProjectVersionForm'
  6. const CREATE_PROJECT = gql`
  7. mutation CREATE_PROJECT($name: String!, $abbreviation: String!, $description: String) {
  8. createProject(name: $name, abbreviation: $abbreviation, description: $description) {
  9. id
  10. }
  11. }
  12. `
  13. const QUERY_PROJECTS = gql`
  14. query QUERY_PROJECTS {
  15. projects {
  16. id
  17. name
  18. abbreviation
  19. description
  20. files {
  21. id
  22. path
  23. name
  24. description
  25. filename
  26. mimetype
  27. size
  28. }
  29. versions {
  30. id
  31. name
  32. changes
  33. date
  34. }
  35. }
  36. }
  37. `
  38. const ProjectSelector = props => (
  39. <Query query={QUERY_PROJECTS}>
  40. {({ data, loading, error }) => {
  41. if (error) return (<p>Error loading project: ${error.message}</p>)
  42. if (loading) return (<p>Loading data...</p>)
  43. if (!data || !data.projects.length) return (<p>No project found.</p>)
  44. if (!props.value) props.onChange({ target: { name: 'project', value: data.projects[0].id } })
  45. const selector = (
  46. <select {...props}>
  47. {data.projects.map(project =>
  48. <option key={project.id} value={project.id}>{project.name}</option>
  49. )}
  50. </select>
  51. )
  52. return selector
  53. }}
  54. </Query >
  55. )
  56. const ProjectFields = props => (
  57. <fieldset>
  58. {props.title && <legend>{props.title}</legend>}
  59. <label htmlFor='name'>Project name</label>
  60. <input type='text' name='name' id='name' placeholder='Project name' value={props.state.name} onChange={props.onChange} />
  61. <label htmlFor='abbreviation'>Project abbreviation</label>
  62. <input type='text' name='abbreviation' id='abbreviation' placeholder='Project abbreviation' value={props.state.abbreviation} onChange={props.onChange} />
  63. <label htmlFor='description'>Project description</label>
  64. <textarea name='description' id='description' placeholder='Project description' value={props.state.description} onChange={props.onChange} />
  65. </fieldset>
  66. )
  67. class Project extends React.Component {
  68. state = {
  69. id: null,
  70. name: '',
  71. abbreviation: '',
  72. description: '',
  73. files: [],
  74. versions: [],
  75. ...this.props.project
  76. }
  77. toState = event => {
  78. this.setState({ [event.target.name]: event.target.value })
  79. }
  80. render() {
  81. return (
  82. <Mutation mutation={CREATE_PROJECT} variables={this.state} refetchQueries={[{ query: QUERY_PROJECTS }]}>
  83. {(createProject, { data, error, loading }) => (
  84. <form onSubmit={async event => {
  85. event.preventDefault()
  86. const { data } = await createProject()
  87. this.state.id = data.createProject.id
  88. }}>
  89. <ProjectFields title="Project" state={this.state} onChange={this.toState} />
  90. {this.state.versions.map(version =>
  91. <ProjectVersionFields title="Project version" state={version} onChange={this.toState} />
  92. )}
  93. <button onClick={event => this.state.versions.push(ProjectVersionState)}>Add project version</button>
  94. {this.state.files.map(file =>
  95. <FileFields state={file} onChange={this.toState} />
  96. )}
  97. <button onClick={event => this.state.files.push(FileState)}>Add file</button>
  98. <button type='submit'>{this.state.id && this.state.id !== "__NEW__" ? "Save" : "Add"}</button>
  99. </form>
  100. )}
  101. </Mutation>
  102. )
  103. }
  104. }
  105. export default Project
  106. export { ProjectSelector, QUERY_PROJECTS, ProjectFields }