Files
old-chat-example/src/App.js
Hamza Iseric 5dc6c76750 prva verzija
2018-12-07 01:39:20 +01:00

45 lines
1.0 KiB
JavaScript

// ./src/App.js
import React, { Component } from 'react'
import UsernameForm from './UsernameForm'
import Chat from './Chat'
class App extends Component {
state = {
currentUsername: null,
currentId: null,
currentScreen: 'usernameForm'
}
onUsernameSubmitted = username => {
fetch('http://localhost:3001/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username })
})
.then(response => response.json())
.then(data => {
this.setState({
currentId: data.id,
currentUsername: data.name,
currentScreen: 'chat'
})
})
.catch(error => {
console.error('error', error)
})
}
render() {
if (this.state.currentScreen === 'usernameForm') {
return <UsernameForm handleSubmit={this.onUsernameSubmitted} />
}
if (this.state.currentScreen === 'chat') {
return <Chat currentId={this.state.currentId} />
}
}
}
export default App;