Add multiple file upload component

This commit is contained in:
Michael Merrill 2017-04-03 12:37:32 -06:00
parent 91896891ad
commit e2420c2a63
2 changed files with 45 additions and 3 deletions

View File

@ -0,0 +1,31 @@
import {Component} from 'react'
import {graphql, gql} from 'react-apollo'
class MultiUploader extends Component {
handleChange = ({target}) => {
if (target.validity.valid) {
this.props
.mutate({
variables: {
files: target.files
}
})
.then(({data}) => console.log('Mutation response:', data))
}
}
render () {
return <input type='file' accept={'image/jpeg,image/png'} multiple required onChange={this.handleChange} />
}
}
export default graphql(gql`
mutation multiUpload ($files: [Upload!]) {
multiUpload (files: $files) {
name
type
size
path
}
}
`)(MultiUploader)

View File

@ -1,6 +1,7 @@
import Head from 'next/head'
import withData from '../helpers/with-data'
import SingleUploader from '../components/single-uploader'
import MultiUploader from '../components/multi-uploader'
export default withData(props => (
<div>
@ -13,10 +14,20 @@ export default withData(props => (
body {
margin: 2em;
}
section {
height: 50vh;
}
`}</style>
</Head>
<h1>Apollo upload example</h1>
<p>Select an image to upload and view the response in the console.</p>
<SingleUploader />
<section>
<h1>Apollo upload example (single file)</h1>
<p>Select an image to upload and view the response in the console.</p>
<SingleUploader />
</section>
<section>
<h1>Apollo upload example (multiple files)</h1>
<p>Select multiple images to upload and view the response in the console.</p>
<MultiUploader />
</section>
</div>
))