Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { PureComponent } from 'react';
  2. import { View, Linking, TouchableOpacity, StyleSheet, Text } from 'react-native';
  3. import UtilDate from '../../../utils/UtilDate';
  4. import FastImage from 'react-native-fast-image';
  5. import * as _ from 'lodash';
  6. import UtiImage from '../../../utils/UtiImage';
  7. import { SCREEN } from '../../../utils/UtilDeviceInfo';
  8. import ParsedText from 'react-native-parsed-text';
  9. import { ReactionToggleLikeChild } from './ReactionToggleLikeChild';
  10. import { ReactionCommentChild } from './ReactionCommentChild';
  11. import Collapsible from 'react-native-collapsible';
  12. import CommentItemChild from './CommentItemChild';
  13. import localization from '../../../localization';
  14. import { ReactionLabelLikeChild } from './ReactionLabelLikeChild';
  15. import { ReactionLabelCommentChild } from './ReactionLabelCommentChild';
  16. import ModalContent from './ModalContent';
  17. import Modal from 'react-native-modal';
  18. import AppConfig from '../../../AppConfig';
  19. import ModalImageComment from './Modal/ModalImageComment';
  20. import Modalize from 'react-native-modalize';
  21. import ModalContentToggle from '../component/Modal/ModalContentToggle'
  22. export default class CommentItemCustom extends PureComponent {
  23.   constructor(props) {
  24.     super(props);
  25.     this.state = {
  26.       visibleCommentChild: false,
  27.       dataCommentChild: [],
  28.       visibleModal: false,
  29.       isCommentChild:false,
  30.       commentParent:{},
  31.       commentChild:{},
  32.       isModalVisible: false,
  33.       urlViewImage: null,
  34.       visibleLoading: false,
  35.     };
  36.   }
  37.  
  38.   componentDidMount() {
  39.     const { comment = {} } = this.props;
  40.     this.setState({
  41.       dataCommentChild: ( (comment.latest_children || {}).comment || [] ).reverse()
  42.     });
  43.   }
  44.   _onPressImage = (images) => {
  45.     let image = UtiImage.getUrlImageStream(images);
  46.     this.setState({
  47.       isModalVisible: true,
  48.       urlViewImage: image.uri
  49.     });
  50.   }
  51.   _closeModal = () => {
  52.     this.setState({ visibleModal: false,isCommentChild:false, isModalVisible: false, visibleLoading:false });
  53.   };
  54.   _LoadCommentChild = () => {
  55.     this.setState({
  56.       visibleCommentChild: true
  57.     });
  58.   };
  59.   renderText(matchingString, matches) {
  60.     let pattern = /\[(@[^:]+):([^\]]+)\]/i;
  61.     let match = matchingString.match(pattern);
  62.     match = (match[1] || '').substring(1);
  63.     return `${match}`;
  64.   }
  65.   handleEmailPress(email) {}
  66.   handlePhonePress(phone) {}
  67.   handleUrlPress(url) {
  68.     Linking.openURL(url);
  69.   }
  70.   handleNamePress(name) {}
  71.   _onPressEditComment = comment => {
  72.     const { onPressEditComment } = this.props;
  73.     if (onPressEditComment) {
  74.       onPressEditComment(comment);
  75.     }
  76.   };
  77.   _onPressEditCommentChild =  async (comment,commentChild) => {
  78.     const { onPressEditComment } = this.props;
  79.     if (onPressEditComment) {
  80.       onPressEditComment(comment,true,commentChild);
  81.     }
  82.   };
  83.   /**
  84.    *   1 comment, default ,
  85.    *   2 comment child comment ,
  86.    *   3 comment child comment child
  87.    */
  88.   _onPressChildComment = comment => {
  89.     const { onPressCommentChild } = this.props;
  90.     if (onPressCommentChild) {
  91.       onPressCommentChild(comment, 2);
  92.     }
  93.   };
  94.   _onPressChildParentComment = (comment, commentParent) => {
  95.     const { onPressCommentChildParent } = this.props;
  96.     if (onPressCommentChildParent) {
  97.       onPressCommentChildParent(comment, commentParent, 3);
  98.     }
  99.   };
  100.   _onShowModalCommentChild = (comment, commentParent) => {
  101.     this.setState({
  102.       visibleModal: true,isCommentChild:true,commentParent,commentChild:comment
  103.     },() => {this.forceUpdate()});
  104.   };
  105.  
  106.   _handlerLongClick = () => {
  107.     this.setState({
  108.       visibleModal: true,isCommentChild:false
  109.     });
  110.   };
  111.   _doReactionDeleteRequest = async idReaction => {
  112.     try {
  113.       const { props = {} } = this.props;
  114.       props.onRemoveReaction(AppConfig.VERB_COMMENT, props.activity, idReaction);
  115.      
  116.     } catch (e) {}
  117.   };
  118.   _doReactionChildDeleteRequest = async (commentParent,idChildReaction) => {
  119.     try {
  120.       const { onRemoveChildReaction = {} } = this.props;
  121.       if(onRemoveChildReaction){
  122.         onRemoveChildReaction(commentParent, idChildReaction);
  123.       }  
  124.     } catch (e) {}
  125.   };
  126.  
  127.   render() {
  128.     const { comment = {}, props, userIdCurrent ,onShowModallize =() => {}} = this.props;
  129.     let text = _.get(comment.data, 'text', '');
  130.     text = text.trim();
  131.     //show tên người bình luận đầu tiên
  132.     const children_counts = (comment.children_counts || {}).comment || 0;
  133.     let label_children = '';
  134.     if (children_counts > 0 &&
  135.       (comment.latest_children || {}).comment &&
  136.       (comment.latest_children || {}).comment.length > 0
  137.     ) {
  138.       let itemFirst = (comment.latest_children || {}).comment[0] || {};
  139.       label_children = _.get(itemFirst.user, 'data.name');
  140.     }
  141.     //check quyền xoá comment
  142.     const userIdComment = _.get(comment.user, 'id');
  143.     return (
  144.       <View style={styles.container}>
  145.         <View style={styles.viewHeader}>
  146.           <TouchableOpacity
  147.             disabled={userIdCurrent === userIdComment ? false : true}
  148.             activeOpacity={0.6}
  149.             onLongPress={this._handlerLongClick}
  150.           >
  151.             <View style={{ flexDirection: 'row' }}>
  152.               <FastImage
  153.                 style={styles.imageView}
  154.                 resizeMode={FastImage.resizeMode.cover}
  155.                 source={
  156.                   _.get(comment.user, 'data.profileImage')
  157.                     ? UtiImage.getLink(comment.user.data.profileImage)
  158.                     : require('../../../assets/imgs/accnt_avatar_defaut.png')
  159.                 }
  160.               />
  161.               <View style={styles.viewAuthor}>
  162.                 <Text style={styles.commentAuthor}>{_.get(comment.user, 'data.name', '')} </Text>
  163.               </View>
  164.             </View>
  165.             {
  166.               (text && text !== '') ?
  167.                 <View style={styles.viewContainParse}>
  168.                  <ParsedText
  169.                       style={styles.text}
  170.                       parse={[
  171.                         { type: 'url', style: styles.url, onPress: this.handleUrlPress },
  172.                         { type: 'email', style: styles.email, onPress: this.handleEmailPress },
  173.                         {
  174.                           pattern: /\[(@[^:]+):([^\]]+)\]/i,
  175.                           style: styles.username,
  176.                           onPress: this.handleNamePress,
  177.                           renderText: this.renderText
  178.                         },
  179.                         { pattern: /#(\w+)/, style: styles.hashTag }
  180.                       ]}
  181.                     >
  182.                     {text}
  183.                   </ParsedText>
  184.                 </View> : null
  185.             }
  186.             {
  187.               ( (comment.data|| {} ).images && (comment.data|| {} ).images.length > 0 ) &&
  188.               <TouchableOpacity
  189.                 onPress={() => this._onPressImage((comment.data || {}).images[0])}
  190.                 style={[styles.touchImageComment]}>
  191.                 <FastImage
  192.                   style={{
  193.                     width: SCREEN.width - SIZE_IMAGE - 15*3,
  194.                     height:180
  195.                   }}
  196.                   source={ UtiImage.getUrlImageStream( (comment.data|| {} ).images[0])}
  197.                 />
  198.               </TouchableOpacity>
  199.             }
  200.           </TouchableOpacity>
  201.  
  202.           <View style={styles.viewContain}>
  203.             <View style={styles.viewBottom}>
  204.               <Text style={styles.commentTime}>{UtilDate.humanizeTimestamp(comment.created_at)}</Text>
  205.               <ReactionLabelLikeChild {...comment} userIdCurrent={this.props.userIdCurrent} props={props} />
  206.               <ReactionLabelCommentChild {...comment} onPressActivity={this._onPressChildComment} />
  207.               <ReactionLabelLikeChild onShowModallize ={(comment) => onShowModallize(comment)} {...comment} isShowIcon={true} userIdCurrent={this.props.userIdCurrent} props={props} />
  208.             </View>
  209.  
  210.             {!this.state.visibleCommentChild && children_counts > 0 && (
  211.               <TouchableOpacity onPress={this._LoadCommentChild} style={styles.touchLabelChildren}>
  212.                 <Text style={styles.labelChildren}>
  213.                   {label_children}
  214.                   <Text style={styles.labelChildrenSecond}> {localization.t('comment_was_reply')}</Text>
  215.                 </Text>
  216.                 <Text style={styles.labelChildrenCount}>
  217.                   {children_counts} {localization.t('comment_response')}
  218.                 </Text>
  219.               </TouchableOpacity>
  220.             )}
  221.             {this.state.visibleCommentChild && (
  222.               <View style={styles.viewChildren}>
  223.                 <Collapsible collapsed={!this.state.visibleCommentChild}>
  224.                   {(this.state.dataCommentChild || []).map((item, index) => {
  225.                     return (
  226.                       <CommentItemChild
  227.                         key={index}
  228.                         userIdCurrent={userIdCurrent}
  229.                         commentParent={comment}
  230.                         onPressReplyChild={this._onPressChildParentComment}
  231.                         comment={item}
  232.                         index={index}
  233.                         onShowModalCommentChild = {this._onShowModalCommentChild}
  234.                       />
  235.                     );
  236.                   })}
  237.                 </Collapsible>
  238.               </View>
  239.             )}
  240.           </View>
  241.           {/* <View  style={{flexDirection:'row'}}>
  242.                      <ReactionToggleLikeChild  {...comment} props={props} />
  243.                      <ReactionToggleHeartChild { ...comment} props = {props} />
  244.                      <ReactionCommentChild {...comment} onPressActivity={this._onPressChildComment} />
  245.             </View> */}
  246.         </View>
  247.         <Modal
  248.           onBackdropPress={() => this.setState({ visibleModal: false })}
  249.           isVisible={this.state.visibleModal}
  250.           onSwipeComplete={() => this.setState({ visibleModal: false })}
  251.           style={styles.bottomModal}
  252.         >
  253.           <ModalContent
  254.             isComment={true}
  255.             isCommentChild={this.state.isCommentChild}
  256.             comment={comment}
  257.             commentChild={this.state.commentChild}
  258.             commentParent={this.state.commentParent}
  259.             onPressEdit={this._onPressEditComment}
  260.             onPressEditCommentChild={this._onPressEditCommentChild}
  261.             activity={this.state.activity}
  262.             userId={this.state.userId}
  263.             activityId={this.state.activityId}
  264.             token={this.state.token}
  265.             doReactionDeleteRequest={this._doReactionDeleteRequest}
  266.             doReactionChildDeleteRequest={this._doReactionChildDeleteRequest}
  267.             closeModal={this._closeModal}
  268.           />
  269.         </Modal>
  270.  
  271.         {/* View Image comment */}
  272.         <ModalImageComment
  273.                 comment={comment}
  274.                 userId={this.props.userIdCurrent}
  275.                 isModalVisible={this.state.isModalVisible}
  276.                 urlViewImage={this.state.urlViewImage}
  277.                 closeModal={this._closeModal}
  278.                 props={props}
  279.         />
  280.       </View>
  281.     );
  282.   }
  283. }
  284. const SIZE_IMAGE = 30;
  285. const styles = StyleSheet.create({
  286.   container: {
  287.     // flexDirection: 'row',
  288.     flex: 1,
  289.     alignItems: 'flex-start',
  290.     paddingTop: 8,
  291.     paddingBottom: 8,
  292.     paddingRight: 15,
  293.     paddingLeft: 15
  294.     // borderBottomColor: '#DADFE3',
  295.     // borderBottomWidth: 1,
  296.   },
  297.   touchImageComment:{
  298.     marginTop:10,
  299.     marginBottom:5,
  300.     marginLeft:SIZE_IMAGE + 15
  301.   },
  302.   viewContain: {
  303.     flex: 1,
  304.     marginHorizontal: 10,
  305.     marginLeft: SIZE_IMAGE + 10
  306.   },
  307.   viewContainParse: {
  308.     marginHorizontal: 10,
  309.     marginLeft: SIZE_IMAGE + 10,
  310.     width: SCREEN.width - SIZE_IMAGE - 20 * 2
  311.   },
  312.   commentText: {
  313.     flex: 1,
  314.     marginLeft: 5,
  315.     paddingTop: 3,
  316.     flexDirection: 'row',
  317.     flexWrap: 'wrap'
  318.   },
  319.   bottomModal: {
  320.     justifyContent: 'flex-end',
  321.     margin: 0
  322.   },
  323.   commentAuthor: {
  324.     fontWeight: '500',
  325.     fontSize: 15,
  326.     fontFamily: 'SFCompactDisplay-Medium',
  327.     color: 'rgb(34,43,69)'
  328.   },
  329.   viewAuthor: {
  330.     justifyContent: 'center',
  331.     marginLeft: 10,
  332.     flex: 1
  333.   },
  334.   commentContent: {
  335.     fontSize: 14
  336.   },
  337.   commentTime: {
  338.     fontSize: 13,
  339.     color: 'rgb(143,155,179)',
  340.     fontWeight: '500',
  341.     fontStyle: 'normal',
  342.     lineHeight: 16,
  343.     fontFamily: 'SFCompactDisplay-Medium',
  344.     marginBottom: 5
  345.   },
  346.   viewBottom: {
  347.     marginTop: 5,
  348.     flexDirection: 'row'
  349.   },
  350.   viewHeader: {
  351.     // flexDirection: 'row',
  352.     // justifyContent:'space-between',
  353.   },
  354.   imageView: {
  355.     width: SIZE_IMAGE,
  356.     height: SIZE_IMAGE,
  357.     borderRadius: SIZE_IMAGE / 2
  358.   },
  359.   text: {
  360.     fontSize: 15,
  361.     fontFamily: 'SFCompactDisplay-Regular',
  362.     // letterSpacing: 0,
  363.     color: 'rgb(34,43,69)'
  364.   },
  365.   phone: {
  366.     color: 'blue',
  367.     textDecorationLine: 'underline'
  368.   },
  369.   name: {
  370.     color: 'red'
  371.   },
  372.  
  373.   username: {
  374.     // color: '#157edc',
  375.     // fontWeight: 'bold'
  376.     fontFamily: 'SFCompactDisplay-Bold',
  377.     fontSize: 15,
  378.     fontWeight: 'bold',
  379.     color: 'rgb(34,43,69)'
  380.   },
  381.  
  382.   magicNumber: {
  383.     fontSize: 42,
  384.     color: 'pink'
  385.   },
  386.  
  387.   hashTag: {
  388.     color: '#157edc',
  389.     fontWeight: 'bold',
  390.     fontStyle: 'italic'
  391.   },
  392.   url: {
  393.     color: 'red',
  394.     textDecorationLine: 'underline'
  395.   },
  396.  
  397.   email: {
  398.     textDecorationLine: 'underline'
  399.   },
  400.   labelChildren: {
  401.     fontFamily: 'SFCompactDisplay-Medium',
  402.     fontSize: 13,
  403.     fontWeight: '500',
  404.     // lineHeight: 16,
  405.     // color: 'rgb(34,43,69)'
  406.     color: '#157edc'
  407.   },
  408.   viewChildren: {
  409.     flex: 1
  410.   },
  411.   touchLabelChildren: {
  412.     minWidth: 36,
  413.     flexDirection: 'row'
  414.   },
  415.   labelChildrenSecond: {
  416.     fontFamily: 'SFCompactDisplay-Medium',
  417.     fontSize: 13,
  418.     fontWeight: '500',
  419.     color: 'rgb(143,155,179)'
  420.   },
  421.   labelChildrenCount: {
  422.     fontFamily: 'SFCompactDisplay-Medium',
  423.     fontSize: 13,
  424.     fontWeight: '500',
  425.     color: 'rgb(143,155,179)',
  426.     marginLeft: 10
  427.   }
  428. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement