Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 1 | import CircularProgress from '@material-ui/core/CircularProgress'; |
| 2 | import React from 'react'; |
| 3 | import MessageList from './MessageList'; |
| 4 | import SendMessageForm from './SendMessageForm'; |
| 5 | import authManager from '../AuthManager' |
| 6 | import Conversation from '../../../model/Conversation'; |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 7 | import LoadingPage from './loading'; |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 8 | |
| 9 | class ConversationView extends React.Component { |
| 10 | constructor(props) { |
| 11 | super(props) |
| 12 | this.state = { |
| 13 | loaded:false, |
| 14 | error: false, |
| 15 | messages:[] |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | componentDidMount() { |
| 20 | this.controller = new AbortController() |
| 21 | if (!this.state.loaded) { |
| 22 | authManager.fetch(`/api/accounts/${this.props.accountId}/conversations/${this.props.conversationId}`, {signal: this.controller.signal}) |
| 23 | .then(res => res.json()) |
| 24 | .then(result => { |
| 25 | console.log(result) |
| 26 | this.setState({ |
| 27 | loaded: true, |
| 28 | conversation: Conversation.from(this.props.accountId, result)// result.map(account => Account.from(account)), |
| 29 | }) |
| 30 | }, error => { |
| 31 | console.log(`get error ${error}`) |
| 32 | this.setState({ |
| 33 | loaded: true, |
| 34 | error: true |
| 35 | }) |
| 36 | }) |
| 37 | } |
| 38 | } |
| 39 | componentDidUpdate(prevProps, prevState) { |
| 40 | console.log("componentDidUpdate " + this.props.conversationId) |
| 41 | if (this.props.conversationId !== prevProps.conversationId) { |
| 42 | if (this.state.loaded === true) { |
| 43 | this.setState({ |
| 44 | loaded:false, |
| 45 | error: false, |
| 46 | messages:[] |
| 47 | }) |
| 48 | } |
| 49 | this.controller = new AbortController() |
| 50 | authManager.fetch(`/api/accounts/${this.props.accountId}/conversations/${this.props.conversationId}`, {signal: this.controller.signal}) |
| 51 | .then(res => res.json()) |
| 52 | .then(result => { |
| 53 | console.log(result) |
| 54 | this.setState({ |
| 55 | loaded: true, |
| 56 | conversation: Conversation.from(this.props.accountId, result)// result.map(account => Account.from(account)), |
| 57 | }) |
| 58 | }, error => { |
| 59 | console.log(`get error ${error}`) |
| 60 | this.setState({ |
| 61 | loaded: true, |
| 62 | error: true |
| 63 | }) |
| 64 | }) |
| 65 | } |
| 66 | } |
| 67 | componentWillUnmount() { |
| 68 | this.controller.abort() |
| 69 | } |
| 70 | |
| 71 | render() { |
| 72 | if (this.state.loaded === false) { |
Adrien Béraud | 150b478 | 2021-04-21 19:40:59 -0400 | [diff] [blame] | 73 | return <LoadingPage /> |
Adrien Béraud | 35e7d7c | 2021-04-13 03:28:39 -0400 | [diff] [blame] | 74 | } else if (this.state.error === true) { |
| 75 | return <div>Error loding {this.props.conversationId}</div> |
| 76 | } else { |
| 77 | return <React.Fragment> |
| 78 | <MessageList conversation={this.state.conversation} messages={this.state.messages} /> |
| 79 | <SendMessageForm sendMessage={this.sendMessage} /> |
| 80 | </React.Fragment> |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | export default ConversationView |