Comentarios.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import React, { Component } from 'react';
  2. import { Text, ListView, View, TouchableOpacity, Image, TextInput } from 'react-native';
  3. import { Actions } from 'react-native-router-flux';
  4. import { Card, CardSection, Button } from '../../components/common';
  5. class Comentarios extends Component {
  6. componentWillMount() {
  7. this.createDataSource(this.props);
  8. }
  9. componentWillReceiveProps(nextProps) {
  10. this.createDataSource(nextProps);
  11. }
  12. createDataSource({comentarios, respuestas}){
  13. const ds = new ListView.DataSource({
  14. rowHasChanged: (r1,r2) => r1 !== r2
  15. });
  16. let c = comentarios.map(function(el) {
  17. el.respuestas = respuestas.filter(function(r){
  18. return (r.parent == el._id);
  19. })
  20. return el;
  21. });
  22. console.log(c);
  23. this.dataSource = ds.cloneWithRows(c);
  24. }
  25. imageUri(id) {
  26. return `https://plataforma.especificosba.com.ar/images/perfiles/${id}.jpg`;
  27. }
  28. renderRespuesta(item, key) {
  29. return (
  30. <View key={item._id} style={{flex:1}}>
  31. <View style={{flexDirection: 'row'}}>
  32. <Image resizeMode='contain' source={{ uri: this.imageUri(item.userid) }} style={{flex:1, height:40}}/>
  33. <View style={{flex:2, justifyContent: 'center', alignItems:'center'}}>
  34. <Text>{item.nombreUsuario}</Text>
  35. <Text>{item.fecha}</Text>
  36. </View>
  37. </View>
  38. <CardSection>
  39. <Text>{item.texto}</Text>
  40. </CardSection>
  41. </View>
  42. );
  43. }
  44. renderComentario(item) {
  45. const uri = this.imageUri(item.userid);
  46. let Arr = item.respuestas.map( el => {
  47. return this.renderRespuesta(el);
  48. });
  49. if ( Arr != undefined && Arr.length > 0 ) {
  50. Arr = (
  51. <CardSection style={{backgroundColor:'#eef'}}>
  52. {Arr}
  53. </CardSection>);
  54. }
  55. return (
  56. <Card>
  57. <CardSection>
  58. <Image resizeMode='contain' source={{ uri }} style={{flex:1, height:70}}/>
  59. <View style={{flex:2, justifyContent: 'center', alignItems:'center'}}>
  60. <Text style={{alignSelf:'center'}}>{item.nombreUsuario}</Text>
  61. <Text style={{alignSelf:'center'}}>{item.fecha}</Text>
  62. </View>
  63. </CardSection>
  64. <CardSection>
  65. <Text>{item.texto}</Text>
  66. </CardSection>
  67. {Arr}
  68. </Card>
  69. );
  70. }
  71. renderNewComment() {
  72. return (
  73. <Card>
  74. <CardSection>
  75. <Text style={{justifyContent:'center'}}>
  76. Dejar un comentario
  77. </Text>
  78. </CardSection>
  79. <CardSection>
  80. <TextInput
  81. autoCorrect={true}
  82. multiline={true}
  83. style={{flex:1}}
  84. />
  85. </CardSection>
  86. <CardSection>
  87. <Button>Enviar</Button>
  88. </CardSection>
  89. </Card>
  90. );
  91. }
  92. render() {
  93. if (this.props.comentarios === undefined ||
  94. this.props.comentarios.length == 0 ) {
  95. return (
  96. <View style={{margin:10}}>
  97. {this.renderNewComment()}
  98. <Text style={{fontSize:14,alignSelf:'center'}}>
  99. Aún no hay comentarios
  100. </Text>
  101. </View>
  102. );
  103. }
  104. return (
  105. <ListView
  106. enableEmptySections
  107. renderHeader={this.renderNewComment}
  108. dataSource={this.dataSource}
  109. renderRow={this.renderComentario.bind(this)}
  110. />
  111. );
  112. }
  113. }
  114. export default Comentarios;