ecency-mobile/src/containers/profileEditContainer.js

171 lines
4.0 KiB
JavaScript
Raw Normal View History

2019-09-04 22:17:39 +03:00
import { Component } from 'react';
2019-09-08 14:52:16 +03:00
import { Alert } from 'react-native';
2019-09-04 22:17:39 +03:00
import { connect } from 'react-redux';
2019-09-08 14:52:16 +03:00
import { injectIntl } from 'react-intl';
import ImagePicker from 'react-native-image-crop-picker';
import get from 'lodash/get';
2019-09-04 22:17:39 +03:00
2019-09-08 14:52:16 +03:00
import { uploadImage } from '../providers/esteem/esteem';
2019-09-04 22:17:39 +03:00
// import ROUTES from '../constants/routeNames';
2019-09-08 00:20:15 +03:00
const FORM_DATA = [
{
valueKey: 'name',
type: 'text',
label: 'display_name',
placeholder: '',
},
{
valueKey: 'about',
type: 'text',
label: 'about',
placeholder: '',
},
{
valueKey: 'location',
type: 'text',
label: 'location',
placeholder: '',
},
{
valueKey: 'website',
type: 'text',
label: 'website',
placeholder: '',
},
];
2019-09-04 22:17:39 +03:00
class ProfileEditContainer extends Component {
/* Props
* ------------------------------------------------
* @prop { type } name - Description....
*/
constructor(props) {
super(props);
2019-09-08 14:52:16 +03:00
this.state = {
isUploading: false,
about: get(props.currentAccount, 'about.profile.about'),
name: get(props.currentAccount, 'about.profile.name'),
location: get(props.currentAccount, 'about.profile.location'),
website: get(props.currentAccount, 'about.profile.website'),
coverUrl: get(props.currentAccount, 'about.profile.cover_image'),
avatarUrl: get(props.currentAccount, 'avatar'),
};
2019-09-04 22:17:39 +03:00
}
// Component Life Cycles
// Component Functions
_handleOnSave = () => {};
2019-09-08 14:52:16 +03:00
_handleOnItemChange = (val, item) => {
console.log(val, item);
};
_uploadImage = (media, action) => {
const { intl } = this.props;
uploadImage(media)
.then(res => {
if (res.data && res.data.url) {
this.setState({ [action]: res.data.url, isUploading: false });
}
})
.catch(error => {
if (error) {
Alert.alert(
intl.formatMessage({
id: 'alert.fail',
}),
error.message || error.toString(),
);
}
this.setState({ isUploading: false });
});
};
_handleMediaAction = (type, uploadAction) => {
if (type === 'camera') {
this._handleOpenCamera(uploadAction);
} else if (type === 'image') {
this._handleOpenImagePicker(uploadAction);
}
};
_handleOpenImagePicker = action => {
ImagePicker.openPicker({
includeBase64: true,
})
.then(image => {
this._handleMediaOnSelected(image, action);
})
.catch(e => {
this._handleMediaOnSelectFailure(e);
});
};
_handleOpenCamera = action => {
ImagePicker.openCamera({
includeBase64: true,
})
.then(image => {
this._handleMediaOnSelected(image, action);
})
.catch(e => {
this._handleMediaOnSelectFailure(e);
});
};
_handleMediaOnSelected = (media, action) => {
this.setState({ isUploading: true }, () => {
this._uploadImage(media, action);
});
};
_handleMediaOnSelectFailure = error => {
const { intl } = this.props;
if (get(error, 'code') === 'E_PERMISSION_MISSING') {
Alert.alert(
intl.formatMessage({
id: 'alert.permission_denied',
}),
intl.formatMessage({
id: 'alert.permission_text',
}),
);
}
};
2019-09-04 22:17:39 +03:00
render() {
2019-09-07 14:49:04 +03:00
const { children, currentAccount, isDarkTheme } = this.props;
2019-09-08 14:52:16 +03:00
const { isUploading, name, location, website, about, coverUrl, avatarUrl } = this.state;
2019-09-04 22:17:39 +03:00
return (
children &&
children({
currentAccount,
2019-09-07 14:49:04 +03:00
isDarkTheme,
2019-09-08 00:20:15 +03:00
formData: FORM_DATA,
2019-09-08 14:52:16 +03:00
isUploading,
handleMediaAction: this._handleMediaAction,
handleOnItemChange: this._handleOnItemChange,
name,
location,
website,
about,
coverUrl,
avatarUrl,
2019-09-04 22:17:39 +03:00
})
);
}
}
const mapStateToProps = state => ({
currentAccount: state.account.currentAccount,
2019-09-07 14:49:04 +03:00
isDarkTheme: state.application.isDarkTheme,
2019-09-04 22:17:39 +03:00
});
2019-09-08 14:52:16 +03:00
export default connect(mapStateToProps)(injectIntl(ProfileEditContainer));