Advertisement
ridwanfirdaus96

Untitled

Jul 21st, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from "react";
  2. import {
  3.   View,
  4.   Text,
  5.   TextInput,
  6.   Button,
  7.   StyleSheet,
  8.   ScrollView,
  9.   Image
  10. } from "react-native";
  11. import { connect } from "react-redux";
  12.  
  13. import { addPlace } from "../../store/actions/index";
  14. import PlaceInput from "../../components/PlaceInput/PlaceInput";
  15. import MainText from "../../components/UI/MainText/MainText";
  16. import HeadingText from "../../components/UI/HeadingText/HeadingText";
  17. import PickImage from "../../components/PickImage/PickImage";
  18. import PickLocation from "../../components/PickLocation/PickLocation";
  19. import validate from "../../utility/validation";
  20.  
  21. class SharePlaceScreen extends Component {
  22.   static navigatorStyle = {
  23.     navBarButtonColor: "orange"
  24.   };
  25.  
  26.   state = {
  27.     controls: {
  28.       placeName: {
  29.         value: "",
  30.         valid: false,
  31.         touched: false,
  32.         validationRules: {
  33.           notEmpty: true
  34.         }
  35.       },
  36.       location: {
  37.         value: null,
  38.         valid: false
  39.       }
  40.     }
  41.   };
  42.  
  43.   constructor(props) {
  44.     super(props);
  45.     this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent);
  46.   }
  47.  
  48.   onNavigatorEvent = event => {
  49.     if (event.type === "NavBarButtonPress") {
  50.       if (event.id === "sideDrawerToggle") {
  51.         this.props.navigator.toggleDrawer({
  52.           side: "left"
  53.         });
  54.       }
  55.     }
  56.   };
  57.  
  58.   placeNameChangedHandler = val => {
  59.     this.setState(prevState => {
  60.       return {
  61.         controls: {
  62.           ...prevState.controls,
  63.           placeName: {
  64.             ...prevState.controls.placeName,
  65.             value: val,
  66.             valid: validate(val, prevState.controls.placeName.validationRules),
  67.             touched: true
  68.           }
  69.         }
  70.       };
  71.     });
  72.   };
  73.  
  74.   locationPickedHandler = location => {
  75.     this.setState(prevState => {
  76.       return {
  77.         controls: {
  78.           ...prevState.controls,
  79.           location: {
  80.             value: location,
  81.             valid: true
  82.           }
  83.         }
  84.       };
  85.     });
  86.   };
  87.  
  88.   placeAddedHandler = () => {
  89.     this.props.onAddPlace(
  90.       this.state.controls.placeName.value,
  91.       this.state.controls.location.value
  92.     );
  93.   };
  94.  
  95.   render() {
  96.     return (
  97.       <ScrollView>
  98.         <View style={styles.container}>
  99.           <MainText>
  100.             <HeadingText></HeadingText>
  101.           </MainText>
  102.           <PickImage />
  103.           <PickLocation onLocationPick={this.locationPickedHandler} />
  104.           <PlaceInput
  105.             placeData={this.state.controls.placeName}
  106.             onChangeText={this.placeNameChangedHandler}
  107.           />
  108.           <View style={styles.button}>
  109.             <Button
  110.               title="Bagikan Tempat"
  111.               onPress={this.placeAddedHandler}
  112.               disabled={
  113.                 !this.state.controls.placeName.valid ||
  114.                 !this.state.controls.location.valid
  115.               }
  116.             />
  117.           </View>
  118.         </View>
  119.       </ScrollView>
  120.     );
  121.   }
  122. }
  123.  
  124. const styles = StyleSheet.create({
  125.   container: {
  126.     flex: 1,
  127.     alignItems: "center"
  128.   },
  129.   placeholder: {
  130.     borderWidth: 1,
  131.     borderColor: "black",
  132.     backgroundColor: "#eee",
  133.     width: "80%",
  134.     height: 150
  135.   },
  136.   button: {
  137.     margin: 8
  138.   },
  139.   previewImage: {
  140.     width: "100%",
  141.     height: "100%"
  142.   }
  143. });
  144.  
  145. const mapDispatchToProps = dispatch => {
  146.   return {
  147.     onAddPlace: (placeName, location) => dispatch(addPlace(placeName, location))
  148.   };
  149. };
  150.  
  151. export default connect(null, mapDispatchToProps)(SharePlaceScreen);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement