-
Notifications
You must be signed in to change notification settings - Fork 20
Hw 4 #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Hw 4 #44
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import React, { Component } from 'react' | ||
| import { connect } from 'react-redux' | ||
| import { addComment } from '../AC' | ||
| import toggleOpen from '../decorators/toggleOpen' | ||
|
|
||
| class CommentForm extends Component { | ||
| state = { | ||
| authorname: "", | ||
| commenttext: "" | ||
| } | ||
|
|
||
| render() { | ||
|
|
||
| const {rel, isOpen, toggleOpen} = this.props | ||
|
|
||
| return ( | ||
| <div> | ||
| <button onClick = {toggleOpen}>comment me</button> | ||
| <form method="post" style={{display: isOpen ? "block": "none"}}> | ||
|
|
||
| <label htmlFor={"authorname-" + rel}>name: </label> | ||
| <input type="text" | ||
| name="authorname" | ||
| id={"authorname-" + rel} | ||
| value={this.state.authorname} | ||
| onChange={this.handleInput} | ||
| /><br/> | ||
|
|
||
| <label htmlFor={"newcommenttext-" + rel}>text: </label> | ||
| <textarea name="text" id={"newcommenttext-" + rel} onChange={this.handleTextChange} | ||
| value={this.state.commenttext}></textarea> <br/> | ||
| <input type="button" onClick={this.handleAddcommentClick} value="Add"/> | ||
| </form> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| handleInput = ev => { | ||
| this.setState({ | ||
| authorname: ev.target.value | ||
| }) | ||
| } | ||
|
|
||
| handleTextChange = ev => { | ||
| this.setState({ | ||
| commenttext: ev.target.value | ||
| }) | ||
| } | ||
|
|
||
| handleAddcommentClick = () => { | ||
| const {addComment, rel, toggleOpen} = this.props | ||
| if (this.state.authorname.length && this.state.commenttext.length ) { | ||
| toggleOpen() | ||
| addComment({articleid: rel, user: this.state.authorname, text: this.state.commenttext}) | ||
| this.setState({authorname: '', commenttext:''}) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export default connect(null, { addComment })(toggleOpen(CommentForm)) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { ADD_COMMENT } from '../constants' | ||
| /** | ||
| * Shuffles array in place. ES6 version | ||
| * @param {Array} a items An array containing the items. | ||
| */ | ||
| function shuffle(a) { | ||
| for (let i = a.length - 1; i > 0; i--) { | ||
| const j = Math.floor(Math.random() * (i + 1)); | ||
| [a[i], a[j]] = [a[j], a[i]]; | ||
| } | ||
| return a; | ||
| } | ||
|
|
||
| export default store => next => action => { | ||
| if (action.type === ADD_COMMENT) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. через мидлвары будет проходить каждый экшин, они должны быть максимально общими, завязывать на конкретные экшины - не лучшее решение |
||
| const randomId = shuffle((new Date()).toLocaleString('en-US').split('').filter((symbol)=> { | ||
| return symbol !== ' ' | ||
| })).join('') | ||
| action.payload.data['id'] = randomId | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше не мутировать payload, мало-ли что там станут передавать |
||
| } | ||
| next(action) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,25 @@ | ||
| import { normalizedArticles as defaultArticles } from '../fixtures' | ||
| import { DELETE_ARTICLE } from '../constants' | ||
| import { normalizedArticles } from '../fixtures' | ||
| import { DELETE_ARTICLE, ADD_COMMENT } from '../constants' | ||
|
|
||
| const defaultArticles = normalizedArticles.reduce((wii, article)=>({ | ||
| ...wii, | ||
| [article.id]: article | ||
| }), {}) | ||
|
|
||
| export default (articlesState = defaultArticles, action) => { | ||
|
|
||
| const { type, payload } = action | ||
|
|
||
| switch (type) { | ||
| case DELETE_ARTICLE: | ||
| return articlesState.filter(article => article.id !== payload.id) | ||
| let articles = Object.assign({}, articlesState); | ||
| delete articles[payload.id] | ||
| return articles | ||
|
|
||
| case ADD_COMMENT: | ||
| let comments = articlesState[payload.data.articleid].comments | ||
| typeof comments !== 'undefined' ? comments.push(payload.data.id) : articlesState[payload.data.articleid].comments = new Array(payload.data.id) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. здесь ты мутируешь стейт и ничего не возвращаешь |
||
| //break | ||
|
|
||
| default: | ||
| return articlesState | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import {} from '../constants' | ||
| import { ADD_COMMENT } from '../constants' | ||
| import { normalizedComments } from '../fixtures' | ||
|
|
||
| const defaultComments = normalizedComments.reduce((acc, comment) => ({ | ||
|
|
@@ -8,10 +8,13 @@ const defaultComments = normalizedComments.reduce((acc, comment) => ({ | |
| , {}) | ||
|
|
||
| export default (commentsState = defaultComments, action) => { | ||
| const {type} = action | ||
| const {type, payload} = action | ||
|
|
||
| switch (type) { | ||
|
|
||
| case ADD_COMMENT: | ||
| const {id, user, text} = payload.data | ||
| commentsState[id] = {id: id, user: user, text: text} | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не мутируй стейт! |
||
| //break | ||
| default: | ||
| return commentsState | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Лучше селектор, который достанет их в виде массива