Advertisement
Guest User

Untitled

a guest
Jul 10th, 2019
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { AsyncStorage, StyleSheet, View } from 'react-native';
  3. import Toolbar from './toolbar/Toolbar';
  4. import PropTypes from 'prop-types';
  5. import { connect } from 'react-redux';
  6. import { addMessage, clearMessages, getLastMessage, getMessages } from '../../../actions';
  7. import { pop } from '../../../NavigationService';
  8. import firebase from 'react-native-firebase';
  9. import FastImage from 'react-native-fast-image';
  10. import bStyles from '../../general/BaseStyles';
  11. import imgs from '../../../assets/image/imgs';
  12. import { Bubble, Composer, Day, GiftedChat, InputToolbar, LoadEarlier, Send, Time } from 'react-native-gifted-chat';
  13. import i18n from 'i18n-js';
  14. import { fontCompat } from '../../../Utils/utils';
  15. import { SafeAreaView } from 'react-navigation';
  16.  
  17. class ChatScreen extends React.Component {
  18.     notificationListener;
  19.  
  20.     constructor(props) {
  21.         super(props);
  22.         this.state = {
  23.             userId: -1,
  24.             sentMessages: [],
  25.         };
  26.     }
  27.  
  28.     componentWillMount() {
  29.         AsyncStorage.getItem('user_id')
  30.             .then(result => this.setState({ userId: Number.parseInt(result) }))
  31.             .catch();
  32.     }
  33.  
  34.     componentDidMount() {
  35.         const orderId = this.props.navigation.getParam('orderId', -1);
  36.         this.props.getMessages(orderId);
  37.         this.notificationListener = firebase.notifications().onNotification(notification => {
  38.             const {
  39.                 _data: {
  40.                     order_id,
  41.                 },
  42.             } = notification;
  43.             this.props.getLastMessage(order_id);
  44.         });
  45.     }
  46.  
  47.     componentWillReceiveProps(nextProps) {
  48.         const orderId = this.props.navigation.getParam('orderId', -1);
  49.         const { sentMessageId } = this.props;
  50.         const {
  51.             sentMessageId: nextSentMessageId,
  52.         } = nextProps;
  53.  
  54.         nextSentMessageId && sentMessageId !== nextSentMessageId && this.props.getLastMessage(orderId);
  55.     }
  56.  
  57.     componentWillUnmount() {
  58.         this.props.clearMessages();
  59.         this.notificationListener && this.notificationListener();
  60.     }
  61.  
  62.     render() {
  63.         const name = this.props.navigation.getParam('name', '');
  64.         const avatarUrl = this.props.navigation.getParam('avatarUrl', '');
  65.         const {
  66.             loading,
  67.             loadedAllData,
  68.             messages,
  69.         } = this.props;
  70.         const {
  71.             userId,
  72.         } = this.state;
  73.  
  74.         return (
  75.             <SafeAreaView style={styles.container}>
  76.                 <FastImage style={bStyles.imageBackground}
  77.                            source={imgs.background}/>
  78.                 <Toolbar onBackPress={this.handleBackPress}
  79.                          title={name}
  80.                          avatarUrl={avatarUrl}/>
  81.                 <GiftedChat
  82.                     messages={messages}
  83.                     listViewProps={{
  84.                         initialNumToRender: 20,
  85.                     }}
  86.                     renderAvatar={null}
  87.                     onSend={this.handleSend}
  88.                     user={{ _id: userId }}
  89.                     isAnimated={false}
  90.                     keyboardShouldPersistTaps={'never'}
  91.                     placeholder={i18n.t('Type your message here')}
  92.                     loadEarlier={!loadedAllData}
  93.                     onLoadEarlier={this.handleLoadEarlier}
  94.                     renderDay={this.renderDay}
  95.                     isLoadingEarlier={loading}
  96.                     renderBubble={this.renderBubble}
  97.                     textInputProps={{
  98.                         autoCapitalize: 'none',
  99.                         keyboardAppearance: 'dark',
  100.                         autoCorrect: false,
  101.                     }}
  102.                     renderInputToolbar={this.renderInputToolbar}
  103.                     renderSend={this.renderSend}
  104.                     renderComposer={this.renderComposer}
  105.                     renderTime={this.renderTime}
  106.                     renderLoadEarlier={this.renderLoadEarlier}/>
  107.             </SafeAreaView>
  108.         );
  109.     }
  110.  
  111.     handleBackPress = () => {
  112.         pop();
  113.     };
  114.  
  115.     handleSend = (messages) => {
  116.         this.setState({
  117.             sentMessages: messages,
  118.         });
  119.         messages.forEach(message => {
  120.             const orderId = this.props.navigation.getParam('orderId', -1);
  121.             const { text } = message;
  122.             this.props.addMessage(orderId, text);
  123.         });
  124.     };
  125.  
  126.     handleLoadEarlier = () => {
  127.         const orderId = this.props.navigation.getParam('orderId', -1);
  128.         this.props.getMessages(orderId);
  129.     };
  130.  
  131.     renderLoadEarlier = props => {
  132.         return (
  133.             <LoadEarlier {...props}
  134.                          textStyle={styles.loadEarlierText}/>
  135.         );
  136.     };
  137.  
  138.     renderBubble = props => {
  139.         return (
  140.             <Bubble
  141.                 {...props}
  142.                 wrapperStyle={{
  143.                     left: styles.left,
  144.                     right: styles.right,
  145.                 }}
  146.                 textStyle={{
  147.                     right: styles.bubbleRightText,
  148.                     left: styles.bubbleLeftText,
  149.                 }}
  150.             />
  151.         );
  152.     };
  153.  
  154.     renderDay = props => {
  155.         return (
  156.             <Day
  157.                 {...props}
  158.                 textStyle={styles.dayText}/>
  159.         );
  160.     };
  161.  
  162.     renderTime = props => {
  163.         return (
  164.             <Time {...props}
  165.                   textStyle={{
  166.                       left: styles.timeTextLeft,
  167.                       right: styles.timeTextRight,
  168.                   }}/>
  169.         );
  170.     };
  171.  
  172.     renderInputToolbar = props => {
  173.         return (
  174.             <InputToolbar
  175.                 {...props}
  176.                 containerStyle={styles.inputContainer}
  177.                 primaryStyle={styles.inputPrimary}/>
  178.         );
  179.     };
  180.  
  181.     renderSend = props => {
  182.         return (
  183.             <Send
  184.                 {...props}
  185.                 containerStyle={styles.sendContainer}
  186.                 children={<FastImage source={imgs.send}
  187.                                      style={styles.send}/>}/>
  188.         );
  189.     };
  190.  
  191.     renderComposer = props => {
  192.         return (
  193.             <Composer {...props}
  194.                       textInputStyle={styles.composerText}
  195.                       placeholderTextColor={'grey'}/>
  196.         );
  197.     };
  198. }
  199.  
  200. ChatScreen.propTypes = {
  201.     orderId: PropTypes.number,
  202.     name: PropTypes.string,
  203.     avatarUrl: PropTypes.string,
  204. };
  205.  
  206. const styles = StyleSheet.create({
  207.     container: {
  208.         flex: 1,
  209.         backgroundColor: 'rgba(0, 0, 0, 0.9)',
  210.     },
  211.     left: {
  212.         backgroundColor: '#e5e6ea',
  213.     },
  214.     right: {
  215.         backgroundColor: '#d1b624',
  216.     },
  217.     inputContainer: {
  218.         backgroundColor: 'rgba(0, 0, 0, 0.9)',
  219.         minHeight: 44,
  220.     },
  221.     inputPrimary: {
  222.         backgroundColor: '#f9f9f9',
  223.         marginVertical: 4,
  224.         marginHorizontal: 16,
  225.         borderRadius: 24,
  226.         alignItems: 'center',
  227.     },
  228.     sendContainer: {
  229.         width: 36,
  230.         height: 36,
  231.         borderRadius: 18,
  232.         backgroundColor: '#f9f9f9',
  233.         alignItems: 'center',
  234.         justifyContent: 'center',
  235.     },
  236.     send: {
  237.         width: 20,
  238.         height: 20,
  239.     },
  240.     composerText: {
  241.         color: 'black',
  242.         fontSize: 14,
  243.         ...fontCompat('OpenSans-Regular'),
  244.     },
  245.     timeTextLeft: {
  246.         color: 'grey',
  247.         fontSize: 10,
  248.         ...fontCompat('OpenSans-Regular'),
  249.     },
  250.     timeTextRight: {
  251.         color: 'white',
  252.         fontSize: 10,
  253.         ...fontCompat('OpenSans-Regular'),
  254.     },
  255.     dayText: {
  256.         color: 'grey',
  257.         fontSize: 13,
  258.         ...fontCompat('OpenSans-Bold'),
  259.     },
  260.     bubbleLeftText: {
  261.         color: 'black',
  262.         fontSize: 14,
  263.         ...fontCompat('OpenSans-Regular'),
  264.     },
  265.     bubbleRightText: {
  266.         color: 'white',
  267.         fontSize: 14,
  268.         ...fontCompat('OpenSans-Regular'),
  269.     },
  270.     loadEarlierText: {
  271.         color: 'white',
  272.         fontSize: 12,
  273.         ...fontCompat('OpenSans-Bold'),
  274.     },
  275. });
  276.  
  277. const mapStateToProps = state => {
  278.     return {
  279.         messages: state.messages.data,
  280.         sentMessageId: state.addMessage.data.message_id,
  281.         loading: state.messages.isLoading,
  282.         loadedAllData: state.messages.loadedAllData,
  283.     };
  284. };
  285.  
  286. const mapDispatchToProps = {
  287.     getMessages,
  288.     addMessage,
  289.     clearMessages,
  290.     getLastMessage,
  291. };
  292.  
  293. export default connect(mapStateToProps, mapDispatchToProps)(ChatScreen);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement