Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react'
  2. import { Text, View, Image, FlatList, Dimensions, ActivityIndicator, StyleSheet } from 'react-native'
  3. import { Container, CardItem, Card, Left, Fab, Button, Icon } from 'native-base'
  4. import { withNavigation } from 'react-navigation'
  5. import Axios from 'axios';
  6. import AsyncStorage from '@react-native-community/async-storage';
  7. import { ScrollView } from 'react-native-gesture-handler';
  8. import Header1 from '../component/Header1';
  9.  
  10. const styles = StyleSheet.create({
  11.     button1: {
  12.         marginVertical: 10,
  13.         marginHorizontal: 10,
  14.         alignItems: 'center',
  15.         justifyContent: 'center',
  16.         width: Dimensions.get('window').width * 1 / 5,
  17.         backgroundColor: '#ddfffb',
  18.     },
  19.     text1: {
  20.         color: '#ffffff',
  21.  
  22.     }
  23.  
  24. })
  25.  
  26. class Home extends Component {
  27.     constructor(props) {
  28.         super(props)
  29.         this.state = {
  30.             data: [],
  31.             isLogin: '',
  32.             page: 1,
  33.             refreshing: false,
  34.             loading: false,
  35.             category: '',
  36.             isHidden: false
  37.         }
  38.     }
  39.  
  40.     getDataStory = () => {
  41.         try {
  42.             const getStory = async () => await Axios.get(`https://glints-redu.herokuapp.com/api/stories/paginate?page=${this.state.page}`, {
  43.                 headers: {
  44.                     'Content-Type': 'application/json',
  45.                 }
  46.             })
  47.             this.setState({ loading: true });
  48.  
  49.             getStory()
  50.                 .then(res => {
  51.                     this.setState({
  52.                         data: [...this.state.data, ...res.data.data.docs],
  53.                         refreshing: false,
  54.                         loading: false
  55.                     })
  56.                 })
  57.                 .catch(err => {
  58.                 })
  59.         } catch (e) { }
  60.     }
  61.  
  62.     renderFooter = () => {
  63.         if (!this.state.loading) return null;
  64.  
  65.         return (
  66.             <View
  67.                 style={{
  68.                     paddingVertical: 20,
  69.                     borderTopWidth: 1,
  70.                     borderColor: "#CED0CE"
  71.                 }}
  72.             >
  73.                 <ActivityIndicator animating size="large" />
  74.             </View>
  75.         );
  76.     }
  77.  
  78.     componentDidMount() {
  79.         this.getDataStory()
  80.         console.log(this.state.page)
  81.         this.userIsSignIn()
  82.         setInterval(this.userIsSignIn, 1000)
  83.     }
  84.  
  85.     handleEnd = () => {
  86.         this.setState(state => ({ page: state.page + 1 }), () => this.getDataStory());
  87.     };
  88.  
  89.     userIsSignIn = async () => {
  90.         const data = await AsyncStorage.getItem('@token')
  91.         this.setState({ isLogin: data })
  92.     }
  93.  
  94.     handleRefresh = () => {
  95.         this.setState(
  96.             {
  97.                 page: 1,
  98.                 refreshing: true,
  99.                 data: []
  100.             },
  101.             () => {
  102.                 this.getDataStory();
  103.             }
  104.         );
  105.     };
  106.  
  107.     hideView = () => { this.setState({ isHidden: true }) }
  108.  
  109.     revealView = () => { this.setState({ isHidden: false }) }
  110.  
  111.     render() {
  112.  
  113.         if (this.state.isLogin !== 'null') {
  114.             return (
  115.                 <Container style={{ backgroundColor: '#faffff' }}>
  116.                     <Header1 />
  117.                     <ScrollView
  118.                         onScroll={this.hideView}
  119.                         horizontal={true}
  120.                         style={{
  121.                             marginVertical: 10,
  122.                         }}
  123.                     >
  124.                         <Button
  125.                             small
  126.                             bordered
  127.                             rounded
  128.                             style={styles.button1
  129.                             }
  130.                             onPress={() => this.props.navigation.navigate('StoryByCategory', { category: 'hutan' })}>
  131.                             <Text>Hutan</Text>
  132.                         </Button>
  133.                         <Button
  134.                             small
  135.                             bordered
  136.                             rounded
  137.                             style={styles.button1
  138.                             }
  139.                             onPress={() => this.props.navigation.navigate('StoryByCategory', { category: 'kota' })}>
  140.                             <Text>Kota</Text></Button>
  141.                         <Button
  142.                             small
  143.                             bordered
  144.                             rounded
  145.                             style={styles.button1
  146.                             }
  147.                             onPress={() => this.props.navigation.navigate('StoryByCategory', { category: 'pantai' })}>
  148.                             <Text>Pantai</Text></Button>
  149.                         <Button
  150.                             small
  151.                             bordered
  152.                             rounded
  153.                             style={styles.button1
  154.                             }
  155.                             onPress={() => this.props.navigation.navigate('StoryByCategory', { category: 'sejarah' })}>
  156.                             <Text>Sejarah</Text></Button>
  157.                         <Button
  158.                             small
  159.                             bordered
  160.                             rounded
  161.                             style={styles.button1
  162.                             }
  163.                             onPress={() => this.props.navigation.navigate('StoryByCategory', { category: 'kota' })}>
  164.                             <Text>Agama</Text></Button>
  165.                     </ScrollView>
  166.                     <FlatList
  167.                         data={this.state.data}
  168.                         renderItem={({ item }) => (
  169.                             <View
  170.                                 key={item._id}
  171.                                 style={{ alignItems: 'center', justifyContent: 'center' }}>
  172.                                 <Card
  173.                                     style={{
  174.                                         marginVertical: 10,
  175.                                         borderRadius: 200 / 12,
  176.                                         width: Dimensions.get('window').width * 0.95,
  177.                                     }}>
  178.  
  179.  
  180.                                     <CardItem
  181.                                         button
  182.                                         onPress={() => this.props.navigation.navigate('StoryDetail', { id: item._id })}
  183.                                         style={{ borderRadius: 200 / 12 }}>
  184.                                         <Text
  185.                                             style={{
  186.                                                 fontSize: 18,
  187.                                                 fontFamily: 'Roboto',
  188.                                                 fontWeight: 'bold',
  189.                                                 color: '#015249'
  190.                                             }}
  191.                                         > {item.title} </Text>
  192.                                     </CardItem>
  193.  
  194.  
  195.                                     <CardItem
  196.                                         cardBody
  197.                                         button
  198.                                         onPress={() => this.props.navigation.navigate('StoryDetail', { id: item._id, photo: item._user.photo.secure_url })}
  199.                                         style={{ marginHorizontal: 10 }}
  200.                                     >
  201.                                         <Image
  202.                                             style={{
  203.                                                 width: '100%',
  204.                                                 height: 300,
  205.                                                 borderRadius: 200 / 12,
  206.                                             }}
  207.                                             source={{ uri: item.photo ? item.photo.secure_url : null }}
  208.                                         />
  209.                                     </CardItem>
  210.  
  211.                                     <CardItem
  212.                                         button
  213.                                         onPress={() => { this.props.navigation.navigate('UserDataById', { id: item._user._id }) }}
  214.                                         style={{ borderRadius: 200 / 12 }}
  215.                                     >
  216.                                         <Left>
  217.                                             <View style={{ flexDirection: 'row', alignItems: 'center' }}>
  218.                                                 <Image
  219.                                                     style={{
  220.                                                         width: 25,
  221.                                                         height: 25,
  222.                                                         borderRadius: 25 / 2
  223.                                                     }}
  224.                                                     source={{ uri: item._user.photo.secure_url }}
  225.                                                 />
  226.                                                 <Text
  227.                                                     style={{
  228.                                                         fontSize: 15,
  229.                                                         fontFamily: 'Roboto',
  230.                                                         fontWeight: 'bold',
  231.                                                     }}
  232.                                                 > {item._user.name} </Text>
  233.                                             </View>
  234.                                         </Left>
  235.                                     </CardItem>
  236.  
  237.  
  238.                                 </Card>
  239.                             </View>
  240.                         )}
  241.                         keyExtractor={item => item._id}
  242.                         onEndReached={this.handleEnd}
  243.                         onEndReachedThreshold={0.01}
  244.                         onRefresh={this.handleRefresh}
  245.                         refreshing={this.state.refreshing}
  246.                         // ListHeaderComponent={this.renderHeader}
  247.                         ListFooterComponent={this.renderFooter}
  248.                     />
  249.                     <Fab
  250.                         style={{
  251.                             backgroundColor: '#22cade',
  252.                         }}
  253.                         onPress={() => this.props.navigation.navigate('AddStory')}>
  254.                         <Icon name="add" />
  255.                     </Fab>
  256.  
  257.  
  258.                 </Container>
  259.  
  260.             )
  261.         } else
  262.             return (
  263.                 <Container style={{ backgroundColor: '#faffff' }}>
  264.                     <Header1 />
  265.                     <ScrollView
  266.                         onScroll={this.hideView}
  267.                         horizontal={true}
  268.                         style={{
  269.                             marginVertical: 10,
  270.                         }}
  271.                     >
  272.                         <Button
  273.                             small
  274.                             bordered
  275.                             rounded
  276.                             style={styles.button1
  277.                             }
  278.                             onPress={() => this.props.navigation.navigate('StoryByCategory', { category: 'hutan' })}>
  279.                             <Text>Hutan</Text>
  280.                         </Button>
  281.                         <Button
  282.                             small
  283.                             bordered
  284.                             rounded
  285.                             style={styles.button1
  286.                             }
  287.                             onPress={() => this.props.navigation.navigate('StoryByCategory', { category: 'kota' })}>
  288.                             <Text>Kota</Text></Button>
  289.                         <Button
  290.                             small
  291.                             bordered
  292.                             rounded
  293.                             style={styles.button1
  294.                             }
  295.                             onPress={() => this.props.navigation.navigate('StoryByCategory', { category: 'pantai' })}>
  296.                             <Text>Pantai</Text></Button>
  297.                         <Button
  298.                             small
  299.                             bordered
  300.                             rounded
  301.                             style={styles.button1
  302.                             }
  303.                             onPress={() => this.props.navigation.navigate('StoryByCategory', { category: 'sejarah' })}>
  304.                             <Text>Sejarah</Text></Button>
  305.                         <Button
  306.                             small
  307.                             bordered
  308.                             rounded
  309.                             style={styles.button1
  310.                             }
  311.                             onPress={() => this.props.navigation.navigate('StoryByCategory', { category: 'kota' })}>
  312.                             <Text>Agama</Text></Button>
  313.                     </ScrollView>
  314.                     <FlatList
  315.                         data={this.state.data}
  316.                         renderItem={({ item }) => (
  317.                             <View
  318.                                 key={item._id}
  319.                                 style={{ alignItems: 'center', justifyContent: 'center' }}>
  320.                                 <Card
  321.                                     style={{
  322.                                         marginVertical: 10,
  323.                                         borderRadius: 200 / 12,
  324.                                         width: Dimensions.get('window').width * 0.95,
  325.                                     }}>
  326.  
  327.  
  328.                                     <CardItem
  329.                                         button
  330.                                         onPress={() => this.props.navigation.navigate('StoryDetail', { id: item._id })}
  331.                                         style={{ borderRadius: 200 / 12 }}>
  332.                                         <Text
  333.                                             style={{
  334.                                                 fontSize: 18,
  335.                                                 fontFamily: 'Roboto',
  336.                                                 fontWeight: 'bold',
  337.                                                 color: '#015249'
  338.                                             }}
  339.                                         > {item.title} </Text>
  340.                                     </CardItem>
  341.  
  342.  
  343.                                     <CardItem
  344.                                         cardBody
  345.                                         button
  346.                                         onPress={() => this.props.navigation.navigate('StoryDetail', { id: item._id, photo: item._user.photo.secure_url })}
  347.                                         style={{ marginHorizontal: 10 }}
  348.                                     >
  349.                                         <Image
  350.                                             style={{
  351.                                                 width: '100%',
  352.                                                 height: 300,
  353.                                                 borderRadius: 200 / 12,
  354.                                             }}
  355.                                             source={{ uri: item.photo ? item.photo.secure_url : null }}
  356.                                         />
  357.                                     </CardItem>
  358.  
  359.                                     <CardItem
  360.                                         button
  361.                                         onPress={() => { this.props.navigation.navigate('UserDataById', { id: item._user._id }) }}
  362.                                         style={{ borderRadius: 200 / 12 }}
  363.                                     >
  364.                                         <Left>
  365.                                             <View style={{ flexDirection: 'row', alignItems: 'center' }}>
  366.                                                 <Image
  367.                                                     style={{
  368.                                                         width: 25,
  369.                                                         height: 25,
  370.                                                         borderRadius: 25 / 2
  371.                                                     }}
  372.                                                     source={{ uri: item._user.photo.secure_url }}
  373.                                                 />
  374.                                                 <Text
  375.                                                     style={{
  376.                                                         fontSize: 15,
  377.                                                         fontFamily: 'Roboto',
  378.                                                         fontWeight: 'bold',
  379.                                                     }}
  380.                                                 > {item._user.name} </Text>
  381.                                             </View>
  382.                                         </Left>
  383.                                     </CardItem>
  384.  
  385.  
  386.                                 </Card>
  387.                             </View>
  388.                         )}
  389.                         keyExtractor={item => item._id}
  390.                         onEndReached={this.handleEnd}
  391.                         onEndReachedThreshold={0.01}
  392.                         onRefresh={this.handleRefresh}
  393.                         refreshing={this.state.refreshing}
  394.                         // ListHeaderComponent={this.renderHeader}
  395.                         ListFooterComponent={this.renderFooter}
  396.                     />
  397.                 </Container>)
  398.     }
  399. }
  400. export default withNavigation(Home)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement