Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 1 | import React from 'react'; |
| 2 | import Header from '../components/Header' |
| 3 | import ContactList from '../components/ContactList' |
| 4 | import MessageList from '../components/MessageList' |
| 5 | import SendMessageForm from '../components/SendMessageForm' |
| 6 | import NewContactForm from '../components/NewContactForm' |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 7 | //import Sound from 'react-sound'; |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 8 | import io from "socket.io-client"; |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 9 | import ConversationList from '../components/ConversationList'; |
| 10 | import CircularProgress from '@material-ui/core/CircularProgress'; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 11 | //const socket = io.connect('http://localhost:3000'); |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 12 | import authManager from '../AuthManager' |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 13 | |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 14 | class JamiMessenger extends React.Component { |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 15 | |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 16 | constructor(props) { |
| 17 | super(props) |
| 18 | this.accountId = props.accountId || props.match.params.accountId |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 19 | this.state = { |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 20 | conversations: undefined, |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 21 | messages: [], |
| 22 | sound: false |
| 23 | } |
| 24 | |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 25 | /*socket.on('connect', () => { |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 26 | console.log("Success !") |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 27 | })*/ |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 28 | |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 29 | |
| 30 | //this.socket = socketIOClient(ENDPOINT); |
| 31 | //this.socket.on("FromAPI", data => { |
| 32 | // this.setState({ |
| 33 | // messages: [...this.state.messages, data] |
| 34 | // }) |
| 35 | //}); |
| 36 | this.sendMessage = this.sendMessage.bind(this) |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 37 | this.controller = new AbortController() |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | componentDidMount() { |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 41 | if (this.req === undefined) { |
| 42 | this.req = authManager.fetch(`/api/accounts/${this.accountId}/conversations`, {signal: this.controller.signal}) |
| 43 | .then(res => res.json()) |
| 44 | .then(result => { |
| 45 | console.log(result) |
| 46 | this.setState({conversations: result}) |
| 47 | }) |
| 48 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 49 | /*socket.on('receivedMessage', (data) => { |
| 50 | const message = { |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 51 | senderId: '65f6674b26e5af6ed0b4e92a13b80ff4bbfdf1e8', |
| 52 | text: data |
| 53 | } |
| 54 | this.setState({ |
| 55 | messages: [...this.state.messages, message], |
| 56 | sound: true |
| 57 | }) |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 58 | });*/ |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 61 | componentWillUnmount(){ |
| 62 | this.controller.abort() |
| 63 | this.req = undefined |
| 64 | } |
| 65 | |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 66 | sendMessage(text) { |
| 67 | var data = { |
| 68 | senderId: 'Me', |
| 69 | destinationId: '65f6674b26e5af6ed0b4e92a13b80ff4bbfdf1e8', |
| 70 | text: text |
| 71 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 72 | //socket.emit("SendMessage", data); |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 73 | console.log(data.text); |
| 74 | this.setState({ |
| 75 | messages: [...this.state.messages, data], |
| 76 | sound: false |
| 77 | }) |
| 78 | } |
| 79 | render() { |
| 80 | return ( |
| 81 | <div className="app" > |
| 82 | <Header /> |
Adrien Béraud | 995e802 | 2021-04-08 13:46:51 -0400 | [diff] [blame] | 83 | {this.state.conversations ? <ConversationList conversations={this.state.conversations} /> : <CircularProgress />} |
Larbi Gharib | e9af973 | 2021-03-31 15:08:01 +0100 | [diff] [blame] | 84 | <MessageList messages={this.state.messages} /> |
| 85 | <SendMessageForm sendMessage={this.sendMessage} /> |
| 86 | <NewContactForm /> |
| 87 | {this.state.sound && <Sound |
| 88 | url="stairs.mp3" /*https://notificationsounds.com/message-tones/stairs-567*/ |
| 89 | playStatus={Sound.status.PLAYING} |
| 90 | playFromPosition={0 /* in milliseconds */} |
| 91 | onLoading={this.handleSongLoading} |
| 92 | onPlaying={this.handleSongPlaying} |
| 93 | onFinishedPlaying={this.handleSongFinishedPlaying} |
| 94 | />} |
| 95 | </div> |
| 96 | ) |
| 97 | } |
| 98 | } |
| 99 | |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 100 | export default JamiMessenger; |