Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 1 | import React, { useEffect, useState } from 'react'; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 2 | import MessageList from './MessageList'; |
| 3 | import SendMessageForm from './SendMessageForm'; |
| 4 | import authManager from '../AuthManager' |
| 5 | import Conversation from '../../../model/Conversation'; |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 6 | import LoadingPage from './loading'; |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame^] | 7 | import io from "socket.io-client"; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 8 | |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 9 | const ConversationView = props => { |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame^] | 10 | const [loadingMesages, setLoadingMesages] = useState(false) |
| 11 | const [socket, setSocket] = useState(undefined) |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 12 | const [state, setState] = useState({ |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 13 | loaded: false, |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 14 | error: false, |
| 15 | conversation: undefined |
| 16 | }) |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 17 | |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 18 | useEffect(() => { |
| 19 | const controller = new AbortController() |
| 20 | authManager.fetch(`/api/accounts/${props.accountId}/conversations/${props.conversationId}`, {signal: controller.signal}) |
| 21 | .then(res => res.json()) |
| 22 | .then(result => { |
| 23 | console.log(result) |
| 24 | setState({ |
| 25 | loaded: true, |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame^] | 26 | conversation: Conversation.from(props.accountId, result) |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 27 | }) |
| 28 | }, error => { |
| 29 | console.log(`get error ${error}`) |
| 30 | setState({ |
| 31 | loaded: true, |
| 32 | error: true |
| 33 | }) |
| 34 | }) |
| 35 | return () => controller.abort() |
| 36 | }, [props.accountId, props.conversationId]) |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 37 | |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame^] | 38 | useEffect(() => { |
| 39 | console.log("io.connect") |
| 40 | const socket = io() |
| 41 | setSocket(socket) |
| 42 | return () => { |
| 43 | console.log("io.disconnect") |
| 44 | socket.disconnect() |
| 45 | setSocket(undefined) |
| 46 | } |
| 47 | }, []) |
| 48 | |
| 49 | useEffect(() => { |
| 50 | if (!state.conversation) |
| 51 | return |
| 52 | console.log(`io set conversation ${state.conversation.getId()} `+ socket) |
| 53 | if (socket) |
| 54 | socket.emit('conversation', { accountId: state.conversation.getAccountId(), conversationId: state.conversation.getId() }) |
| 55 | socket.off('newMessage') |
| 56 | socket.on('newMessage', (data) => { |
| 57 | console.log("newMessage") |
| 58 | console.log(data) |
| 59 | setState(state => { |
| 60 | if (state.conversation) |
| 61 | state.conversation.addMessage(data) |
| 62 | return {...state} |
| 63 | }) |
| 64 | }) |
| 65 | }, [state.conversation ? state.conversation.getId() : "", socket]) |
| 66 | |
| 67 | useEffect(() => { |
| 68 | if (!loadingMesages || !state.conversation) |
| 69 | return |
| 70 | console.log(`Load more messages`) |
| 71 | const controller = new AbortController() |
| 72 | authManager.fetch(`/api/accounts/${state.conversation.getAccountId()}/conversations/${state.conversation.getId()}/messages`, {signal: controller.signal}) |
| 73 | .then(res => res.json()) |
| 74 | .then(messages => { |
| 75 | console.log(messages) |
| 76 | setLoadingMesages(false) |
| 77 | if (state.conversation) |
| 78 | state.conversation.addLoadedMessages(messages) |
| 79 | setState(state) |
| 80 | }).catch(e => console.log(e)) |
| 81 | return () => controller.abort() |
| 82 | }, [state, loadingMesages]) |
| 83 | |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 84 | const sendMessage = (message) => { |
| 85 | authManager.fetch(`/api/accounts/${props.accountId}/conversations/${props.conversationId}`, { |
| 86 | headers: { |
| 87 | 'Accept': 'application/json', |
| 88 | 'Content-Type': 'application/json' |
| 89 | }, |
| 90 | method:"POST", |
| 91 | body: JSON.stringify({ message }) |
| 92 | }) |
| 93 | } |
| 94 | |
| 95 | if (state.loaded === false) { |
| 96 | return <LoadingPage /> |
| 97 | } else if (state.error === true) { |
| 98 | return <div>Error loding {props.conversationId}</div> |
| 99 | } else { |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 100 | return <div className="messenger"> |
Adrien Béraud | abba2e5 | 2021-04-24 21:39:56 -0400 | [diff] [blame^] | 101 | <MessageList conversation={state.conversation} loading={loadingMesages} loadMore={() => setLoadingMesages(true)} messages={state.conversation.getMessages()} /> |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 102 | <SendMessageForm onSend={sendMessage} /> |
Adrien Béraud | 4e287b9 | 2021-04-24 16:15:56 -0400 | [diff] [blame] | 103 | </div> |
Adrien Béraud | 5e9e19b | 2021-04-22 01:38:53 -0400 | [diff] [blame] | 104 | } |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | export default ConversationView |