1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import React from 'react'
- import gql from 'graphql-tag'
- import { endpoint } from '../config'
- /*import { Query } from 'react-apollo'
- const QUERY_UPLOAD = gql`
- query QUERY_UPLOAD($id: ID!) {
- upload(id: $id) {
- id
- filename
- path
- description
- name
- size
- }
- }
- `*/
- class FileUpload extends React.Component {
- state = {
- path: ''
- }
- upload = async event => {
- event.preventDefault()
- const { validity, files } = event.target
- const data = new FormData()
- data.append('file', files[0])
- const res = await fetch(`${endpoint}/upload`, {
- method: 'POST',
- body: data
- })
- const { error, file } = await res.json()
- if (error) throw new Error('Failed to upload.')
- console.log(file)
- this.setState({ path: `${endpoint}/${file.path}` })
- }
- render() {
- return (
- <form>
- <input type='file' name='file' id='file' onChange={this.upload} />
- <img src={this.state.path} alt={this.state.name} />
- </form>
- )
- }
- }
- export default FileUpload
|