Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component, useState } from 'react';
  2. import { Actions } from 'react-native-router-flux';
  3. import { SafeAreaView, AsyncStorage, StyleSheet, Image, Text, View, TouchableOpacity, Modal, FlatList } from 'react-native';
  4. import Constants from 'expo-constants';
  5. import { Ionicons } from '@expo/vector-icons';
  6.  
  7. function Item({ marca, targa, index, allData, jwt }) {
  8.  
  9.   const [modalVisible, setModalVisible] = useState(false);
  10.   const [encData, setEncData] = useState('');
  11.  
  12.   console.log(jwt);
  13.   console.log(allData);
  14.  
  15.   const content = {
  16.     data: allData
  17.   }
  18.  
  19.   fetch('https://example.com/api/encrypt', {
  20.     method: 'POST',
  21.     body: JSON.stringify(content),
  22.     headers: {
  23.       'Content-Type': 'application/json; charset=utf-8',
  24.       'authorization': jwt
  25.     }
  26.   })
  27.   .then(response => response.json())
  28.   .then(res => setEncData(res.message))
  29.   .catch(err => console.log(err));
  30.  
  31.   return (
  32.     <React.Fragment>
  33.       <Modal
  34.         animationType='fade'
  35.         transparent={false}
  36.         visible={modalVisible}
  37.         onRequestClose={() => {
  38.           Alert.alert('Modal has been closed.');
  39.         }}
  40.       >
  41.         <View style={{flex:1, justifyContent: 'center', alignItems: 'center' }}>
  42.           <View style={styles.modalInsideView}>                  
  43.             <View style={{bottom: 50}}>
  44.               <Text style={[styles.buttonText, styles.medium]}>{marca}   <Text style={styles.buttonText}>-   {targa}</Text></Text>
  45.             </View>
  46.            
  47.             <Text>{JSON.stringify(encData)}</Text>
  48.            
  49.             <View style={{alignItems: 'center', justifyContent: 'center'}}>
  50.               <TouchableOpacity style={[styles.button, {backgroundColor: '#00b0ff'}]} onPress={ () => setModalVisible(!modalVisible) }>
  51.                 <Ionicons
  52.                   name={'ios-close'}
  53.                   size={40}
  54.                   style={{ color: 'white' }}
  55.                 />
  56.               </TouchableOpacity>
  57.             </View>
  58.           </View>
  59.         </View>
  60.       </Modal>
  61.       <TouchableOpacity
  62.         style={[styles.infoVehicle, {marginTop: index === 1 ? 10 : 18}]}
  63.         onPress={ () => setModalVisible(true) }>
  64.         <View style={{flexDirection:'row', alignItems: 'stretch', alignItems: 'center', justifyContent: 'space-between'}}>
  65.           <View style={{}}>
  66.             <Text style={[styles.buttonText, styles.medium]}>{marca}   <Text style={styles.buttonText}>-   {targa}</Text></Text>
  67.           </View>
  68.           <View style={{}}>
  69.             <Image
  70.               style={{width: 40, height: 40, opacity: 0.5}}
  71.               source={require('../../images/example.png')}
  72.             />
  73.           </View>
  74.         </View>
  75.       </TouchableOpacity>
  76.     </React.Fragment>
  77.   );
  78. }
  79.  
  80. export default class Pages extends Component {
  81.  
  82.   constructor(props) {
  83.     super(props);
  84.     this.state = {
  85.       jwt: null,
  86.       username: null,
  87.       myList: [],
  88.       showModal: false,
  89.     }
  90.   }
  91.  
  92.   async componentDidMount() {
  93.     try {
  94.       this.setState({ jwt: await AsyncStorage.getItem('jwt') });
  95.       this.setState({ username: await AsyncStorage.getItem('username') });
  96.       this.setState({ myList: JSON.parse(await AsyncStorage.getItem('myList')) });
  97.     }
  98.     catch(err) {
  99.       console.log(err);
  100.     }
  101.   }
  102.  
  103.   componentWillUpdate() {
  104.    
  105.     try {
  106.       this.setState({ myList: JSON.parse(await AsyncStorage.getItem('myList')) });
  107.     }
  108.     catch(err) {
  109.       console.log(err);
  110.     }
  111.   }
  112.  
  113.   toggleModal = (value) => {
  114.     this.setState({ showModal: value });
  115.   }
  116.  
  117.   render() {
  118.  
  119.     const { myList } = this.state;
  120.  
  121.     return (
  122.       <SafeAreaView style={{flex: 1, backgroundColor: 'white', padding: 20}}>
  123.        
  124.         {
  125.           myList.length >= 1 ?
  126.             <FlatList
  127.               data={myList}
  128.               renderItem={
  129.                 ({ item, index }) =>
  130.                   <Item
  131.                     index={index}
  132.                     marca={item.vehicle.marca}
  133.                     targa={item.vehicle.targa}
  134.                     allData={item}
  135.                     jwt={this.state.jwt}
  136.                   />
  137.               }
  138.               keyExtractor={item => item.vehicle.targa}
  139.               extraData={this.props.dataUpdate}
  140.             />
  141.           :
  142.           <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
  143.             <Text style={styles.noVehicle}>Non hai veicoli salvati</Text>
  144.           </View>
  145.          
  146.         }
  147.  
  148.         <View style={{paddingBottom: 40, alignItems: 'center', justifyContent: 'center'}}>
  149.           <TouchableOpacity style={styles.button} onPress={ () => Actions.qrScanner() }>
  150.             <Ionicons
  151.               name={'ios-add'}
  152.               size={40}
  153.               style={{ color: 'white' }}
  154.             />
  155.           </TouchableOpacity>
  156.         </View>
  157.       </SafeAreaView>
  158.     )
  159.   }
  160. }
  161.  
  162. const styles = StyleSheet.create({
  163.   container: {
  164.     flex: 1,
  165.     marginTop: Constants.statusBarHeight,
  166.     backgroundColor: 'white',
  167.   },
  168.   scroll: {
  169.     height: '75%',
  170.     flexGrow: 1,
  171.     paddingVertical: 30
  172.   },
  173.   infoVehicle: {
  174.     marginBottom: 10,
  175.     height: 70,
  176.     width: '100%',
  177.     backgroundColor: 'white',
  178.     justifyContent: 'center',
  179.     borderRadius: 5,
  180.     borderWidth: 0.6,
  181.     borderColor: '#b3b3b5',
  182.     padding: 10
  183.   },
  184.   button: {
  185.     height: 60,
  186.     width: 60,
  187.     alignItems: 'center',
  188.     alignContent: 'center',
  189.     backgroundColor: '#7fc297',
  190.     justifyContent: 'center',
  191.     borderRadius: 60,
  192.     borderWidth: 0.6,
  193.     borderColor: '#7fc297'
  194.   },
  195.   buttonText: {
  196.     color: 'white',
  197.     fontSize: 40,
  198.     fontFamily: 'RobotoRegular',
  199.     alignItems: 'center',
  200.     textAlign: 'center',
  201.     justifyContent: 'center',
  202.   },
  203.   noVehicle: {
  204.     fontSize: 20,
  205.     fontFamily: 'RobotoRegular',
  206.     textAlign: 'center',
  207.     textAlignVertical: 'center',
  208.     justifyContent: 'center',
  209.     alignContent: 'center',
  210.   },
  211.   buttonText: {
  212.     color: 'black',
  213.     fontSize: 20,
  214.     fontFamily: 'RobotoThin',
  215.   },
  216.   medium: {
  217.     fontFamily: 'RobotoMedium',
  218.   },
  219.   modalInsideView: {
  220.     flex: 1,
  221.     justifyContent: 'center',
  222.     alignItems: 'center',
  223.   },
  224.   containerButtonClose: {
  225.     top: 50,
  226.     textAlign: 'center',
  227.     backgroundColor: '#00b0ff',
  228.     borderRadius: 70,
  229.     height: 70,
  230.     width: 70,
  231.     justifyContent: 'center',
  232.   },
  233. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement