This commit is contained in:
data 2019-07-10 22:08:28 +02:00
parent 4e90f48cb8
commit e56afa8869
5 changed files with 132 additions and 96 deletions

View File

@ -8,11 +8,7 @@ import {
selectArticle, selectArticle,
} from './controllers/articles' } from './controllers/articles'
import { import { addTag, selectTag, removeTag } from './controllers/tags'
addTag,
selectTag,
removeTag,
} from './controllers/tags'
import { import {
createTask, createTask,
@ -59,7 +55,7 @@ const defaultState = {
newTask: '', newTask: '',
newProject: '', newProject: '',
project: {}, project: {},
newTag: '' newTag: '',
} }
class App extends React.Component { class App extends React.Component {
@ -160,7 +156,15 @@ class App extends React.Component {
const projects = await this.fetchProjects() const projects = await this.fetchProjects()
const comments = await this.fetchComments() const comments = await this.fetchComments()
const votes = await this.fetchVotes() const votes = await this.fetchVotes()
await this.setState({ loading: false, articles, tags, tasks, projects, comments, votes }) await this.setState({
loading: false,
articles,
tags,
tasks,
projects,
comments,
votes,
})
} }
componentDidMount() { componentDidMount() {
@ -185,8 +189,7 @@ class App extends React.Component {
renderError() { renderError() {
if (!this.state.error) { if (!this.state.error) {
return <div>There was an error.</div> return <div>There was an error.</div>
} } else if (this.state.error.message === 'Network Error') {
else if (this.state.error.message === 'Network Error') {
alert(`Failed to reach backend at\n${this.state.error.config.url}.`) alert(`Failed to reach backend at\n${this.state.error.config.url}.`)
} else { } else {
return ( return (
@ -214,7 +217,8 @@ class App extends React.Component {
) )
} }
renderArticles() { renderArticles() {
return (<Articles return (
<Articles
navigate={this.navigate} navigate={this.navigate}
handleChange={this.handleChange} handleChange={this.handleChange}
createArticle={this.createArticle} createArticle={this.createArticle}
@ -226,7 +230,8 @@ class App extends React.Component {
) )
} }
renderArticle() { renderArticle() {
return (<Article return (
<Article
navigate={this.navigate} navigate={this.navigate}
addTag={this.addTag} addTag={this.addTag}
removeTag={this.removeTag} removeTag={this.removeTag}
@ -238,7 +243,8 @@ class App extends React.Component {
) )
} }
renderUpdateArticle() { renderUpdateArticle() {
return (<UpdateArticle return (
<UpdateArticle
handleChange={this.handleChange} handleChange={this.handleChange}
updateArticle={this.updateArticle} updateArticle={this.updateArticle}
navigate={this.navigate} navigate={this.navigate}

View File

@ -11,23 +11,32 @@ class Article extends React.Component {
render() { render() {
return ( return (
<div> <div>
<Tags <Tags {...this.props} />
{...this.props}
/>
<h2>{this.state.title}</h2> <h2>{this.state.title}</h2>
<div className="mb-3">{this.state.text}</div> <div className="mb-3">{this.state.text}</div>
<div> <div>
<button className="btn btn-primary" onClick={() => this.props.navigate('articles')}>Back</button> <button
<button className="btn btn-primary ml-1" onClick={() => this.props.navigate('update-article')}>Edit</button> className="btn btn-primary"
onClick={() => this.props.navigate('articles')}
>
Back
</button>
<button
className="btn btn-primary ml-1"
onClick={() => this.props.navigate('update-article')}
>
Edit
</button>
</div> </div>
<h2 className="mt-3">Comments</h2> <h2 className="mt-3">Comments</h2>
<ul className="list-group"> <ul className="list-group">
{this.props.comments ? this.props.comments.map(comment=>( {this.props.comments
? this.props.comments.map(comment => (
<li className="list-group-item" key={comment.id}> <li className="list-group-item" key={comment.id}>
{comment.text} {comment.text}
</li> </li>
)) : "No comments yet." ))
} : 'No comments yet.'}
</ul> </ul>
</div> </div>
) )

View File

@ -26,18 +26,22 @@ function Articles(props) {
> >
{article.title}{' '} {article.title}{' '}
</button> </button>
<button <button className="btn ml-1 float-right">
className="btn ml-1 float-right" {props.comments.map(c => c.articleId === article.id).length}{' '}
> comment(s)
{props.comments.map(c => c.articleId === article.id).length} comment(s)
</button> </button>
{props.tags && props.tags.filter(t => t.articleId === article.id).map(tag => ( {props.tags &&
<button className="btn btn-outline-success mr-1 float-right" key={tag.id} props.tags
onClick={(e) => props.selectTag(tag.id)}> .filter(t => t.articleId === article.id)
.map(tag => (
<button
className="btn btn-outline-success mr-1 float-right"
key={tag.id}
onClick={e => props.selectTag(tag.id)}
>
{tag.name} {tag.name}
</button> </button>
)) ))}
}
</li> </li>
))} ))}

View File

@ -9,14 +9,23 @@ class Tags extends React.Component {
render() { render() {
return ( return (
<div> <div>
{this.props.tags &&
{this.props.tags && this.props.tags.filter(t => t.articleId === this.props.selectedArticleId).map(tag => ( this.props.tags
.filter(t => t.articleId === this.props.selectedArticleId)
.map(tag => (
<span key={tag.id}> <span key={tag.id}>
<button className="btn btn-outline-success ml-1" <button
onClick={(e) => this.props.selectTag(tag.id)}> className="btn btn-outline-success ml-1"
onClick={e => this.props.selectTag(tag.id)}
>
{tag.name} {tag.name}
</button> </button>
<button className="btn btn-danger mr-1" onClick={(e) => this.props.removeTag(tag.id)}>-</button> <button
className="btn btn-danger mr-1"
onClick={e => this.props.removeTag(tag.id)}
>
-
</button>
</span> </span>
))} ))}
<form className="mt-2 mb-2" onSubmit={this.props.addTag}> <form className="mt-2 mb-2" onSubmit={this.props.addTag}>
@ -27,9 +36,15 @@ class Tags extends React.Component {
value={this.props.newTag} value={this.props.newTag}
onChange={this.props.handleChange} onChange={this.props.handleChange}
/> />
<button className="col btn btn-primary col-sm-1" onClick={this.props.addTag}>+</button> <button
className="col btn btn-primary col-sm-1"
onClick={this.props.addTag}
>
+
</button>
</form> </form>
</div> </div>
)} )
}
} }
export default Tags export default Tags

View File

@ -19,7 +19,9 @@ class UpdateArticle extends React.Component {
render() { render() {
return ( return (
<div> <div>
<button className="btn" onClick={() => this.props.navigate('article')}>back</button> <button className="btn" onClick={() => this.props.navigate('article')}>
back
</button>
<form> <form>
<h2 className="form-group row">Article {this.state.id}</h2> <h2 className="form-group row">Article {this.state.id}</h2>
<div className="form-group row"> <div className="form-group row">
@ -38,7 +40,7 @@ class UpdateArticle extends React.Component {
className="form-control" className="form-control"
name="text" name="text"
rows="25" rows="25"
value={this.state.text || ""} value={this.state.text || ''}
onChange={this.handleChange} onChange={this.handleChange}
/> />
</div> </div>