Created
September 19, 2018 09:00
-
-
Save ihorkatkov/1a510264a5a2f946359ff73abb046565 to your computer and use it in GitHub Desktop.
Example with file uploader component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import { graphql } from 'react-apollo' | |
import gql from 'graphql-tag' | |
import { pipe, prop } from 'ramda' | |
type FileInputProps = { | |
label: string, | |
onChange: Function, | |
required: boolean, | |
dataCy: string, | |
} | |
const FileInput = (props: FileInputProps) => ( | |
<div className="up-pic-btn-container upload-btn"> | |
<label> | |
<input type="file" {...props} data-cy={props.dataCy} /> | |
{props.label} | |
</label> | |
</div> | |
) | |
type SingleUploaderProps = { | |
mutate: Function, | |
onUpload: Function, | |
label: string, | |
} | |
const SingleUploader = (props: SingleUploaderProps) => { | |
const handleChange = ({ target }) => | |
target.validity.valid && | |
props | |
.mutate({ | |
variables: { file: target.files[0] }, | |
}) | |
.then( | |
pipe( | |
prop('data'), | |
prop('uploadPhoto'), | |
props.onUpload, | |
), | |
) | |
return <FileInput required onChange={handleChange} {...props} /> | |
} | |
const query = gql` | |
mutation($file: Upload!) { | |
uploadPhoto(photo: $file) { | |
id | |
url | |
} | |
} | |
` | |
export default graphql(query)(SingleUploader) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment