import React, {Component} from 'react' import {Alert, ScrollView, TouchableOpacity, View, Platform, KeyboardAvoidingView} from 'react-native' import Image from 'react-native-fast-image' import {CustomStepIndicator} from '../../components/StepIndicator' import {CustomInput} from '../../components/CustomInput' import {CustomButton} from '../../components/CustomButton' import Icon from 'src/components/Icon' import {bindActionCreators} from 'redux' import {appSetUser} from '../../redux/app/action' import {connect} from 'react-redux' import {Picker} from 'native-base' import moment from 'moment' import {BackgroundImage_RegisterForm} from '../../components/BackgroundImage_RegisterForm' import LinearGradient from 'react-native-linear-gradient' import Text from '../../components/Text' import {getBuildingByProjectId, project} from '../../api/UserApi' 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('cancel') ] let CANCEL_INDEX = 2 class RegisterFormLoginScreen extends Component { constructor(props) { super(props) this.state = { user: props.user, project_list: [], building_list: [], selected_project: 0, project_name: t('choose_project'), image_url: require('../../../assets/images/profile.png'), enableSummit: false, checkFullname: false, checkCitizenID: false, checkBirthDate: null, checkRoomNo: false, is_customer: true } this._onNextStep = this._onNextStep.bind() this.onImagePick = this.onImagePick.bind(this) this.onTakeCamera = this.onTakeCamera.bind(this) } componentDidMount() { this.getProjectList() this.setDefaultUserData() } setDefaultUserData() { this.setState({ user: { ...this.state.user, gender: 'ชาย' } }) } getProjectList() { project() .then(res => { if (res.ok) { return this.setState({ project_list: res.data.data, selected_project: res.data.data[0].id }) } }) .then(res => { // this.getBuildingList() }) } getBuildingList() { getBuildingByProjectId(this.state.selected_project) .then(res => { // console.log('-------------------> ', res) if (res.ok) { this.setState({ building_list: res.data.data, user: { //set default building index 0 ...this.state.user, building: res.data.data[0].room_build } }) } }) } _onNextStep = async () => { // if (this.state.user.email && this.state.user.mobile) { // let tax_id = this.state.user && this.state.user.tax_id // if(!tax_id && !this.state.is_customer){ // tax_id = (Math.floor(1000000000000 + Math.random() * 9000000000000)).toString(); // } if (this.state.user.mobile // && this.state.is_customer && this.state.user.tax_id && (this.state.user.tax_id.toString()).length > 0 // && tax_id.length > 0 && this.state.user.name) { /*var email_validate = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ if (email_validate.test(this.state.user.email) === true) { this.props.appSetUser(this.state.user) this.props.navigation.navigate('RegisterProfile') } else { Alert.alert(null, 'ท่านกรอกรูปแบบอีเมลไม่ถูกต้อง') }*/ console.log('check user data next <<<<<<<< ', this.state.user); this.setState({ user: { ...this.state.user, // tax_id: !this.state.is_customer ? Math.floor(1000000000000 + Math.random() * 9000000000000) : this.state.user.tax_id, // is_customer: this.state.is_customer } }, () => { // console.log('user >>> ', this.state.user) this.props.appSetUser(this.state.user) // next page !! this.props.navigation.navigate('RegisterProfile', {image_url: this.state.image_url}) }) } else { console.log('กรุณากรอกข้อมูลให้ครบถ้วน') Alert.alert(null, 'กรุณากรอกข้อมูลให้ครบถ้วน') } } 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', } this.setState({ image_url: {uri: `data:${image.mime};base64,` + image.data, width: image.width, height: image.height}, images: null, user: { ...this.state.user, profile_image_id: image_profile } }) }).catch(e => alert(e)) } onTakeCamera() { ImagePicker.openCamera({ cropping: true, width: 300, height: 300, includeExif: true }).then(image => { console.log('received image', image) 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 } }) }).catch(e => alert(e)) } validateNextStepBtn(){ if (this.state.checkFullname.length > 0 && this.state.user.project && this.state.user.building && this.state.checkRoomNo.length > 0) { this.setState({ enableSummit: true }); } else { this.setState({ enableSummit: false }); } } handleRequired = (e, type) => { // e = value switch (type) { case 'fullname': // code block this.setState(() => ({['checkFullname']: e}), () => { this.validateNextStepBtn() }); break; case 'room': // code block this.setState(() => ({['checkRoomNo']: e}), () => { this.validateNextStepBtn() }); break; /*case 'citizen_id': this.setState(() => ({['checkCitizenID']: e})); break;*/ default: this.validateNextStepBtn() // code block } // console.log(this.state.user.birth_date); // check all } render() { return ( { 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() } }) }} > {/* LOGO */} {t('profile')} { let input = e.nativeEvent.text this.setState({ user: { ...this.state.user, name: input, } }) }} value={this.state.user.name} onChangeText={(evt) => { this.handleRequired(evt, "fullname") this.setState({ user:{ ...this.state.user, name: evt.replace(/\u0020/, '\u00a0') } }) }} placeholder={t('specific_fullname')} placeholderTextColor={'#FFFFFF40'} /> {/* { let input = e.nativeEvent.text this.setState({ user: { ...this.state.user, email: input } }) }} textContentType={'emailAddress'} placeholder={'ระบุอีเมล'} placeholderTextColor={'#FFFFFF40'} /> */} {/* {t('gender')} { this.setState({ user: { ...this.state.user, gender: e } }) }} > */} {/* /// */} {/* { this.setState({ user: { ...this.state.user, birth_date: moment(data).format('YYYY-MM-DD'), birth_date_show: moment(data).format('MM/DD/YYYY'), text_birth: moment(data).add(543, 'years').format('DD-MM-YYYY') } }, () => { this.handleRequired(data, "birth_date"); }) }}/> */} { let input = e.nativeEvent.text this.setState({ user: { ...this.state.user, mobile: input } }) }} placeholder={t('phone')} placeholderTextColor={'#FFFFFF40'} value={this.state.user.mobile || ''} /> {/* {this.setState({ is_customer: true }, () => { this.handleRequired(this.state.is_customer, "user_type"); }) }} > { this.state.is_customer ? : } {t('customer')} { this.setState({ is_customer: false, user: { ...this.state.user, tax_id: '', building: '', room: '' }, checkCitizenID: false }, () => { this.handleRequired(this.state.is_customer, "user_type"); }) }} > { this.state.is_customer ? : } {t('guest')} */} {/*{ this.state.is_customer && { let input = e.nativeEvent.text this.setState({ user: { ...this.state.user, tax_id: input } }) }} onChangeText={(evt) => this.handleRequired(evt, "citizen_id")} placeholder={t('citizen_id_13')} placeholderTextColor={'#FFFFFF40'} /> }*/} {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, project_name: e.name }, }, () => { // console.log('check project selected --------> ',this.state.project_name); this.handleRequired(e, "project") this.getBuildingList() }) }} > {this.state.project_list.map((item) => { return })} { this.state.is_customer && {t('building')} { this.setState({ user: { ...this.state.user, building: e } }, () => { this.handleRequired(e, "building") }) }} > {this.state.building_list.map((item) => { return })} { // let input = e.nativeEvent.text this.setState({ user: { ...this.state.user, room: e } }, () => { this.handleRequired(e, "room") }) }} placeholder={t('room_no')} placeholderTextColor={'#FFFFFF40'} /> } ) } } const styles = { container: { flex: 1, flexGrow: 1, flexDirection: 'column' }, logo: { flex: 1, backgroundColor: '#3AA40C', 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: { backgroundColor: '#3AA40C', flex: 1 }, buttons: { flexDirection: 'row', marginTop: 15, marginBottom: 0, bottom: 10 }, row_grow: { flexGrow: 1 }, row_grow_first: { flexGrow: 1, }, headerTitle: { color: '#8BC34A', marginVertical: 5, marginTop: 10 }, btn_next_register: { opacity: 1, backgroundColor: '#145EB3' }, register_input: { backgroundColor: 'rgba(255,255,255,0.3)', color: 'red', fontFamily: 'Prompt-Regular', borderRadius: 30 }, disableNext: { opacity: 0.65, cursor: "not-allowed", backgroundColor: '#AAAAAA' }, enableNext: { opacity: 1, color: '#FFFFFF', backgroundColor: '#145EB3' }, userTypeContainer: { flex: 1, flexDirection: 'row', paddingTop: 5, paddingBottom: 10 }, radioInput: { flex: 1, flexDirection: 'row', alignItems: 'center' }, custom_button_container: { // flex:1, // flexDirection: 'row', height: 35, abckgroundColor: '#FF9500', borderRadius: 5, width: '100%', justifyContent: 'center', alignItems: 'center', }, icon: { justifyContent: 'center', // alignItems: '', // flex: 1, paddingLeft: 5, paddingRight: 5 }, } const mapDisPatchToProps = state => { return state.app } const setUser = dispatch => bindActionCreators({appSetUser}, dispatch) export default connect( mapDisPatchToProps, setUser )(RegisterFormLoginScreen)