Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { StyleSheet, View, ImageBackground, Dimensions, ScrollView, FlatList, ActivityIndicator, TouchableOpacity, Text } from 'react-native';
  3. import ProductDetail from '../components/ProductDetail';
  4. import BannerProduct from '../components/BannerProduct';
  5. import SizeSelector from '../components/SizeSelector';
  6. import ColorSelector from '../components/ColorSelector';
  7. import Product from '../components/Product';
  8. import firebase from '../enviroments/Firebase'
  9. import { StackActions } from 'react-navigation';
  10. import BottomButton from '../components/BottomButton';
  11. import ProductAddContainer from '../containers/ProductAddContainer';
  12. import {createStackNavigator, createAppContainer, createBottomTabNavigator } from 'react-navigation';
  13.  
  14. class ProductDetailContainer extends React.Component {
  15.  
  16.     constructor(props) {
  17.         super(props);
  18.         this.state = {
  19.             isLoading: true,
  20.             isLoading2: true,
  21.             imageProduct: [],
  22.             popularProducts: [],
  23.         }
  24.         this.refPopular = firebase.firestore().collection('fl_content')
  25.                                 .where('_fl_meta_.schema','==','product')
  26.                                 .where('popular','==','1');
  27.         this.unsubscribePopular = null;
  28.     }
  29.  
  30.     componentDidMount() {
  31.         //this.unsubscribe = this.ref.onSnapshot(this.onCollectionUpdate);
  32.         const imageProduct = []
  33.         this.unsubscribePopular = this.refPopular.onSnapshot(this.onCollectionUpdatePopular);
  34.         this.props.screenProps.ref.get().then( (fileDoc) => {
  35.             const fileStorage = fileDoc;
  36.             const file_name =  fileStorage.data().file;
  37.             const urlImg = "flamelink/media/" + file_name;
  38.             let refImg = firebase.storage().ref(urlImg);
  39.            
  40.             refImg.getDownloadURL().then((urlDownload) => {
  41.                 imageProduct.push(urlDownload)
  42.                 this.setState({
  43.                     imageProduct : imageProduct,
  44.                     isLoading: false
  45.                 })
  46.            
  47.             });
  48.            
  49.         })
  50.         this.props.screenProps.doc.data().categoryProduct[0].get().then( (categoryDoc) => {
  51.             //console.log(categoryDoc)
  52.             this.setState({
  53.                 category: categoryDoc.data()
  54.              });
  55.         })
  56.     }
  57.     onCollectionUpdatePopular =   (querySnapshot) => {
  58.         const popularProducts = [];
  59.         querySnapshot.forEach((doc) => {
  60.             const { name, description, price } = doc.data();
  61.             //console.log(doc.data())
  62.             const ref =  doc.data().imageCover[0]
  63.            
  64.             popularProducts.push({
  65.                 id: doc.id,
  66.                 doc, // DocumentSnapshot
  67.                 name,
  68.                 description,
  69.                 price,
  70.                 ref
  71.             });
  72.         });
  73.         this.setState({
  74.             popularProducts,
  75.             isLoading2: false,
  76.         });
  77.     }
  78.  
  79.     handleRefresh() {
  80.         this.setState(
  81.           {
  82.                 isLoading2: true,
  83.         },
  84.           () => {
  85.                 this.unsubscribePopular = this.refPopular.onSnapshot(this.onCollectionUpdatePopular);
  86.             });
  87.     }
  88.  
  89.  
  90.     render() {
  91.         const { imageProduct } = this.state
  92.         const { isLoading2, popularProducts } = this.state;
  93.         const {navigate} = this.props.navigation;
  94.  
  95.        
  96.  
  97.         return (
  98.             <ImageBackground
  99.                 source={require('../assets/images/bg.png')}
  100.                 style={styles.bg}
  101.             >  
  102.  
  103.                 <View style={styles.wrapper}>
  104.                     <View style={styles.container}>
  105.                         <ScrollView
  106.                             ref='_scrollView'
  107.                             contentContainerStyle={styles.contentContainer}>
  108.                             <BannerProduct imageProduct={imageProduct}/>
  109.                             <View style={styles.top}>
  110.                                 <ProductDetail product={this.props.screenProps} />
  111.                             </View>
  112.                             <View
  113.                                 style={[styles.top, styles.marginTop20, styles.marginHorizontal25]}
  114.                                 >
  115.                                 <Text>Produk Rekomendasi</Text>
  116.                             </View>
  117.                             <View style={styles.top}>
  118.                                 {/* <SizeSelector product={this.props.navigation.state.params.product} /> */}
  119.                                 {
  120.                                     !isLoading2 ?
  121.                                         <View style={[styles.paddingHorizontal10,styles.top]}>
  122.                                        
  123.                                             <ScrollView horizontal={true}
  124.                                                 showsHorizontalScrollIndicator={false}
  125.                                                 contentContainerStyle={styles.contentContainerHorizontal}>
  126.                                                 <FlatList
  127.                                                     horizontal={true}
  128.                                                     data={popularProducts}
  129.                                                     renderItem={({ item }) =>
  130.                                                         <TouchableOpacity onPress={() => {
  131.                                                             const pushAction = StackActions.push({
  132.                                                                 routeName: 'ProductDetail',
  133.                                                                 params: {
  134.                                                                     product: item,
  135.                                                                 },
  136.                                                             });
  137.                                                             //this.refs._scrollView.scrollTo({x:0,y:0,animated:false});
  138.                                                             this.props.navigation.dispatch(pushAction)
  139.                                                         }}>
  140.                                                             <Product product={item} />
  141.                                                         </TouchableOpacity>}
  142.                                                     keyExtractor={item => item.id}
  143.                                                     refreshing={this.state.isLoading2}
  144.                                                     onRefresh={this.handleRefresh.bind(this)}
  145.                                                 />
  146.                                             </ScrollView>
  147.                                         </View> :
  148.                                         <View style={[styles.containerLoad, styles.horizontalLoad]}>
  149.                                             <ActivityIndicator size="large" color="#3498db" />
  150.                                         </View>
  151.                                 }
  152.                             </View>
  153.                             {/* <View style={[styles.top, styles.marginTop20]}>
  154.                                 <ColorSelector product={this.props.navigation.state.params.product} />
  155.                             </View> */}
  156.                         </ScrollView>
  157.                        
  158.                        
  159.                        
  160.                     </View>
  161.                 </View>
  162.                 <View style={styles.navbar}>
  163.                     <BottomButton navigate={this.props.navigation.navigate}  product={this.props.screenProps} />
  164.                 </View>
  165.             </ImageBackground>
  166.         );
  167.     }
  168. }
  169.  
  170. const styles = StyleSheet.create({
  171.     navbar: {
  172.         flex:1,
  173.     },
  174.     paddingHorizontal10:{
  175.         paddingHorizontal:10
  176.     },
  177.     marginHorizontal25: {
  178.         marginHorizontal: 25
  179.     },
  180.     bg: {
  181.         flex: 1,
  182.         resizeMode: 'cover',
  183.         width: Dimensions.get('window').width,
  184.         height: Dimensions.get('window').height
  185.     },
  186.     top: {
  187.  
  188.         flexDirection: 'row',
  189.         justifyContent: 'space-between'
  190.     },
  191.     marginTop20:{
  192.         marginTop: 20
  193.     },
  194.     wrapper: {
  195.         flex: 7
  196.     },
  197.     contentContainer: {
  198.         paddingBottom: 50
  199.     },
  200. });
  201.  
  202. //export default ProductDetailContainer;
  203.  
  204.  
  205. const AppNavigator = createStackNavigator({
  206.     ProductDetail: {
  207.         screen:ProductDetailContainer,
  208.         navigationOptions: ({ navigation, screenProps  }) => {
  209.             //console.log(screenProps )
  210.             return {
  211.                 tabBarVisible: false,
  212.                 title: screenProps.name,
  213.                 headerStyle: {
  214.                     elevation: 0,       //remove shadow on Android
  215.                     shadowOpacity: 0,   //remove shadow on iOS
  216.                     borderBottomWidth: 0.5
  217.                 },
  218.             }
  219.            
  220.         }
  221.     },
  222.     ProductAddContainer: {
  223.         screen: ProductAddContainer,
  224.     }
  225. });
  226.  
  227.  
  228. const BottomContainer = createAppContainer(AppNavigator);
  229.  
  230. export default class App extends React.Component {
  231.    
  232.     render() {
  233.        
  234.         return (
  235.             <BottomContainer screenProps={this.props.navigation.state.params.product}/>
  236.            
  237.         )
  238.     }
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement