import React, { useState } from 'react'; import AccountCircleIcon from '@material-ui/icons/AccountCircle'; import Button from '@material-ui/core/Button'; import { bindActionCreators, Dispatch } from 'redux'; import { connect } from 'react-redux'; // @ts-ignore import styles from './Profile.module.scss'; import TextField from '@material-ui/core/TextField'; import classNames from 'classnames'; import { uploadAvatar, updateUsername } from '../../reducers/auth/service'; import { useTranslation } from 'react-i18next'; const mapDispatchToProps = (dispatch: Dispatch): any => ({ uploadAvatar: bindActionCreators(uploadAvatar, dispatch), updateUsername: bindActionCreators(updateUsername, dispatch) }); interface Props { avatarUrl: string; uploadAvatar?: typeof uploadAvatar; updateUsername?: typeof updateUsername; auth: any; } const UserProfile = (props: Props): any => { const { auth } = props; const user = auth.get('user'); const { t } = useTranslation(); const [file, setFile] = useState({}); const [fileUrl, setFileUrl] = useState(''); const [username, setUsername] = useState(user.name); const handleChange = (e: any): void => { const efile = e.target.files[0]; const formData = new FormData(); if (efile != null) { formData.append('file', efile, efile.type); formData.append('name', efile.name); formData.append('type', 'user-thumbnail'); const file = formData; setFile(file); setFileUrl(efile); } else { setFile({}); setFileUrl(''); } }; const handleSubmit = async (): Promise => { await props.uploadAvatar(file); }; const handleUsernameChange = (e: any): void => { const name = e.target.value; setUsername(name); }; const updateUsername = async (): Promise => { await props.updateUsername(user.id, username); }; return (
handleUsernameChange(e)} />
{t('user:profile.userIcon.userId')}: {user.id}
{fileUrl ? ( ) : props.avatarUrl ? ( ) : ( )}
); }; export default connect(null, mapDispatchToProps)(UserProfile);