Advertisement
Guest User

react native fragment

a guest
Mar 6th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // React
  2. import React, { Component } from 'react';
  3. import { View, ScrollView, TouchableWithoutFeedback } from 'react-native';
  4.  
  5. // PropTypes
  6. import PropTypes from 'prop-types';
  7.  
  8. // lodash
  9. import _ from 'lodash';
  10.  
  11. // Redux
  12. import { connect } from 'react-redux';
  13. import { fetchNews } from '../../store/actions/news_actions';
  14.  
  15. // UI
  16. import { Container, Text } from 'native-base';
  17. import SmallButton from '../uiElements/smallButton';
  18.  
  19. // Styles
  20. import styles from './styles';
  21.  
  22. import Spinner from '../uiElements/spinner';
  23.  
  24. // core
  25. import { formatDate } from '../../core/utils';
  26.  
  27. class NewsList extends Component {
  28.   static propTypes = {
  29.     news: PropTypes.array,
  30.     loadFrom: PropTypes.number,
  31.     isLoading: PropTypes.bool,
  32.     isLoadingFinished: PropTypes.bool,
  33.     fetchNews: PropTypes.func,
  34.     widgetMode: PropTypes.bool,
  35.     navigation: PropTypes.object,
  36.   }
  37.  
  38.   constructor(props) {
  39.     super(props);
  40.  
  41.     this.state = {
  42.       pagination: {
  43.         'sort[date]': '-1',
  44.       },
  45.     };
  46.   }
  47.  
  48.   componentWillMount() {
  49.     this.loadNews(0, this.state.pagination);
  50.   }
  51.  
  52.   onPageScroll = (e) => {
  53.     if (this.isCloseToBottom(e.nativeEvent)) {
  54.       if (!this.props.isLoadingFinished) {
  55.         this.loadNews(this.props.loadFrom);
  56.       }
  57.     }
  58.   }
  59.  
  60.   isCloseToBottom = ({ layoutMeasurement, contentOffset, contentSize }) => {
  61.     const paddingToBottom = 20;
  62.     return layoutMeasurement.height + contentOffset.y >=
  63.       contentSize.height - paddingToBottom;
  64.   };
  65.  
  66.   loadNews = (from, params = {}) => {
  67.     if (!this.props.isLoading) {
  68.       const currentParams = {
  69.         ...this.state.pagination,
  70.         ...params,
  71.       };
  72.  
  73.       this.props.fetchNews(from, currentParams);
  74.     }
  75.   }
  76.  
  77.   goToDetailNewsPage = (newsId, newsTitle, newsText) => {
  78.     this.props.navigation.dispatch({ type: 'DetailNewsPage', params: { newsId, newsTitle, newsText } });
  79.   }
  80.  
  81.   renderNewsItems = news => news.map(newsData => (
  82.     <TouchableWithoutFeedback
  83.       key={newsData.id}
  84.       onPress={() => {
  85.         this.goToDetailNewsPage(
  86.           newsData.id,
  87.           newsData.name,
  88.           newsData.description,
  89.         );
  90.       }}
  91.     >
  92.       <View key={newsData.id} style={styles.newsItem}>
  93.         <View style={styles.promotionContainer} >
  94.           <Text style={styles.newsDate}>{formatDate(newsData.date, 'ddmmyyyy')}</Text>
  95.           <Text style={styles.newsText}>{newsData.title}</Text>
  96.           <SmallButton
  97.             style={styles.detailPromotionButton}
  98.             direction="right"
  99.             onPress={() => {
  100.               this.goToDetailNewsPage(
  101.                 newsData.id,
  102.                 newsData.name,
  103.                 newsData.description,
  104.               );
  105.             }}
  106.           />
  107.         </View>
  108.       </View>
  109.     </TouchableWithoutFeedback>
  110.   ))
  111.  
  112.   renderNews = () => {
  113.     if (this.props.news.length > 0) {
  114.       const news = _.toArray(this.props.news);
  115.  
  116.       return (
  117.         <ScrollView style={styles.newsScrollContainer} onScroll={this.onPageScroll}>
  118.           {this.renderNewsItems(news)}
  119.         </ScrollView>);
  120.     }
  121.  
  122.     return (
  123.       <View>
  124.         {!this.props.isLoading &&
  125.         <Text style={styles.newsNotFound}>Акций нет</Text>}
  126.         {this.props.isLoading &&
  127.           <Spinner animating />
  128.         }
  129.       </View>
  130.     );
  131.   }
  132.  
  133.   render() {
  134.     return (
  135.       <Container style={styles.content}>
  136.         {this.renderNews()}
  137.       </Container>
  138.     );
  139.   }
  140. }
  141.  
  142. function mapStateToProps(state) {
  143.   return {
  144.     news: state.newsReducer.toJS().news,
  145.     loadFrom: state.newsReducer.get('loadFrom'),
  146.     isLoading: state.newsReducer.get('isLoading'),
  147.     isLoadingFinished: state.newsReducer.get('isLoadingFinished'),
  148.   };
  149. }
  150.  
  151. const actions = {
  152.   fetchNews,
  153. };
  154.  
  155. export default connect(mapStateToProps, actions)(NewsList);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement