From 6e9a031a75d383b9d28fc7c391b8b892df19fd64 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 12 Dec 2018 12:56:58 +0300 Subject: Upload avatar --- vnext/src/App.js | 2 +- vnext/src/api/index.js | 6 +++++ vnext/src/components/Settings.js | 58 +++++++++++++++++++++++++++++++--------- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/vnext/src/App.js b/vnext/src/App.js index 94b2ced5..5c9b3b81 100644 --- a/vnext/src/App.js +++ b/vnext/src/App.js @@ -155,7 +155,7 @@ export default class App extends React.Component { } /> - + } /> } /> } /> diff --git a/vnext/src/api/index.js b/vnext/src/api/index.js index ab26a359..9bb9d5a3 100644 --- a/vnext/src/api/index.js +++ b/vnext/src/api/index.js @@ -76,6 +76,12 @@ export function comment(mid, rid, body, attach) { return client.post('/comment', form); } +export function updateAvatar(newAvatar) { + let form = new FormData(); + form.append('avatar', newAvatar); + return client.post('/me/upload', form); +} + function socialLink(network) { return `${apiBaseUrl}/_${network}login?state=${window.location.protocol}//${window.location.host}${window.location.pathname}`; } diff --git a/vnext/src/components/Settings.js b/vnext/src/components/Settings.js index bf42180a..62115455 100644 --- a/vnext/src/components/Settings.js +++ b/vnext/src/components/Settings.js @@ -1,21 +1,27 @@ import React from 'react'; +import PropTypes from 'prop-types'; import update from 'immutability-helper'; -import { me } from '../api'; +import { me, updateAvatar } from '../api'; import { UserType } from './Types'; import Button from './Button'; import Icon from './Icon'; +import UploadButton from './UploadButton'; +import Avatar, { AvatarLink } from './Avatar'; export default class Settings extends React.Component { constructor(props) { super(props); this.state = { - settings: {}, + settings: { + newAvatar: '' + }, me: {} }; + this.avatarinput = React.createRef(); } componentDidMount() { me().then(visitor => { @@ -28,12 +34,18 @@ export default class Settings extends React.Component { if (event.preventDefault) { event.preventDefault(); } - console.log('avatar update'); + updateAvatar(this.avatarinput.current.files[0]).then(() => { + this.avatarChanged(''); + me().then(visitor => { + this.props.onChange(visitor); + }); + }); } passwordChanged = (event) => { - this.setState({ - settings: { password: event.target.value } + let newState = update(this.state, { + settings: { password: { $set: event.target.value } } }); + this.setState(newState); } onSubmitPassword = (event) => { if (event.preventDefault) { @@ -66,18 +78,39 @@ export default class Settings extends React.Component { disableTwitter = () => { console.log('twitter disable'); } + avatarChanged = (newAvatar) => { + let newState = update(this.state, { + settings: { newAvatar: { $set: newAvatar }, preview: { $set: '' } } + }); + this.setState(newState); + if (newAvatar) { + let reader = new FileReader(); + reader.onloadend = (preview) => { + let newState = update(this.state, { + settings: { preview: { $set: preview.target.result } } + }); + this.setState(newState); + }; + reader.readAsDataURL(this.avatarinput.current.files[0]); + } + } render() { - const { me } = this.state; + const { me, settings } = this.state; + let previewUser = { ...this.props.visitor, uname: '' }; + if (settings.preview) { + previewUser = { ...this.props.visitor, avatar: settings.preview, uname: '' }; + } return (
Changing your avatar
-

Avatar:
- Recommendations: PNG, 96x96, <50Kb. Also, JPG and GIF supported.

-

- -

+ Recommendations: PNG, 96x96, <50Kb. Also, JPG and GIF supported. + + + +
@@ -221,6 +254,7 @@ export default class Settings extends React.Component { } Settings.propTypes = { - visitor: UserType.isRequired + visitor: UserType.isRequired, + onChange: PropTypes.func.isRequired }; -- cgit v1.2.3