Guest User

Untitled

a guest
May 4th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component, isValidElement } from 'react';
  2. import {
  3.   SafeAreaView,
  4.   TouchableOpacity,
  5.   TouchableHighlight,
  6.   FlatList,
  7.   StyleSheet,
  8.   Text,
  9.   View,
  10.   Image,
  11. } from 'react-native';
  12. import firebase from 'react-native-firebase';
  13.  
  14. firebase.app();
  15.  
  16. class SympathyList extends Component {
  17.   constructor(props) {
  18.     super(props);
  19.     this.state = { chats: [] };
  20.     this.getChats = this.getChats.bind(this);
  21.   }
  22.  
  23.   componentDidMount() {
  24.     var _userId = firebase.auth().currentUser.uid;
  25.     this.getChats(_userId);
  26.   }
  27.  
  28.   getChats = _userId => {
  29.     let data;
  30.     var readedData = firebase
  31.       .database()
  32.       .ref('chats')
  33.       .orderByChild('members/' + _userId)
  34.       .equalTo(true);
  35.     readedData.once('value', snapshot => {
  36.       this.setState({ chats: snapshot.val() });
  37.       // console.log(JSON.stringify(this.state.chats));
  38.       data = snapshot.val();
  39.       delete data._userId;
  40.       delete data.members;
  41.       console.log(data);
  42.     });
  43.   };
  44.  
  45.   render() {
  46.     const { navigate } = this.props.navigation;
  47.     // console.log('chats: ' + this.state.chats);
  48.     let allChats = Object.keys(this.state.chats).map(key => key);
  49.     return <ListItems navigate={navigate} data={allChats} />;
  50.   }
  51. }
  52.  
  53. export default SympathyList;
  54.  
  55. function Item({ id, title, selected, onSelect, navigate, data }) {
  56.   return (
  57.     <View
  58.       style={{
  59.         // borderColor: 'gray',
  60.         borderBottomWidth: 0.75,
  61.         borderBottomColor: '#BFBFBF',
  62.         flexDirection: 'row',
  63.         backgroundColor: '#FFFFFF',
  64.         margin: 10,
  65.       }}
  66.     >
  67.       <Image
  68.         style={{ height: 50, width: 50, borderRadius: 10, marginBottom: 10 }}
  69.         source={{
  70.           uri: 'https://facebook.github.io/react/logo-og.png',
  71.         }}
  72.       />
  73.       <Text
  74.         style={{
  75.           padding: 12,
  76.           color: '#171717',
  77.           textTransform: 'capitalize',
  78.           fontFamily: 'Roboto',
  79.           fontStyle: 'normal',
  80.           fontWeight: 'bold',
  81.         }}
  82.       >
  83.         {title}
  84.       </Text>
  85.       {/* {console.log('Chat duomenys: ' + JSON.stringify(data))} */}
  86.  
  87.       <TouchableHighlight
  88.         style={{ right: 0, position: 'absolute' }}
  89.         title={data}
  90.         onPress={() => navigate('Chat', { name: 'Chat', data: data })}
  91.       >
  92.         <Text
  93.           style={{
  94.             color: 'white',
  95.             backgroundColor: 'orange',
  96.             height: 50,
  97.             alignContent: 'center',
  98.             padding: 12,
  99.             borderColor: '#182343',
  100.             borderWidth: 2,
  101.             borderRadius: 5,
  102.           }}
  103.         >
  104.           Chat
  105.         </Text>
  106.       </TouchableHighlight>
  107.     </View>
  108.   );
  109. }
  110.  
  111. const ListItems = props => {
  112.   const [selected, setSelected] = React.useState(new Map());
  113.  
  114.   const onSelect = React.useCallback(
  115.     id => {
  116.       const newSelected = new Map(selected);
  117.       newSelected.set(id, !selected.get(id));
  118.  
  119.       setSelected(newSelected);
  120.     },
  121.     [selected],
  122.   );
  123.  
  124.   // console.log(JSON.stringify(props.data));
  125.   return (
  126.     <SafeAreaView style={styles.container}>
  127.       <FlatList
  128.         data={props.data}
  129.         renderItem={({ item }) => (
  130.           <Item
  131.             id={item.id}
  132.             title={item}
  133.             selected={!!selected.get(item.id)}
  134.             onSelect={onSelect}
  135.             navigate={props.navigate}
  136.             data={item}
  137.             key={item}
  138.           />
  139.         )}
  140.         keyExtractor={item => item.id}
  141.         extraData={selected}
  142.       />
  143.     </SafeAreaView>
  144.   );
  145. };
  146.  
  147. const styles = StyleSheet.create({
  148.   container: {
  149.     flex: 1,
  150.     backgroundColor: '#FFFFFF',
  151.     height: '100%',
  152.     borderTopLeftRadius: 20,
  153.     borderTopRightRadius: 20,
  154.     // marginTop: Constants.statusBarHeight,
  155.   },
  156.   item: {
  157.     backgroundColor: '#f9c2ff',
  158.     padding: 20,
  159.     marginVertical: 8,
  160.     marginHorizontal: 16,
  161.   },
  162.   title: {
  163.     fontSize: 32,
  164.   },
  165. });
Add Comment
Please, Sign In to add comment