ProjectForm.js 2.8 KB

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