Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { BackHandler,AsyncStorage,Alert } from 'react-native';
  3. import UpdateScreenn from "../views/UpdateScreenView";
  4. import {responseData,updateHandler} from '../models/updateBridge';
  5. import ImagePicker from 'react-native-image-picker';
  6. import Crop from 'react-native-image-crop-picker';
  7. export var username1 = null;
  8. const options = {
  9.     title: "Select a Photo",
  10.     takePhotoButtonTitle: "Take a Photo",
  11.     chooseFromLibraryButtonTitle: "Choose from gallery",
  12.     quality: 1
  13. }
  14.  
  15. export default class UpdateScreen extends React.Component{
  16.     constructor(props) {
  17.         super(props)
  18.         this.state = {
  19.             username : '',
  20.             email: '',
  21.             password: '',
  22.             uri: '',
  23.             newProf:'',
  24.             type: '',
  25.             name: '',
  26.             user_id: '',
  27.             modalVisibility: false,
  28.             imageSource: null,
  29.             image: null
  30.         }
  31.         this.handleBackButtonClick = this.handleBackButtonClick.bind(this);
  32.     }
  33.     showModal = (visibility) => {
  34.         this.setState({modalVisibility : visibility})
  35.     }
  36.     clearAsyncStorage = async() => {
  37.         AsyncStorage.clear();
  38.     }
  39.     // selectImage = () => {
  40.     //     ImagePicker.clean().then(() => {
  41.     //         console.log("img temp cleaned")
  42.     //     }).catch(e => {
  43.     //         alert(e)
  44.     //     });
  45.     //     ImagePicker.openPicker({
  46.     //         width: 720,
  47.     //         height: 720,
  48.     //         compressImageMaxWidth: 720,
  49.     //         compressImageMaxHeight: 720,
  50.     //         cropping: true,
  51.     //         cropperCircleOverlay: true,
  52.     //         mediaType: 'photo'
  53.     //     }).then(image => {
  54.     //         console.log(image);
  55.     //         let source = { uri: image.path }
  56.     //         let pathParts = image.path.split('/');
  57.     //         const data = new FormData();
  58.     //         data.append('image', {
  59.     //             uri: image.path,
  60.     //             type: image.mime,
  61.     //             name: pathParts[pathParts.length - 1]
  62.     //         })
  63.     //         this.setState({
  64.     //             imageSource: source,
  65.     //             image: data
  66.     //         })
  67.     //     });
  68.     //     this.showModal(false);
  69.     // }
  70.     // takeCamera = () => {
  71.     //     ImagePicker.clean().then(() => {
  72.     //         console.log("img temp cleaned")
  73.     //     }).catch(e => {
  74.     //         alert(e)
  75.     //     });
  76.     //     ImagePicker.openCamera({
  77.     //         width: 720,
  78.     //         height: 720,
  79.     //         compressImageMaxWidth: 720,
  80.     //         compressImageMaxHeight: 720,
  81.     //         cropping: true,
  82.     //         cropperCircleOverlay: true,
  83.     //         mediaType: 'photo'
  84.     //     }).then(image => {
  85.     //         console.log(image);
  86.     //         let source = { uri: image.path }
  87.     //         let pathParts = image.path.split('/');
  88.     //         const data = new FormData();
  89.     //         data.append('image', {
  90.     //             uri: image.path,
  91.     //             type: image.mime,
  92.     //             name: pathParts[pathParts.length - 1]
  93.     //         })
  94.     //         this.setState({
  95.     //             imageSource: source,
  96.     //             image: data
  97.     //         })
  98.     //     });
  99.     //     this.showModal(false);
  100.     // }
  101.     componentWillMount() {
  102.         BackHandler.addEventListener('hardwareBackPress', this.handleBackButtonClick);
  103.         AsyncStorage.getItem("email",(error,result)=>{
  104.             this.setState({
  105.                 email : result
  106.             })
  107.                  
  108.         })
  109.         AsyncStorage.getItem("username",(error,result)=>{
  110.             this.setState({
  111.                 username : result
  112.             })            
  113.         })
  114.         AsyncStorage.getItem("imageProf",(error,result)=>{
  115.             this.setState({
  116.                 imageSource : result
  117.             })            
  118.         })
  119.         AsyncStorage.getItem("user_id",(error,result)=>{
  120.             this.setState({
  121.                 user_id : result
  122.             })            
  123.         })
  124.         username1 = this.state.username;
  125.     }
  126.    
  127.     componentWillUnmount() {
  128.         BackHandler.removeEventListener('hardwareBackPress', this.handleBackButtonClick);
  129.        
  130.     }
  131.    
  132.     handleBackButtonClick() {
  133.         this.props.navigation.goBack(null);
  134.         return true;
  135.     }
  136.  
  137.     async selectPhoto(){
  138.         Crop.clean().then(() => {
  139.             console.log("img temp cleaned")
  140.         }).catch(e => {
  141.             alert(e)
  142.         });
  143.         ImagePicker.showImagePicker(options, (response) => {
  144.             console.log("Response = ", response);
  145.  
  146.             if(response.didCancel)  console.log("User cancelled image picker");
  147.             else if(response.error) console.log("ImagePicker Error: ", response.error);
  148.             else {
  149.                 let sources = { uri : response.uri }
  150.                 Crop.openCropper({
  151.                 path: response.uri,
  152.                 width: 600,
  153.                 height: 600,
  154.                 compressImageMaxWidth: 600,
  155.                 compressImageMaxHeight: 600,
  156.                 compressImageQuality: 0.8,
  157.                 cropping: true,
  158.                 cropperCircleOverlay: true,
  159.                 mediaType: 'photo'
  160.             }).then(image =>{
  161.                 console.log(image);
  162.                 let source = { uri: image.path }
  163.                 let pathParts = image.path.split('/');
  164.                 // const imageData = {
  165.                 // uri: image.path,
  166.                 // type: image.mime,
  167.                 // name: pathParts[pathParts.length - 1]
  168.                 // }
  169.                 this.setState({
  170.                     imageSource: source,
  171.                     uri : image.path,
  172.                     type: image.mime,
  173.                     name: pathParts[pathParts.length - 1]
  174.                 })
  175.                
  176.                 // this.setState({
  177.                 // imageSource: source,
  178.                 // image: data
  179.        
  180.             })
  181.                 // this.setState({
  182.                 //     imageSource: source,
  183.                 //     uri : response.uri,
  184.                 //     type : response.type,
  185.                 //     name : response.fileName
  186.                 // })
  187.  
  188.                
  189.                
  190.             }
  191.         })
  192.      
  193.     }
  194.     saveData(){
  195.         AsyncStorage.setItem('status', this.state.status, ( error ) => {
  196.             if( error ){
  197.                 console.error( error );
  198.             }
  199.         });
  200.         AsyncStorage.setItem('email', this.state.email, ( error ) => {
  201.             if( error ){
  202.                 console.error( error );
  203.             }
  204.         });
  205.         AsyncStorage.setItem('imageProf', this.state.newProf, ( error ) => {
  206.             if( error ){
  207.                 console.error( error );
  208.             }
  209.         });
  210.         AsyncStorage.setItem('username', this.state.username, ( error ) => {
  211.             if( error ){
  212.                 console.error( error );
  213.             }
  214.         });
  215.         AsyncStorage.setItem('user_id', this.state.user_id, ( error ) => {
  216.             if( error ){
  217.                 console.error( error );
  218.             }
  219.         });
  220.        
  221.     }
  222.     sendData = async () => {
  223.         const data = new FormData();
  224.         data.append('user_id', this.state.user_id)
  225.         data.append('name', this.state.username )
  226.         data.append('password', this.state.password)
  227.         data.append('email', this.state.email)
  228.         data.append('image',{
  229.             uri     : this.state.uri,
  230.             type    : this.state.type,
  231.             name    : this.state.name,
  232.            
  233.        
  234.            
  235.         },)
  236.         console.log(data);
  237.         // this.setState({
  238.         //     _isLoading : true
  239.         // })
  240.         await updateHandler(data).then(res => {
  241.             if(responseData != null){ console.log("Update Succes");
  242.             this.setState({
  243.                 _isLoading : false
  244.             })
  245.             AsyncStorage.clear();
  246.             this.setState({
  247.                 status : "Yeah",
  248.                 username : responseData.data.username,
  249.                 email : responseData.data.email,
  250.                 newProf : responseData.data.imageProfil,
  251.                 gender : responseData.data.gender,
  252.                 user_id: responseData.data.id_user
  253.                
  254.             })
  255.             this.saveData();
  256.             Alert.alert('Upload Succes');
  257.             this.props.navigation.push("Home");
  258.         }else{
  259.             Alert.alert('Gagal')
  260.         }
  261.    
  262.     })
  263.  
  264.     }
  265.     Moleh = async () => {
  266.         this.props.navigation.push("Home");
  267.     }
  268.     setUsername = (usnam) =>{
  269.         this.setState({
  270.             username : usnam
  271.         })
  272.     }
  273.     setEmail = (imel) =>{
  274.         this.setState({
  275.             email : imel
  276.         })
  277.     }
  278.     setPassw = (pass) =>{
  279.         this.setState({
  280.             password : pass
  281.         })
  282.     }
  283.  
  284.  
  285.     render = () => {
  286.         return <UpdateScreenn
  287.         _isLoading={this.state._isLoading}
  288.         sendData={()=> this.sendData()}
  289.         onPress={()=> this.Moleh()}
  290.         nameChange = {
  291.             name => this.setUsername(name)
  292.         }
  293.         emailChange = {
  294.             email => this.setEmail(email)
  295.         }
  296.         passwordChange = {
  297.             password => this.setPassw(password)
  298.         }
  299.        
  300.         username = {this.state.username}
  301.         email = {this.state.email}
  302.         modalVisibility={this.state.modalVisibility}
  303.         openModal={() => this.showModal(true)}
  304.         closeModal={() => this.showModal(false)}
  305.         openPicker={() => this.selectImage()}
  306.         openCamera={() => this.selectPhoto()}
  307.         image={this.state.imageSource}
  308.         />
  309.     }
  310. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement