/**
* Edit Profile页面
* @邠心vbe on 2021/05/07
*/
import React, { Component } from 'react';
import { View, Text, StyleSheet, Pressable, Image, TextInput } from 'react-native';
import ImagePicker from 'react-native-image-crop-picker';
import Modal from 'react-native-modal';
import apiUpload from '../../api/apiUpload';
import apiUser from '../../api/apiUser';
import { host } from '../../api/http';
import { ModalProps } from '../../components/BottomModal';
import Button, { ViewHeight } from '../../components/Button';
import { CountryDropNum, GetCountryList } from '../../components/CountryIcon';
import Dropdown from '../../components/Dropdown';
import utils from '../../utils/utils';
import { DialogMaxWidth } from '../charge/InfoDialog';
import { PageList } from '../Router';
//Image Picker参数
const options = {
cropping: true,
multiple: false,
minFiles: 1,
maxFiles: 3,
mediaType: 'photo',
compressImageQuality: 0.8,
cropperCircleOverlay: true,
cropperStatusBarColor: colorAccent,
cropperToolbarColor: colorAccent,
cropperActiveWidgetColor: colorAccent
}
//向右的箭头
const ArrowRight = () => {
return (
);
}
//分割线
const Divide = () => {
return (
);
}
//编辑弹窗
const EditDialog = ({visible, title, value, keyType, countryList, areaNo, showCalling=false, onChangedText}) => {
var changedText = value;
var callingCode = areaNo;
return (
onChangedText('')}
onBackButtonPress={() => onChangedText('')}>
{title}
{ showCalling &&
{
callingCode = value;
}}
customerItemView={
(item, index, onClick) =>
}
/>
}
{
changedText = text;
}}/>
{/* {
onChangedText('');
}} /> */}
);
}
//页面
export default class EditProfile extends Component {
constructor(props) {
super(props);
this.state = {
editDialog: {
key: '',
value: '',
title: '',
visible: false
},
userInfo: {},
countryNums: []
};
this.editAddress = false;
}
componentDidMount() {
/*this.props.navigation.addListener('beforeRemove', e => {
if (this.canUpdate) {
e.preventDefault();
//console.log('----update---', userInfo);
}
});*/
this.props.navigation.addListener('focus', e => {
if (this.editAddress && userInfo.editAddress) {
this.updateProfile(global.userInfo);
}
this.editAddress = false;
});
this.setState({
userInfo: userInfo
})
GetCountryList(list => {
this.setState({
countryNums: list
})
})
}
updateProfile(_userInfo) {
apiUser.setProfile(_userInfo).then(res => {
toastShort('Update profile successfully');
userInfo = _userInfo;
this.setState({
userInfo: _userInfo
});
//this.canUpdate = false;
//goBack();
}).catch(err => {
//this.canUpdate = false;
toastShort(err);
});
}
hideDialog() {
this.state.editDialog.visible = false;
this.setState({
editDialog: this.state.editDialog
});
}
render() {
return (
{
ImagePicker.openPicker({
width: 200,
height: 200,
...options
}).then(image => {
if (image.path) {
apiUpload.uploadImage(image.path, image.mime, 'USER_PROFILE').then(res => {
if (res.success && res.data.picturePath) {
//this.canUpdate = true;
this.state.userInfo.photoUrl = res.data.picturePath
/*this.setState({
userInfo: userInfo
});*/
this.updateProfile(this.state.userInfo);
} else {
toastShort('Upload failed, please retry');
}
}).catch(err => {
toastShort(err);
});
}
}).catch(err => {
});
}}>
Avatar
{ userInfo.photoUrl
?
:
}
{
this.setState({
editDialog: {
key: 'nickName',
title: 'Update Nickname',
value: this.state.userInfo.nickName,
visible: true
}
});
}}>
Nickname
{this.state.userInfo.nickName}
{
this.setState({
editDialog: {
key: 'phone',
visible: true,
showCalling: true,
title: 'Update Phone Number',
value: this.state.userInfo.phone,
areaNo: this.state.userInfo.callingCode
}
});
}}>
Phone Number
{(utils.isNotEmpty(this.state.userInfo.callingCode) && "+" + this.state.userInfo.callingCode)} {this.state.userInfo.phone}
Email
{userInfo.email}
{/**/}
{
/*this.setState({
editDialog: {
key: 'addressLine',
title: 'Update Address',
value: userInfo.addressLine,
visible: true
}
});*/
this.editAddress = true;
startPage(PageList.editAddress);
}}>
Address
{userInfo.addressLine}
{
if (text) {
var info = Object.assign({}, userInfo);
if (this.state.editDialog.key == 'phone') {
if (!/^[0-9\+]\d{6,15}$/.test(text)) {
toastShort('Phone Number is incorrect format');
return;
}
info.callingCode = areaNo;
}
info[this.state.editDialog.key] = text;
/*this.setState({
userInfo: userInfo
});
this.canUpdate = true;*/
this.updateProfile(info);
}
this.hideDialog();
}}/>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white'
},
profileView: {
padding: 16,
alignItems: 'center',
flexDirection: 'row'
},
label: {
color: '#666',
fontSize: 14
},
infoText: {
flex: 1,
color: '#333',
fontSize: 14,
paddingLeft: 16,
textAlign: 'right'
},
avatar: {
width: 62,
height: 62,
borderRadius: 80,
},
dialog: {
width: DialogMaxWidth,
paddingTop: 16,
paddingLeft: 20,
paddingRight: 20,
paddingBottom: 16,
marginLeft: 'auto',
marginRight: 'auto',
backgroundColor: 'white',
borderRadius: isIOS ? 20 : 3
},
editNickname: {
color: '#000',
fontSize: 18,
textAlign: 'center'
},
nickInput: {
flex: 1,
color: '#333',
fontSize: 14,
paddingTop: 8,
paddingLeft: 16,
paddingRight: 16,
paddingBottom: 8,
borderRadius: 4,
marginTop: 24,
backgroundColor: '#F6F6F6'
},
modalButtons: {
marginTop: 24,
paddingBottom: 16,
alignItems: 'center',
flexDirection: 'row'
},
btnCancel: {
flex: 1,
borderWidth: 1,
borderColor: colorPrimary,
backgroundColor: 'white'
},
btnOk: {
flex: 1,
marginLeft: 16,
borderWidth: 1,
borderColor: colorPrimary,
},
closeIcon: {
top: 12,
right: 12,
position: "absolute"
},
countryView: {
width: 80,
marginTop: 24,
marginRight: 8,
minHeight: 43,
borderRadius: 4,
flexDirection: 'row',
backgroundColor: '#F6F6F6'
},
selectText: {
color: '#333',
fontSize: 15,
...$padding(11, 10),
},
});