-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7ecf7d
commit 369b5fa
Showing
4 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React, { useState, useRef, useEffect } from 'react' | ||
import { useField } from '@rocketseat/unform' | ||
import api from '~/services/api' | ||
|
||
import { Container } from './styles' | ||
|
||
export default function AvatarInput() { | ||
const { defaultValue, registerField } = useField('avatar') | ||
|
||
const [file, setFile] = useState(defaultValue && defaultValue.id) | ||
const [preview, setPreview] = useState(defaultValue && defaultValue.url) | ||
|
||
const ref = useRef() | ||
useEffect(() => { | ||
if (ref.current) { | ||
registerField({ | ||
name: 'avatar_id', | ||
ref: ref.current, | ||
path: 'dataset.file', | ||
}) | ||
} | ||
}, [ref, registerField]) | ||
|
||
async function handleChange(e) { | ||
const data = new FormData() | ||
|
||
data.append('file', e.target.files[0]) | ||
|
||
const response = await api.post('files', data) | ||
|
||
const { id, url } = response.data | ||
|
||
setFile(id) | ||
setPreview(url) | ||
} | ||
|
||
return ( | ||
<Container> | ||
<label htmlFor="avatar"> | ||
<img | ||
src={ | ||
preview || 'https://api.adorable.io/avatars/50/abott@adorable.png' | ||
} | ||
alt="" | ||
/> | ||
<input | ||
type="file" | ||
id="avatar" | ||
accept="image/*" | ||
data-file={file} | ||
onChange={handleChange} | ||
ref={ref} | ||
/> | ||
</label> | ||
</Container> | ||
) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import styled from 'styled-components' | ||
|
||
export const Container = styled.div` | ||
align-self: center; | ||
margin-bottom: 30px; | ||
label { | ||
cursor: pointer; | ||
&:hover { | ||
opacity: 0.7; | ||
} | ||
img { | ||
height: 120px; | ||
width: 120px; | ||
border-radius: 50%; | ||
border: 3px solid rgba(255, 255, 255, 0.3); | ||
background: #eee; | ||
} | ||
input { | ||
display: none; | ||
} | ||
} | ||
` |
This file contains 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
This file contains 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