Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/avatar #3

Open
wants to merge 3 commits into
base: dev-main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"normalize.css": "^4.1.1",
"postcss-loader": "^0.13.0",
"react": "15.4.0",
"react-avatar-cropper": "^0.1.3",
"react-dom": "^15.0.0",
"react-redux": "^4.4.5",
"react-router": "3.0.0",
Expand Down
56 changes: 53 additions & 3 deletions src/containers/profile/profilePageContent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FormattedMessage } from 'react-intl';
import autobind from 'autobind-decorator';
import { SubmissionError } from 'redux-form';

import AvatarForm from 'forms/profile/avatarForm.jsx';
import BasicInfoForm from 'forms/profile/basicInfoForm.jsx';
import AddressForm from 'forms/profile/addressForm.jsx';
import PersonalTagForm from 'forms/profile/personalTagForm.jsx';
Expand Down Expand Up @@ -46,14 +47,21 @@ class ProfilePageContent extends React.Component {

state = {
isExpanded: {
avatarForm: true,
basicInfoForm: true,
addressForm: true,
personalTagForm: true
},
basicInforFormInitialValues: null,
addressFormInitialValues: null,
personalTagFormInitialValues: null
}
personalTagFormInitialValues: null,
avatarFormInitialValues: {
last: null,
cropperOpen: false,
img: null,
croppedImage: 'http://ww3.sinaimg.cn/mw690/6f6fe5a7jw1e5oococe8lj20c80lrab6.jpg'
}
};

componentWillMount() {
this.componentWillReceiveProps(this.props);
Expand Down Expand Up @@ -98,7 +106,7 @@ class ProfilePageContent extends React.Component {
addressFormInitialValues: newAddressInitialValues
});
}
}
};

@autobind
updateAddress(formData) {
Expand Down Expand Up @@ -138,6 +146,40 @@ class ProfilePageContent extends React.Component {
console.log(geoCode);
}

@autobind
avatarHandleFileChange(dataURI) {
const avatarConfig = this.state.avatarFormInitialValues;
avatarConfig.cropperOpen = true;
avatarConfig.img = dataURI;
if (avatarConfig.last) {
avatarConfig.croppedImage = avatarConfig.last;
}
this.setState({
avatarFormInitialValues: avatarConfig
});
}

@autobind
avatarHandleCrop(dataURI) {
this.setState({
avatarFormInitialValues: {
last: dataURI,
cropperOpen: false,
img: null,
croppedImage: dataURI
}
});
}

@autobind
avatarHandleRequestHide() {
const temp = this.state.avatarFormInitialValues;
temp.cropperOpen = false;
this.setState({
avatarFormInitialValues: temp
});
}

@autobind
expandForm(form) {
const newState = this.state.isExpanded;
Expand All @@ -152,6 +194,14 @@ class ProfilePageContent extends React.Component {
return (
<div className="profile-content">
<div className="profile-content__container">
<div className="profile-content__form-group">
<FormGroupHeader expandForm={this.expandForm} formName="avatarForm" />
{
isExpanded.avatarForm ?
<AvatarForm handleFileChange={this.avatarHandleFileChange} handleCrop={this.avatarHandleCrop} handleRequestHide={this.avatarHandleRequestHide} initialValues={this.state.avatarFormInitialValues} /> :
null
}
</div>
<div className="profile-content__form-group">
<FormGroupHeader expandForm={this.expandForm} formName="basicInfoForm" />
{
Expand Down
56 changes: 56 additions & 0 deletions src/forms/profile/avatarForm.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import AvatarCropper from 'react-avatar-cropper';
import React from 'react';
import FileUpload from 'utils/avatarUpload.jsx';
import autobind from 'autobind-decorator';

class AvatarForm extends React.Component {

static propTypes = {
handleFileChange: React.PropTypes.func.isRequired,
handleCrop: React.PropTypes.func.isRequired,
handleRequestHide: React.PropTypes.func.isRequired,
initialValues: React.PropTypes.object.isRequired
};

@autobind
handleFileChange(dataURI) {
this.props.handleFileChange(dataURI);
}

@autobind
handleCrop(dataURI) {
this.props.handleCrop(dataURI);
}

@autobind
handleRequestHide() {
this.props.handleRequestHide();
}

render() {
const { initialValues } = this.props;
return (
<div>
<div className="avatar-photo">
<FileUpload handleFileChange={this.handleFileChange} />
<div className="avatar-edit">
<span>Click to pick avatar</span>
</div>
<img src={initialValues.croppedImage} />
</div>
{initialValues.cropperOpen &&
<AvatarCropper
onRequestHide={this.handleRequestHide}
cropperOpen={initialValues.cropperOpen}
onCrop={this.handleCrop}
image={initialValues.img}
width={400}
height={400}
/>
}
</div>
);
}
};

export default AvatarForm;
1 change: 1 addition & 0 deletions src/routes/restrictedRoute.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const connectDispatch = (dispatch) => ({
});

const unrestrcitedPathNames = [
'/profile/info',
'/home',
'/error'
];
Expand Down
41 changes: 41 additions & 0 deletions src/utils/avatarUpload.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Created by 50528 on 1/7/2017.
*/
import React from 'react';
import ReactDom from 'react-dom';
import autobind from 'autobind-decorator';

class FileUpload extends React.Component {
static propTypes = {
handleFileChange: React.PropTypes.func.isRequired
};

// constructor(props) {
// super(props);
// this.node = null;
// }
@autobind
handleFile(e) {
const reader = new FileReader();
const file = e.target.files[0];

if (!file) {
return;
}

reader.onload = function(img) {
ReactDom.findDOMNode(this.refs.in).value = '';
this.props.handleFileChange(img.target.result);
//console.log(this.node);
}.bind(this);
reader.readAsDataURL(file);
}

render() {
return (
<input ref="in" type="file" accept="image/*" onChange={this.handleFile} />
);
}
}

export default FileUpload;