blob: a91a04e3eb8d69d7a9c250e7e6228a09b9113425 [file] [log] [blame]
Larbi Gharibe9af9732021-03-31 15:08:01 +01001import React from 'react';
2import Header from '../components/Header'
3import ContactList from '../components/ContactList'
4import MessageList from '../components/MessageList'
5import SendMessageForm from '../components/SendMessageForm'
6import NewContactForm from '../components/NewContactForm'
Adrien Béraud6ecaa402021-04-06 17:37:25 -04007//import Sound from 'react-sound';
Larbi Gharibe9af9732021-03-31 15:08:01 +01008import io from "socket.io-client";
Adrien Béraud995e8022021-04-08 13:46:51 -04009import ConversationList from '../components/ConversationList';
10import CircularProgress from '@material-ui/core/CircularProgress';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040011//const socket = io.connect('http://localhost:3000');
Adrien Béraud995e8022021-04-08 13:46:51 -040012import authManager from '../AuthManager'
Larbi Gharibe9af9732021-03-31 15:08:01 +010013
Adrien Béraud6ecaa402021-04-06 17:37:25 -040014class JamiMessenger extends React.Component {
Larbi Gharibe9af9732021-03-31 15:08:01 +010015
Adrien Béraud995e8022021-04-08 13:46:51 -040016 constructor(props) {
17 super(props)
18 this.accountId = props.accountId || props.match.params.accountId
Larbi Gharibe9af9732021-03-31 15:08:01 +010019 this.state = {
Adrien Béraud995e8022021-04-08 13:46:51 -040020 conversations: undefined,
Larbi Gharibe9af9732021-03-31 15:08:01 +010021 messages: [],
22 sound: false
23 }
24
Adrien Béraud6ecaa402021-04-06 17:37:25 -040025 /*socket.on('connect', () => {
Larbi Gharibe9af9732021-03-31 15:08:01 +010026 console.log("Success !")
Adrien Béraud6ecaa402021-04-06 17:37:25 -040027 })*/
Larbi Gharibe9af9732021-03-31 15:08:01 +010028
Larbi Gharibe9af9732021-03-31 15:08:01 +010029
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éraud995e8022021-04-08 13:46:51 -040037 this.controller = new AbortController()
Larbi Gharibe9af9732021-03-31 15:08:01 +010038 }
39
40 componentDidMount() {
Adrien Béraud995e8022021-04-08 13:46:51 -040041 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éraud6ecaa402021-04-06 17:37:25 -040049 /*socket.on('receivedMessage', (data) => {
50 const message = {
Larbi Gharibe9af9732021-03-31 15:08:01 +010051 senderId: '65f6674b26e5af6ed0b4e92a13b80ff4bbfdf1e8',
52 text: data
53 }
54 this.setState({
55 messages: [...this.state.messages, message],
56 sound: true
57 })
Adrien Béraud6ecaa402021-04-06 17:37:25 -040058 });*/
Larbi Gharibe9af9732021-03-31 15:08:01 +010059 }
60
Adrien Béraud995e8022021-04-08 13:46:51 -040061 componentWillUnmount(){
62 this.controller.abort()
63 this.req = undefined
64 }
65
Larbi Gharibe9af9732021-03-31 15:08:01 +010066 sendMessage(text) {
67 var data = {
68 senderId: 'Me',
69 destinationId: '65f6674b26e5af6ed0b4e92a13b80ff4bbfdf1e8',
70 text: text
71 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040072 //socket.emit("SendMessage", data);
Larbi Gharibe9af9732021-03-31 15:08:01 +010073 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éraud995e8022021-04-08 13:46:51 -040083 {this.state.conversations ? <ConversationList conversations={this.state.conversations} /> : <CircularProgress />}
Larbi Gharibe9af9732021-03-31 15:08:01 +010084 <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éraud6ecaa402021-04-06 17:37:25 -0400100export default JamiMessenger;