Created
May 6, 2020 15:36
-
-
Save siljanoskam/753dc4632851b6acb9d7fbdb68618794 to your computer and use it in GitHub Desktop.
Main component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {Component} from 'react'; | |
import ReactDOM from 'react-dom'; | |
import Task from './Task'; | |
const totalNumberOfUsers = 25; | |
// for now we'll get the userId randomly until we implement authentication | |
const userId = Math.floor(Math.random() * Math.floor(totalNumberOfUsers)); | |
class Main extends Component { | |
constructor() { | |
super(); | |
// currentTask keeps track of the product currently displayed | |
this.state = { | |
tasks: [] | |
} | |
} | |
componentDidMount() { | |
fetch(`/api/tasks`) | |
.then(response => { | |
return response.json(); | |
}) | |
.then(tasks => { | |
this.setState({tasks}); | |
}); | |
} | |
handleClick(task) { | |
this.setState({currentTask: task}); | |
} | |
renderTasks() { | |
return this.state.tasks.map(task => { | |
return ( | |
<li onClick={() => this.handleClick(task)} | |
key={task.id} | |
className={task.status ? "task strikethrough" : "task"}> | |
{task.title} | |
</li> | |
); | |
}) | |
} | |
render() { | |
return ( | |
<div id="app" className="container p-5"> | |
<div className="row"> | |
<div className="col-4"> | |
<h3> All tasks </h3> | |
<ul> | |
{this.renderTasks()} | |
</ul> | |
</div> | |
<div className="col-8"> | |
<Task task={this.state.currentTask ?? ''}/> | |
</div> | |
</div> | |
</div> | |
); | |
} | |
} | |
export default Main; | |
if (document.getElementById('root')) { | |
ReactDOM.render(<Main/>, document.getElementById('root')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment