Advertisement
Guest User

Untitled

a guest
Jul 12th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import { View, Image, StyleSheet, ImageBackground, Dimensions, ScrollView, Linking, TouchableWithoutFeedback } from 'react-native';
  3. import { H3, Text, Card, CardItem, Button, Container, Content, Toast } from 'native-base';
  4. import Carousel, { Pagination } from 'react-native-snap-carousel';
  5. import HTML from 'react-native-render-html';
  6. import axios from 'axios';
  7. import MapView from 'react-native-maps';
  8. import { Callout } from 'react-native-maps';
  9. import { heightPercentageToDP as hp } from 'react-native-responsive-screen';
  10. import RF from 'react-native-responsive-fontsize';
  11. import Loading from '../components/Loading';
  12. import AppHeader from '../components/AppHeader';
  13.  
  14. export default class PageDetailCarousel extends Component {
  15.  
  16.     constructor(props) {
  17.         super(props);
  18.         this.state = {
  19.             currentIndex: 0,
  20.             activeSlide: 0,
  21.             type: 'carousel',
  22.             isLoading: false,
  23.             error: '',
  24.             items: [],
  25.             content: null,
  26.             tab_id: null
  27.         }
  28.         this.reRenderSomething = this.props.nav.addListener('willFocus', () => {
  29.             this.componentDidMount();
  30.         });
  31.     }
  32.  
  33.     componentDidMount() {
  34.         this.getItems(this.props.url);
  35.         console.log(this.state.items.tabs);
  36.         // this.state.items.tabs.filter(i => i.anchor === '#overview').map(i => {
  37.         //   this.setState({tab_id: i.id})
  38.         // });
  39.     }
  40.  
  41.     showErrorToast = () => {
  42.         this.setState({ isLoading: false });
  43.         Toast.show({
  44.             text: I18n.t('internetErrorText'),
  45.             type: "danger",
  46.             x: 0,
  47.             y: 0,
  48.             duration: 3000,
  49.             position: "top"
  50.         });
  51.     }
  52.  
  53.     getItems = (url_path) => {
  54.         this.setState({ isLoading: true });
  55.         axios.create({ headers: { 'from-app': '1' } }).get(url_path)
  56.             .then(result =>
  57.                 this.setState({
  58.                     items: result.data.page,
  59.                     isLoading: false
  60.                 })
  61.             )
  62.             .catch(error => this.showErrorToast());
  63.     }
  64.  
  65.     switchView = (type) => {
  66.         this.setState({ type: type, tab_id: type });
  67.     }
  68.  
  69.     changeContent = (anchor, id) => {
  70.         this.state.items.blocks.filter(i => "#" + i.anchor === anchor).map(i =>
  71.             i.blocks.map(i1 => {
  72.                 (i.anchor === 'overview') ? (this.setState({
  73.                     content: <View><H3 style={styles.titleStyle}>{this.state.items.title.replace(/&amp;/g, '&')}</H3>
  74.                         <View style={styles.textStyle}>
  75.                             <HTML html={i1.body} />
  76.                         </View></View>, type: 'carousel', tab_id: id
  77.                 })) : (
  78.                   (i.anchor === 'getting-here') ? (
  79.                   this.setState({
  80.                     content: <View style={styles.textStyle}>
  81.                         <HTML html={i1.body} />
  82.                     </View>, type: 'map', tab_id: id
  83.                   })
  84.                 ) : (
  85.                   this.setState({
  86.                     content: <View style={styles.textStyle}>
  87.                         <HTML html={i1.body} />
  88.                     </View>, type: 'carousel', tab_id: id
  89.                   })
  90.                 )
  91.               )
  92.             }
  93.             )
  94.         )
  95.     }
  96.  
  97.     _renderItem = ({ item }) => {
  98.         return (
  99.             <Image source={{ uri: item.image }} style={styles.imageBackground} />
  100.         );
  101.     }
  102.  
  103.     get pagination() {
  104.         const { items, activeSlide } = this.state;
  105.         return (
  106.             <Pagination
  107.                 dotsLength={items.banner_images.length}
  108.                 activeDotIndex={activeSlide}
  109.                 dotStyle={{
  110.                     width: 10,
  111.                     height: 10,
  112.                     padding: 1,
  113.                     borderRadius: 5,
  114.                     marginHorizontal: 8,
  115.                     backgroundColor: 'red'
  116.                 }}
  117.                 inactiveDotStyle={{
  118.                     // Define styles for inactive dots here
  119.                 }}
  120.                 inactiveDotOpacity={0.4}
  121.                 inactiveDotScale={0.6}
  122.             />
  123.         );
  124.     }
  125.  
  126.     render() {
  127.  
  128.         const { isLoading, items, content, tab_id } = this.state;
  129.         if (isLoading || items.length === 0) {
  130.             return <Loading />
  131.         }
  132.         let main_type;
  133.         if (this.state.type === 'map') {
  134.             main_type = <View>
  135.                 <MapView
  136.                     ref={c => this.mapView = c}
  137.                     style={styles.mapStyle}
  138.                     region={{
  139.                         latitude: 40.404917,
  140.                         longitude: 49.838502,
  141.                         latitudeDelta: 0.0922,
  142.                         longitudeDelta: 0.0421
  143.                     }}
  144.                 >
  145.                     <MapView.Marker
  146.                         coordinate={{
  147.                             latitude: parseFloat(this.props.ltd),
  148.                             longitude: parseFloat(this.props.lng)
  149.                         }}
  150.                     >
  151.                         <Image source={require('../assets/location.png')} />
  152.                         <Callout style={styles.calloutStyle}>
  153.                             <Card>
  154.                                 <CardItem cardBody>
  155.                                     <ImageBackground source={{ uri: items.banner_image }}
  156.                                         style={styles.imageStyle}
  157.                                         resizeMode='cover'></ImageBackground>
  158.                                 </CardItem>
  159.                                 <CardItem>
  160.                                     <Text>{items.title}</Text>
  161.                                 </CardItem>
  162.                             </Card>
  163.                         </Callout>
  164.                     </MapView.Marker>
  165.                 </MapView>
  166.             </View>;
  167.         } else {
  168.             if (typeof items.banner_images === "undefined" || items.banner_images.length === 0) {
  169.                 main_type = <View style={{ flex: 1 }}>
  170.                     <Image resizeMode='cover' style={styles.itemImage} source={{ uri: items.banner_image }} />
  171.                 </View>
  172.             } else {
  173.                 main_type = <View style={styles.carouselViewStyle}><Carousel
  174.                     ref={(c) => { this._carousel = c; }}
  175.                     onPress={() => { this._carousel.snapToNext(); }}
  176.                     onSnapToItem={(index) => this.setState({ activeSlide: index })}
  177.                     data={items.banner_images}
  178.                     renderItem={this._renderItem}
  179.                     sliderWidth={410}
  180.                     itemWidth={310}
  181.                 />
  182.                     {this.pagination}
  183.                 </View>;
  184.             }
  185.         }
  186.         return (
  187.             <Container>
  188.                 <AppHeader title={this.props.title} type={'detail'} />
  189.                 {main_type}
  190.                 {(typeof items.tabs !== "undefined") ?
  191.                     (<ScrollView style={styles.scrollViewStyle} horizontal={true} showsHorizontalScrollIndicator={false}>
  192.                         <View style={styles.listTypeViewStyle}>
  193.                             {(typeof items.tabs === "undefined") ? (<View></View>) :
  194.                                 (items.tabs.map(i =>
  195.                                     <TouchableWithoutFeedback style={{ height: 10 }} onPress={() => this.changeContent(i.anchor, i.id)}>
  196.                                         {(tab_id === i.id) ?
  197.                                             (
  198.                                                 <View style={[styles.listItemStyle, { backgroundColor: '#FF0014', borderColor: '#FF0014' }]}>
  199.                                                     <Text style={[styles.tabTextStyle, { color: '#fff' }]}>{i.title}</Text>
  200.                                                 </View>
  201.                                             ) :
  202.                                             (
  203.                                                 <View style={styles.listItemStyle}>
  204.                                                     <Text style={styles.tabTextStyle}>{i.title}</Text>
  205.                                                 </View>
  206.                                             )}
  207.                                     </TouchableWithoutFeedback>
  208.                                 ))
  209.                             }
  210.                         </View>
  211.                     </ScrollView>) : (<View></View>)
  212.                 }
  213.                 <Content style={styles.infoCardStyle}>
  214.                     {(content !== null) ? (content) :
  215.                         (items.blocks.filter(i => i.anchor === 'overview' || i.anchor === 'free-entry').map(i =>
  216.                             i.blocks.map(i1 =>
  217.                                 <View><H3 style={styles.titleStyle}>{items.title.replace(/&amp;/g, '&')}</H3>
  218.                                     <View style={styles.textStyle}>
  219.                                         <HTML html={i1.body} />
  220.                                     </View></View>
  221.                             )
  222.                         ))}
  223.                     {(typeof items.enquire_now !== "undefined") ?
  224.                         (<Button onPress={() => { Linking.openURL(items.enquire_now) }} style={styles.btnStyle} small><Text> Enquire now </Text></Button>) :
  225.                         (<View></View>)}
  226.                 </Content>
  227.             </Container>
  228.         );
  229.     }
  230.  
  231. }
  232.  
  233. const styles = StyleSheet.create({
  234.     viewStyle: {
  235.         paddingTop: 20,
  236.         paddingBottom: 20,
  237.         borderRadius: 15
  238.     },
  239.     carouselViewStyle: {
  240.         flex: 1,
  241.         backgroundColor: '#F5F7F6',
  242.         alignItems: 'center',
  243.         justifyContent: 'center'
  244.     },
  245.     scrollViewStyle: {
  246.         flexGrow: 0.07,
  247.         borderBottomWidth: 1,
  248.         borderBottomColor: '#c4c5c4'
  249.     },
  250.     listTypeViewStyle: {
  251.         alignItems: 'center',
  252.         justifyContent: 'center',
  253.         flexDirection: 'row',
  254.         elevation: 3,
  255.         paddingLeft: 20,
  256.         paddingRight: 20
  257.     },
  258.     listItemStyle: {
  259.         borderWidth: 1,
  260.         backgroundColor: '#F5F7F6',
  261.         borderColor: '#c4c5c4',
  262.         alignItems: 'center',
  263.         justifyContent: 'center',
  264.         marginRight: 10,
  265.         borderRadius: 30
  266.     },
  267.     tabTextStyle: {
  268.         paddingTop: 5,
  269.         paddingBottom: 5,
  270.         paddingLeft: 10,
  271.         paddingRight: 10,
  272.         fontSize: RF('1.6'),
  273.         color: '#333'
  274.     },
  275.     infoCardStyle: {
  276.         flex: 1,
  277.         shadowOpacity: 0
  278.     },
  279.     scrollMenuStyle: {
  280.         backgroundColor: '#fff',
  281.         paddingLeft: 5,
  282.         paddingRight: 20,
  283.         paddingTop: 20,
  284.         paddingBottom: 10,
  285.         marginLeft: 20,
  286.         height: hp('2%')
  287.     },
  288.     menuTextStyle: {
  289.         flex: 1,
  290.         color: '#333',
  291.         fontSize: hp("1.5%")
  292.     },
  293.     titleStyle: {
  294.         fontWeight: 'bold',
  295.         paddingLeft: 20,
  296.         paddingRight: 20,
  297.         paddingBottom: 20,
  298.         paddingTop: 10
  299.     },
  300.     textStyle: {
  301.         paddingLeft: 20,
  302.         paddingRight: 20,
  303.         paddingBottom: 20
  304.     },
  305.     cardStyle: {
  306.         borderRadius: 15
  307.     },
  308.     btnStyle: {
  309.         backgroundColor: 'red',
  310.         marginLeft: 20,
  311.         marginBottom: 20
  312.     },
  313.     imageBackground: {
  314.         height: hp('55%'),
  315.         width: null,
  316.         margin: 20,
  317.         flex: 1,
  318.         borderRadius: 15
  319.     },
  320.     line: {
  321.         borderBottomColor: 'black',
  322.         borderBottomWidth: 1,
  323.         marginLeft: 20,
  324.         marginRight: 20
  325.     },
  326.     mapStyle: {
  327.         width: Dimensions.get('window').width,
  328.         height: 248
  329.     },
  330.     calloutStyle: {
  331.         position: 'absolute',
  332.         minWidth: 200,
  333.         minHeight: 60
  334.     },
  335.     imageStyle: {
  336.         height: 120,
  337.         width: null
  338.     },
  339.     itemImage: {
  340.         flex: 1,
  341.         width: Dimensions.get('window').width,
  342.         height: hp('50%')
  343.     },
  344.     sectionViewStyle: {
  345.         paddingLeft: 10,
  346.         paddingRight: 10
  347.     }
  348. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement