import React, { Component } from 'react' import { View, ScrollView, TouchableOpacity, Platform, KeyboardAvoidingView, Alert } from 'react-native' import Image from 'react-native-fast-image' import Icon from '../../components/Icon' import { Picker } from 'native-base' import { BackgroundImage_RegisterForm } from '../../components/BackgroundImage_RegisterForm' import LinearGradient from 'react-native-linear-gradient' import Text from '../../components/Text' import { CustomInput } from '../../components/CustomInput' import { CustomButton } from '../../components/CustomButton' import { CustomPicker } from '../../components/CustomPicker' import GetWidthHeightDevice from '../../components/GetWidthHeightDevice' import { bindActionCreators } from 'redux' import { appSetDevice, appSetUser, appCleanUser } from '../../redux/app/action' import { connect } from 'react-redux' import { edit_profile, project, getBuildingByProjectId } from '../../api/UserApi' import moment from 'moment' import ImagePicker from 'react-native-image-crop-picker' import ActionSheet from 'react-native-action-sheet' import CustomDatePicker from '../room/CustomDatePicker'; import { t } from '../../utils/i18n' let ACTIONSHEET_BUTTONS = [ t('take_photo'), t('from_gallery'), t('cancel') ] // let DESTRUCTIVE_INDEX = 3; let CANCEL_INDEX = 2 class EditProfileScreen extends Component { constructor (props) { super(props) this.edit_profile = this.edit_profile.bind(this) this.state = { user: {}, image_url: require('../../../assets/images/profile.png'), project_list:[], building_list: [], selected_project: 0, project_name:t('choose_project'), } this.onImagePick = this.onImagePick.bind(this) this.onTakeCamera = this.onTakeCamera.bind(this) } componentDidMount () { const { navigation } = this.props const users_state = navigation.getParam('user', 'NO ITEM') const image = navigation.getParam('image', 'NO ITEM') console.log("defaultImage",image); if(image != null ){ this.setState({ image_url :image }) } // console.log('check user state ----------- ',users_state) if(users_state.gender == null || users_state.gender == 'null'){ console.log('NULL'); // this.state.user.show_birth_date = users_state.birth_date; this.setState({ user: { gender: 'male', name: users_state.name, ...users_state, } }) }else{ console.log('NOT NULL'); this.setState({ user: { ...users_state, name: users_state.name } }) } // this.getProjectList() } getProjectList() { project() .then(res => { if (res.ok && res.data && res.data.data) { this.setState({ project_list: res.data.data, selected_project: res.data.data[0].id }) } }) } getBuildingList() { getBuildingByProjectId(this.state.selected_project) .then(res => { if (res.ok) { this.setState({ building_list: res.data.data }) } }) } edit_profile () { if(this.state.user.password != null || this.state.user.password != ''){ if(this.state.user.password === this.state.user.confirm_password){ edit_profile(this.state.user) .then(res => { if(res.ok){ this.props.appSetUser(res.data) Alert.alert(t('save_success'), '', [{ text: 'ok', onPress: () => { this.props.navigation.navigate('Profile') } }]) }else { Alert.alert(t('save_fail_and_try'), '', [{ text: 'ok', onPress: () => { this.props.navigation.navigate('Profile') } }]) } }) }else{ Alert.alert(t('incorrect_pw'), t('pw_not_match_and_retry')) } }else{ edit_profile(this.state.user) .then(res => { if(res.ok){ this.props.appSetUser(res.data) Alert.alert(t('save_success'), '', [{ text: 'ok', onPress: () => { this.props.navigation.navigate('Profile') } }]) }else { Alert.alert(t('save_fail_and_try'), '', [{ text: 'ok', onPress: () => { this.props.navigation.navigate('Profile') } }]) } }) } } onImagePick () { ImagePicker.openPicker({ width: 300, height: 300, cropping: true, includeBase64: true }).then(image => { console.log('received base64 image', image) let image_profile = { uri: image.path, type: image.mime, name: 'image_profile.jpg', } let image_data = { uri: `data:${image.mime};base64,` + image.data, width: image.width, height: image.height }; this.setState({ image_url: image_profile, images: null, user: { ...this.state.user, profile_image_id: image_profile, profile_image: image_data } }) }) } onTakeCamera () { ImagePicker.openCamera({ cropping: true, width: 300, height: 300, includeExif: true, includeBase64: true }).then(image => { console.log('received image', image) let image_data = { uri: `data:${image.mime};base64,` + image.data, width: image.width, height: image.height }; let image_profile = { uri: image.path, type: image.mime, name: 'image_profile.jpg', } this.setState({ image_url: { uri: image.path, width: image.width, height: image.height, mime: image.mime }, images: null, user: { ...this.state.user, profile_image_id: image_profile, profile_image: image_data } }) }) } render () { const { navigation } = this.props const users = navigation.getParam('user', 'NO ITEM') // console.log('users :', users); return ( {this.scrollView = view;}} > { // this.onImagePick() ActionSheet.showActionSheetWithOptions({ options: ACTIONSHEET_BUTTONS, cancelButtonIndex: CANCEL_INDEX, // destructiveButtonIndex: DESTRUCTIVE_INDEX, // tintColor: 'blue' }, (buttonIndex) => { // console.log('button clicked :', buttonIndex) if (buttonIndex == 0) { this.onTakeCamera() } else if (buttonIndex == 1) { this.onImagePick() } }) }}> {t('profile')} { this.setState({ user: { ...this.state.user, name: text, name_th: text } }) }}/> { this.setState({ user: { ...this.state.user, email: text } }) }}/> {/* {t('gender')} { // console.log('check gender +++++++++++++++ ',e); this.setState({ user: { ...this.state.user, gender: e } }) }} > */} {/* { this.setState({ user: { ...this.state.user, birth_date: moment(data).format('YYYY-MM-DD'), show_birth_date: moment(data).format('YYYY-MM-DD') } })}}/> */} {/* { this.setState({ user: { ...this.state.user, tax_id: text } }) }}/> */} { this.setState({ user: { ...this.state.user, mobile: text } }) }}/> {/*{t('accom_info')}*/} {/* {t('project')} { // console.log('@@@@@@ project selected --------> ',e); this.setState({ project_name: e.name, project_item:e, selected_project: e.id, user: { ...this.state.user, project: e.code, }, }, () => { this.getBuildingList() }) }} > {this.state.project_list.map((item,index) => { return })} */} {/* {t('building')} { this.setState({ user: { ...this.state.user, building: e } }) }}> {this.state.building_list.map((item,index) => { return })} { this.setState({ user: { ...this.state.user, room: text } }) }}/> */} {t('change_password')} { this.setState({ user: { ...this.state.user, password: text } }) }}/> { this.setState({ user: { ...this.state.user, confirm_password: text } }) }}/> ) } } const styles = { container: { flex: 1, flexDirection: 'column', height: GetWidthHeightDevice.HeightDevice, }, logo: { height: '20%', flexDirection: 'row', justifyContent: 'center', alignItems: 'center', }, form: { flex: 1, padding: 15, paddingTop: 5, paddingBottom: 0, }, row: { flexDirection: 'row', marginBottom: 5, }, form_address: { padding: 10, paddingBottom: 0, }, step_indicator: { }, buttons: { flexDirection: 'row', marginTop: 15, bottom: 10 }, row_grow: { flexGrow: 1, }, row_grow_first: { flexGrow: 1, }, headerTitle: { color: '#8BC34A', marginVertical: 5, marginTop: 10 }, btn_next_register: { backgroundColor: '#145EB3' }, register_input: { backgroundColor: 'rgba(255,255,255,0.3)', borderRadius: 30, } }; const mapDisPatchToProps = state => { return state.app }; const setUser = dispatch => bindActionCreators({ appSetDevice, appSetUser, appCleanUser }, dispatch) export default connect(mapDisPatchToProps, setUser)(EditProfileScreen)