configuring to work with new users table in db and working on large msgCont problem

This commit is contained in:
talksik
2018-08-04 14:43:34 -07:00
parent 661387c7a5
commit a7b97e4aa1
+22 -20
View File
@@ -46,20 +46,14 @@ export default class Chat extends Component<Props> {
} }
componentDidMount() { componentDidMount() {
this.socket = io('https://murmuring-sierra-86040.herokuapp.com', {jsonp: false}); this.socket = io('http://10.0.2.2:3000', {jsonp: false});
this.socket.on('connect', () => { this.socket.on('connect', () => {
this.socket.emit('initial', this.props.position); this.socket.emit('initial', {position: this.props.position, avatar: this.props.avatar});
});
fetch('https://murmuring-sierra-86040.herokuapp.com/userid') this.socket.on("userId", (obj) => {
.then((response) => response.json()) console.log("User Id assigned: " + obj.userId);
.then((responseJson) => { this.setState({userId: obj.userId});
console.log(responseJson);
this.setState({userId: responseJson.userId});
})
.catch((error) => {
console.log(error);
this.setState({userId: 1}); // TODO: error popup
});
}); });
this.socket.on('initMessages', (allMsg) => { this.socket.on('initMessages', (allMsg) => {
@@ -77,8 +71,10 @@ export default class Chat extends Component<Props> {
addMessage = (msg) => { addMessage = (msg) => {
console.log('New message received: '); console.log('New message received: ');
var oldDOM = this.state.messagesDOM; var oldDOM = this.state.messagesDOM;
var newMessage = this.createMessage(msg); var newMessage = this.createMessage(msg);
if (this.state.prevMessageUser != msg.userId && this.state.prevMessageUser) {
if (this.state.prevMessageUser != msg.user_id && this.state.prevMessageUser) {
var dotSeparator = ( var dotSeparator = (
<View key={msg.text} style={styles.separatorCont}> <View key={msg.text} style={styles.separatorCont}>
<Text>. .</Text> <Text>. .</Text>
@@ -86,30 +82,34 @@ export default class Chat extends Component<Props> {
); );
oldDOM.push(dotSeparator); oldDOM.push(dotSeparator);
} }
oldDOM.push(newMessage); oldDOM.push(newMessage);
this.setState({ this.setState({
messagesDOM: oldDOM, messagesDOM: oldDOM,
prevMessageUser: msg.userId prevMessageUser: msg.user_id
}); });
} }
sendMessage = () => { sendMessage = () => {
console.log(this.state.userId); console.log(this.state.userId);
console.log('Message to send: ' + this.state.messageToSend); console.log('Message to send: ' + this.state.messageToSend);
this.socket.emit('newMessage', { this.socket.emit('newMessage', {
userId: this.state.userId, user_id: this.state.userId,
text: this.state.messageToSend, text: this.state.messageToSend,
lng: this.props.position.long, long: this.props.position.long,
lat: this.props.position.lat, lat: this.props.position.lat,
avatar: this.props.avatar avatar: this.props.avatar
}); });
this.setState({ this.setState({
messageToSend: '' messageToSend: ''
}); });
} }
createMessage = (msg) => { createMessage = (msg) => {
var isUser = msg.userId == this.state.userId; var isUser = msg.user_id == this.state.userId;
var contStyle = isUser ? styles.userMsg : styles.otherMsg; var contStyle = isUser ? styles.userMsg : styles.otherMsg;
var txtContStyle = isUser ? styles.userMsgBox : styles.otherMsgBox; var txtContStyle = isUser ? styles.userMsgBox : styles.otherMsgBox;
var txtStyle = isUser ? styles.userTxt : null; var txtStyle = isUser ? styles.userTxt : null;
@@ -119,7 +119,7 @@ export default class Chat extends Component<Props> {
<Image style={styles.avatar} source={{uri: avatarLink}} /> <Image style={styles.avatar} source={{uri: avatarLink}} />
</View> </View>
); );
var avatarAlready = this.state.prevMessageUser == msg.userId; var avatarAlready = this.state.prevMessageUser == msg.user_id;
var result = ( var result = (
<View key={msg.id} <View key={msg.id}
style={[contStyle, styles.msgCont, avatarAlready ? styles.avatarAlready : null]}> style={[contStyle, styles.msgCont, avatarAlready ? styles.avatarAlready : null]}>
@@ -202,7 +202,8 @@ const styles = {
}, },
msgCont: { msgCont: {
flexDirection: 'row', flexDirection: 'row',
alignItems: 'center' alignItems: 'center',
margin: 2
}, },
userMsg: { userMsg: {
alignSelf: 'flex-end' alignSelf: 'flex-end'
@@ -221,7 +222,8 @@ const styles = {
margin: 5 margin: 5
}, },
msgTxtCont: { msgTxtCont: {
margin: 3, width: 0,
flex: 1,
padding: 12, padding: 12,
borderBottomRightRadius: 15, borderBottomRightRadius: 15,
borderBottomLeftRadius: 15 borderBottomLeftRadius: 15