Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. import { Post, SelectPostFromOffers } from '@posts';
  2. import { ActionDispatcher, AppState } from '@store/models';
  3. import { mainStyles } from '@ui';
  4. import { HTMLParagraph } from '@ui/components/HTMLParagraph';
  5. import HeaderIcon from '@ui/components/HeaderIcon';
  6. import Loader from '@ui/components/Loader';
  7. import { GLOBAL_COLORS, GLOBAL_ICONS } from '@ui/const';
  8. import * as React from 'react';
  9. import { LayoutAnimation, Platform, StyleSheet, Text, UIManager, View } from 'react-native';
  10. import SafeAreaView from 'react-native-safe-area-view';
  11. import { NavigationScreenProp } from 'react-navigation';
  12. import { connect } from 'react-redux';
  13. import { Dispatch } from 'redux';
  14.  
  15. import { BackToFilterOffer, UpdateFilterOffers } from '../actions';
  16. import ChoicesList from '../components/ChoicesList';
  17. import { Offer } from '../models';
  18. import { getOffersList, isOffersListLoading } from '../selectors';
  19.  
  20. interface Props {
  21. back: ActionDispatcher<BackToFilterOffer>;
  22. data: Offer[];
  23. isLoading: boolean;
  24. navigation: NavigationScreenProp<any, any>;
  25. selectPost: ActionDispatcher<SelectPostFromOffers>;
  26. update: ActionDispatcher<UpdateFilterOffers>;
  27. }
  28.  
  29. class FilterView extends React.Component<Props> {
  30. constructor(props: Props) {
  31. super(props);
  32.  
  33. if (Platform.OS === 'android') {
  34. UIManager.setLayoutAnimationEnabledExperimental(true);
  35. }
  36. }
  37.  
  38. backButtonHandler = () => {
  39. this.props.navigation.goBack();
  40. };
  41.  
  42. selectCategory = (offer: Offer) => {
  43. const { selectPost, data, update } = this.props;
  44.  
  45. if (offer.title) {
  46. selectPost(offer);
  47. } else {
  48. LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
  49.  
  50. const updatedOffers = [...data, offer];
  51. const indexToUpdate = updatedOffers.length - 2;
  52.  
  53. updatedOffers[indexToUpdate] = {
  54. ...updatedOffers[indexToUpdate],
  55. label: offer.name,
  56. };
  57.  
  58. update(updatedOffers);
  59. }
  60. };
  61.  
  62. backToPrevious = (offer: Offer) => {
  63. LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
  64. this.props.back(offer);
  65. };
  66.  
  67. _renderHeader = (offer: Offer | undefined): JSX.Element | null =>
  68. offer ? (
  69. <View style={styles.headerContainer}>
  70. <Text style={styles.header}>{offer.header}</Text>
  71. <HTMLParagraph
  72. primaryColor={GLOBAL_COLORS.white}
  73. secondaryColor={GLOBAL_COLORS.black}
  74. content={offer.description}
  75. font={styles.desc}
  76. />
  77. </View>
  78. ) : null;
  79.  
  80. _renderChoicesContainer = (data: Offer[]): JSX.Element | null =>
  81. data ? (
  82. <ChoicesList
  83. back={this.backToPrevious}
  84. data={data}
  85. latest={data[data.length - 1]}
  86. select={this.selectCategory}
  87. />
  88. ) : null;
  89.  
  90. _renderBackButton = (): JSX.Element => (
  91. <View style={mainStyles.headerIcon}>
  92. <HeaderIcon icon={GLOBAL_ICONS.times} onPress={this.backButtonHandler} />
  93. </View>
  94. );
  95.  
  96. render(): JSX.Element {
  97. const { data, isLoading } = this.props;
  98.  
  99. return (
  100. <View style={styles.background}>
  101. <SafeAreaView style={mainStyles.mainContainer}>
  102. <Loader isLoading={isLoading} />
  103. {this._renderHeader(data ? data[data.length - 1] : undefined)}
  104. {this._renderChoicesContainer(data)}
  105. {this._renderBackButton()}
  106. </SafeAreaView>
  107. </View>
  108. );
  109. }
  110. }
  111.  
  112. const styles = StyleSheet.create({
  113. background: {
  114. backgroundColor: GLOBAL_COLORS.blue,
  115. flex: 1,
  116. },
  117. desc: {
  118. color: GLOBAL_COLORS.white,
  119. fontSize: 15,
  120. fontWeight: '400',
  121. },
  122. header: {
  123. color: GLOBAL_COLORS.white,
  124. fontSize: 28,
  125. fontWeight: 'bold',
  126. letterSpacing: 1.4,
  127. marginBottom: 25,
  128. },
  129. headerContainer: {
  130. paddingHorizontal: 25,
  131. paddingTop: 60,
  132. width: '100%',
  133. },
  134. });
  135.  
  136. const mapStateToProps = (state: AppState) => ({
  137. data: getOffersList(state),
  138. isLoading: isOffersListLoading(state),
  139. });
  140.  
  141. const mapDispatchToProps = (dispatch: Dispatch) => ({
  142. back: (offer: Offer) => dispatch(new BackToFilterOffer(offer)),
  143. selectPost: (post: Post) => dispatch(new SelectPostFromOffers(post)),
  144. update: (offers: Offer[]) => dispatch(new UpdateFilterOffers(offers)),
  145. });
  146.  
  147. export default connect(
  148. mapStateToProps,
  149. mapDispatchToProps,
  150. )(FilterView);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement