From 4e90f48cb8852ce367de2345aef6644d429e6d36 Mon Sep 17 00:00:00 2001 From: data Date: Wed, 10 Jul 2019 20:48:40 +0200 Subject: [PATCH] add / remove Tag to Article --- src/App.js | 3 +++ src/components/article.js | 14 ++++++++++---- src/components/articles.js | 19 ++++++++++++------- src/components/index.js | 1 + src/components/tags.js | 35 +++++++++++++++++++++++++++++++++++ src/controllers/tags.js | 6 +++--- 6 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 src/components/tags.js diff --git a/src/App.js b/src/App.js index b3cb760..f790de8 100644 --- a/src/App.js +++ b/src/App.js @@ -231,6 +231,8 @@ class App extends React.Component { addTag={this.addTag} removeTag={this.removeTag} deleteArticle={this.deleteArticle} + selectTag={this.selectTag} + handleChange={this.handleChange} {...this.state} /> ) @@ -240,6 +242,7 @@ class App extends React.Component { handleChange={this.handleChange} updateArticle={this.updateArticle} navigate={this.navigate} + selectTag={this.selectTag} {...this.state} /> ) diff --git a/src/components/article.js b/src/components/article.js index 4a1d274..20a80ec 100644 --- a/src/components/article.js +++ b/src/components/article.js @@ -1,4 +1,5 @@ import React from 'react' +import Tags from './tags' class Article extends React.Component { constructor(props) { @@ -10,12 +11,17 @@ class Article extends React.Component { render() { return (
- +

{this.state.title}

-
{this.state.text}
- +
{this.state.text}
+
+ + +
+

Comments

    -

    Comments

    {this.props.comments ? this.props.comments.map(comment=>(
  • {comment.text} diff --git a/src/components/articles.js b/src/components/articles.js index d039539..93c5a82 100644 --- a/src/components/articles.js +++ b/src/components/articles.js @@ -2,6 +2,11 @@ import React from 'react' function Articles(props) { console.log(props.tags, props.articles) + const articles = props.articles || [] + if (props.selectedTagId) { + const name = props.tags.filter(t => t.id === props.selectedTagId)['name'] + //articles = props.tags.getArticles() + } return (

    Articles

    @@ -21,18 +26,18 @@ function Articles(props) { > {article.title}{' '} - {props.tags && props.tags.filter(t => t.articleId === article.id).map(tag => ( - - )) - } + {props.tags && props.tags.filter(t => t.articleId === article.id).map(tag => ( + + )) + }
  • ))} diff --git a/src/components/index.js b/src/components/index.js index d85688d..6602ecc 100644 --- a/src/components/index.js +++ b/src/components/index.js @@ -3,6 +3,7 @@ export { default as About } from './about' export { default as Articles } from './articles' export { default as Article } from './article' export { default as UpdateArticle } from './update-article' +export { default as Tags } from './tags' export { default as Tasks } from './tasks' export { default as UpdateTask } from './update-task' export { default as Profile } from './profile' diff --git a/src/components/tags.js b/src/components/tags.js new file mode 100644 index 0000000..f6b9ada --- /dev/null +++ b/src/components/tags.js @@ -0,0 +1,35 @@ +import React from 'react' + +class Tags extends React.Component { + constructor(props) { + super(props) + //props.state.tags = tags + } + + render() { + return ( +
    + + {this.props.tags && this.props.tags.filter(t => t.articleId === this.props.selectedArticleId).map(tag => ( + + + + + ))} +
    + + +
    +
    + )} +} +export default Tags diff --git a/src/controllers/tags.js b/src/controllers/tags.js index a1933a9..3a414ae 100644 --- a/src/controllers/tags.js +++ b/src/controllers/tags.js @@ -4,9 +4,9 @@ const API = process.env.REACT_APP_API || 'http://localhost:1337' export async function addTag(e) { e.preventDefault() if (this.state.newTag === '') { - return alert('No, i will not add an empty tag.') + return alert("Refusing to add an empty tag.") } - const tag = { name: this.state.newTag } + const tag = { name: this.state.newTag, articleId: this.state.selectedArticleId } try { const { data, error } = await axios.post(API + '/api/tags', tag) if (error) { @@ -27,7 +27,7 @@ export async function addTag(e) { export function selectTag(selectedTagId) { this.setState({ selectedTagId }) - this.navigate('tag') + this.navigate('articles') } export async function removeTag(id) {