blob: f5e5104f378d866a413017c9c17f14118a752d90 [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éraud35e7d7c2021-04-13 03:28:39 -04007
Adrien Béraud6ecaa402021-04-06 17:37:25 -04008//import Sound from 'react-sound';
Larbi Gharibe9af9732021-03-31 15:08:01 +01009import io from "socket.io-client";
Adrien Béraud995e8022021-04-08 13:46:51 -040010import ConversationList from '../components/ConversationList';
11import CircularProgress from '@material-ui/core/CircularProgress';
Adrien Béraud6ecaa402021-04-06 17:37:25 -040012//const socket = io.connect('http://localhost:3000');
Adrien Béraud995e8022021-04-08 13:46:51 -040013import authManager from '../AuthManager'
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040014import Conversation from '../../../model/Conversation'
15import Contact from '../../../model/Contact'
16import ConversationView from '../components/ConversationView';
17import AddContactPage from './addContactPage.jsx';
Larbi Gharibe9af9732021-03-31 15:08:01 +010018
Adrien Béraud6ecaa402021-04-06 17:37:25 -040019class JamiMessenger extends React.Component {
Larbi Gharibe9af9732021-03-31 15:08:01 +010020
Adrien Béraud995e8022021-04-08 13:46:51 -040021 constructor(props) {
22 super(props)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040023
Larbi Gharibe9af9732021-03-31 15:08:01 +010024 this.state = {
Adrien Béraud995e8022021-04-08 13:46:51 -040025 conversations: undefined,
Larbi Gharibe9af9732021-03-31 15:08:01 +010026 messages: [],
27 sound: false
28 }
29
Adrien Béraud6ecaa402021-04-06 17:37:25 -040030 /*socket.on('connect', () => {
Larbi Gharibe9af9732021-03-31 15:08:01 +010031 console.log("Success !")
Adrien Béraud6ecaa402021-04-06 17:37:25 -040032 })*/
Larbi Gharibe9af9732021-03-31 15:08:01 +010033
Larbi Gharibe9af9732021-03-31 15:08:01 +010034
35 //this.socket = socketIOClient(ENDPOINT);
36 //this.socket.on("FromAPI", data => {
37 // this.setState({
38 // messages: [...this.state.messages, data]
39 // })
40 //});
41 this.sendMessage = this.sendMessage.bind(this)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040042 this.handleSearch = this.handleSearch.bind(this)
Adrien Béraud995e8022021-04-08 13:46:51 -040043 this.controller = new AbortController()
Larbi Gharibe9af9732021-03-31 15:08:01 +010044 }
45
46 componentDidMount() {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040047 const accountId = this.props.accountId || this.props.match.params.accountId
48 const conversationId = this.props.conversationId || this.props.match.params.conversationId
49
Adrien Béraud995e8022021-04-08 13:46:51 -040050 if (this.req === undefined) {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040051 this.req = authManager.fetch(`/api/accounts/${accountId}/conversations`, {signal: this.controller.signal})
Adrien Béraud995e8022021-04-08 13:46:51 -040052 .then(res => res.json())
53 .then(result => {
54 console.log(result)
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040055 this.setState({conversations: Object.values(result).map(c => Conversation.from(accountId, c))})
Adrien Béraud995e8022021-04-08 13:46:51 -040056 })
57 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -040058 /*socket.on('receivedMessage', (data) => {
59 const message = {
Larbi Gharibe9af9732021-03-31 15:08:01 +010060 senderId: '65f6674b26e5af6ed0b4e92a13b80ff4bbfdf1e8',
61 text: data
62 }
63 this.setState({
64 messages: [...this.state.messages, message],
65 sound: true
66 })
Adrien Béraud6ecaa402021-04-06 17:37:25 -040067 });*/
Larbi Gharibe9af9732021-03-31 15:08:01 +010068 }
69
Adrien Béraud995e8022021-04-08 13:46:51 -040070 componentWillUnmount(){
71 this.controller.abort()
72 this.req = undefined
73 }
74
Adrien Béraud35e7d7c2021-04-13 03:28:39 -040075 handleSearch(query) {
76 const accountId = this.props.accountId || this.props.match.params.accountId
77 const conversationId = this.props.conversationId || this.props.match.params.conversationId
78
79 authManager.fetch(`/api/accounts/${accountId}/ns/name/${query}`)
80 .then(response => {
81 if (response.status === 200) {
82 return response.json()
83 } else {
84 throw new Error(response.status)
85 }
86 }).then(response => {
87 console.log(response)
88 const contact = new Contact(response.address)
89 contact.setRegisteredName(response.name)
90 this.setState({searchResult: contact ? Conversation.fromSingleContact(accountId, contact) : undefined})
91 }).catch(e => {
92 this.setState({searchResult: e})
93 })
94 }
95
Larbi Gharibe9af9732021-03-31 15:08:01 +010096 sendMessage(text) {
97 var data = {
98 senderId: 'Me',
99 destinationId: '65f6674b26e5af6ed0b4e92a13b80ff4bbfdf1e8',
100 text: text
101 }
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400102 //socket.emit("SendMessage", data);
Larbi Gharibe9af9732021-03-31 15:08:01 +0100103 console.log(data.text);
104 this.setState({
105 messages: [...this.state.messages, data],
106 sound: false
107 })
108 }
109 render() {
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400110 const accountId = this.props.accountId || this.props.match.params.accountId
111 const conversationId = this.props.conversationId || this.props.match.params.conversationId
112 const contactId = this.props.contactId || this.props.match.params.contactId
113
114 console.log("JamiMessenger render " + conversationId)
115 console.log(this.props)
116 console.log(this.state)
117
Larbi Gharibe9af9732021-03-31 15:08:01 +0100118 return (
119 <div className="app" >
120 <Header />
Adrien Béraud35e7d7c2021-04-13 03:28:39 -0400121 {this.state.conversations ?
122 <ConversationList search={this.state.searchResult} conversations={this.state.conversations} accountId={accountId} /> :
123 <CircularProgress />}
124 <NewContactForm onChange={query => this.handleSearch(query)} />
125 {conversationId && <ConversationView accountId={accountId} conversationId={conversationId} />}
126 {contactId && <AddContactPage accountId={accountId} contactId={contactId} />}
Larbi Gharibe9af9732021-03-31 15:08:01 +0100127 {this.state.sound && <Sound
128 url="stairs.mp3" /*https://notificationsounds.com/message-tones/stairs-567*/
129 playStatus={Sound.status.PLAYING}
130 playFromPosition={0 /* in milliseconds */}
131 onLoading={this.handleSongLoading}
132 onPlaying={this.handleSongPlaying}
133 onFinishedPlaying={this.handleSongFinishedPlaying}
134 />}
135 </div>
136 )
137 }
138}
139
Adrien Béraud6ecaa402021-04-06 17:37:25 -0400140export default JamiMessenger;