import React, {Component} from 'react' import {Alert, ScrollView, StyleSheet, TextInput, View, TouchableOpacity, KeyboardAvoidingView, Platform} from 'react-native' import Image from 'react-native-fast-image' import Icon from "../../components/Icon"; import {Button} from "native-base"; import {BackgroundImage} from '../../components/BackgroundImage' import Text from '../../components/Text'; import LinearGradient from 'react-native-linear-gradient'; import GetWidthHeightDevice from '../../components/GetWidthHeightDevice' import ActionSheet from "react-native-action-sheet"; import ImagePicker from 'react-native-image-crop-picker' import {postSuggestion, postSuggestionLogin} from '../../api/UserApi'; import { connect } from "react-redux"; import { t } from 'src/utils/i18n' import IndicatorLoading from '../../components/IndicatorLoading'; let ACTIONSHEET_BUTTONS = [ 'ถ่ายรูป', 'รูปจากแกลอรี่', t('cancel') ] let CANCEL_INDEX = 2 class SuggestionScreen extends Component { constructor(props) { super(props); this.state = { suggestionText: null, image_list: [], isShowAddBtn: true, image_selected: {}, isLoading: false, suggestionTitle: null }; this.onImagePick = this.onImagePick.bind(this) this.onTakeCamera = this.onTakeCamera.bind(this) this.sendDataSuggestion = this.sendDataSuggestion.bind(this) } sendDataSuggestion() { this.setState({ isLoading: true }, () => { let param = { message: this.state.suggestionText, image: this.state.image_selected, title: this.state.suggestionTitle } console.log("param",param); if(this.props.user == null && param.message != null && param.title != null && param.title.trim() !== ""){ postSuggestion(param) .then(res => { console.log('check response suggestion -----------> ',res) Alert.alert('รับข้อเสนอแนะเรียบร้อย', 'ทางเราได้รับคำแนะนำและเรื่องไว้พิจารณาแล้ว ขอบคุณสำหรับความร่วมมือค่ะ',[{text: t('ok'), onPress:() => { this.setState({ isLoading: false }, () => { this.props.navigation.goBack() }) }}]) }) }else if(this.props.user != null && param.message != null && param.title != null && param.title.trim() !== ""){ postSuggestionLogin(param) .then(res => { console.log('check response suggestion -----------> ',res) Alert.alert('รับข้อเสนอแนะเรียบร้อย', 'ทางเราได้รับคำแนะนำและเรื่องไว้พิจารณาแล้ว ขอบคุณสำหรับความร่วมมือค่ะ',[{text: t('ok'), onPress:() => { this.setState({ isLoading: false }, () => { this.props.navigation.goBack() }) }}]) }) }else{ console.log('suggestion fails') this.setState({ isLoading: false }, () => { Alert.alert('ส่งข้อเสนอแนะไม่สำเร็จ', 'กรุณากรอกข้อมูลให้ครบถ้วน',[{text: t('ok')}]) }) } }) } 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, }, () => { this.setState({ image_list: this.state.image_list.concat(this.state.image_url), image_selected: image_profile, isShowAddBtn: false }) }) }).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, }, () => { this.setState({ image_list: this.state.image_list.concat(this.state.image_url), image_selected: image_profile, isShowAddBtn: false }) }) }).catch(e => { // alert(e) }) } render() { return ( {t('title')} this.setState({suggestionTitle: text})} value={this.state.suggestionTitle}/> {t('suggestion')} this.setState({suggestionText: text})}/> {t('photo')} { this.state.isShowAddBtn && { ActionSheet.showActionSheetWithOptions({ options: ACTIONSHEET_BUTTONS, cancelButtonIndex: CANCEL_INDEX, }, (buttonIndex) => { console.log('button clicked :', buttonIndex) if (buttonIndex == 0) { this.onTakeCamera() } else if (buttonIndex == 1) { this.onImagePick() } }) }}> } { this.state.image_list.map((item, index) => { return { this.setState({ image_list: [ ...this.state.image_list.slice(0, index), ...this.state.image_list.slice(index + 1) ], isShowAddBtn: true }) }}> }) } ) } } const styles = StyleSheet.create({ container: { flex: 1, flexDirection: 'column', height: GetWidthHeightDevice.HeightContainer, //backgroundColor: '#FAFAFA' }, formGroup: { marginHorizontal: 15, marginVertical: 5, backgroundColor: '#ffffff', width: '93%', height: 40, borderRadius: 5, display: 'flex', flexDirection: 'row' }, textInput: { backgroundColor: 'white', paddingVertical: 15, borderRadius: 5, borderWidth: 1, borderColor: 'rgba(0, 0, 0, 0.25)', paddingHorizontal: 10, marginHorizontal: 10, }, textArea: { backgroundColor: 'white', marginHorizontal: 10, paddingHorizontal: 10, marginVertical: 10, paddingVertical: 15, height: 100, textAlignVertical: 'top', borderRadius: 5, borderWidth: 1, borderColor: 'rgba(0, 0, 0, 0.25)' }, squareShapeView: { width: 64, height: 64, borderRadius: 5, justifyContent: 'center', alignItems: 'center', backgroundColor: 'rgba(0, 0, 0, 0.25)', margin: 10 } }); const mapDisPatchToProps = state => ({ user: state.app.user }) export default connect(mapDisPatchToProps)(SuggestionScreen)