-
Notifications
You must be signed in to change notification settings - Fork 20
Home work 4 #45
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?
Home work 4 #45
Changes from all commits
0238b0e
5bee377
ea77aab
027352c
bb43939
468a48c
0452ec0
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,79 @@ | ||
| import React, {Component} from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { connect } from 'react-redux'; | ||
| import { addNewComment } from '../AC'; | ||
|
|
||
| class AddNewCommentForm extends Component { | ||
|
|
||
| state = { | ||
| userName: '', | ||
| text: '' | ||
| }; | ||
|
|
||
| static propTypes = { | ||
| articleId: PropTypes.string.isRequired, | ||
| addComment: PropTypes.func.isRequired | ||
| }; | ||
|
|
||
| static defaultProps = {}; | ||
|
|
||
| handleSubmit = (e) => { | ||
| e.preventDefault(); | ||
| this.props.addComment(this.state.userName, this.state.text, this.props.articleId); | ||
| this.setState({ | ||
| userName: '', | ||
| text: '' | ||
| }) | ||
| }; | ||
|
|
||
| handleUsernameInput = (e) => { | ||
| this.setState({ | ||
| userName: e.target.value | ||
| }); | ||
| }; | ||
|
|
||
| handleTextInput = (e) => { | ||
| this.setState({ | ||
| text: e.target.value | ||
| }); | ||
| }; | ||
|
|
||
| render() { | ||
| return ( | ||
| <div style={{border: '1px solid #000', padding: '5px', marginTop: '10px'}}> | ||
| <form onSubmit={this.handleSubmit}> | ||
| <div> | ||
| <label htmlFor='username'>Username</label><br /> | ||
| <input type='text' name='username' id='username' placeholder='Input your name' value={this.state.userName} onChange={this.handleUsernameInput} /> | ||
| <br /><br /> | ||
| </div> | ||
| <div> | ||
| <label htmlFor='text'>Text</label><br /> | ||
| <textarea name='text' id='text' placeholder='Input text' value={this.state.text} onChange={this.handleTextInput} /> | ||
| <br /><br /> | ||
| </div> | ||
| <div> | ||
| <input type="submit" value="Add" disabled={!(this.state.userName && this.state.text)}/> | ||
| </div> | ||
| </form> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| const mapStateToProps = (state, ownProps) => { | ||
| return { | ||
| articleId: ownProps.articleId | ||
| } | ||
| }; | ||
|
|
||
| const mapDispatchToProps = (dispatch) => { | ||
| return { | ||
| addComment(user, text, articleId) { | ||
| dispatch(addNewComment(user, text, articleId)); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| export default connect(mapStateToProps, mapDispatchToProps)(AddNewCommentForm); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,9 @@ | ||
| export const INCREMENT = 'INCREMENT' | ||
| export const INCREMENT = 'INCREMENT'; | ||
|
|
||
| export const DELETE_ARTICLE = 'DELETE_ARTICLE' | ||
| export const DELETE_ARTICLE = 'DELETE_ARTICLE'; | ||
| export const UPDATE_ARTICLE_COMMENTS = 'UPDATE_ARTICLE_COMMENTS'; | ||
|
|
||
| export const CHANGE_SELECTION = 'CHANGE_SELECTION' | ||
| export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE' | ||
| export const CHANGE_SELECTION = 'CHANGE_SELECTION'; | ||
| export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE'; | ||
|
|
||
| export const ADD_NEW_COMMENT = 'ADD_NEW_COMMENT'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { ADD_NEW_COMMENT } from '../constants'; | ||
| import { updateArticleComments } from '../AC'; | ||
| import randomString from 'randomstring'; | ||
|
|
||
| export const handleNewComment = (store) => (next) => (action) => { | ||
| if (action.type === ADD_NEW_COMMENT) { | ||
| let newAction = {...action}; | ||
| let commentId = randomString.generate(12); | ||
| newAction.payload.data.id = commentId; | ||
|
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, мало-ли что там станут передавать |
||
| let result = next(newAction); | ||
| store.dispatch(updateArticleComments(newAction.payload.articleId, commentId)); | ||
|
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. зачем тебе 2 диспатча? |
||
| return result; | ||
| } | ||
|
|
||
| return next(action); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,33 @@ | ||
| import { normalizedArticles as defaultArticles } from '../fixtures' | ||
| import { DELETE_ARTICLE } from '../constants' | ||
| import { normalizedArticles } from '../fixtures'; | ||
| import { DELETE_ARTICLE, UPDATE_ARTICLE_COMMENTS } from '../constants'; | ||
|
|
||
| const defaultArticles = normalizedArticles.reduce((acc, article) => { | ||
| return { | ||
| ...acc, | ||
| [article.id]: article | ||
| } | ||
| }, {}); | ||
|
|
||
| export default (articlesState = defaultArticles, action) => { | ||
| const { type, payload } = action | ||
| const { type, payload } = action; | ||
|
|
||
| switch (type) { | ||
| case DELETE_ARTICLE: | ||
| return articlesState.filter(article => article.id !== payload.id) | ||
| let articlesForDelete = {...articlesState}; | ||
| delete articlesForDelete[payload.id]; | ||
| return articlesForDelete; | ||
|
|
||
| case UPDATE_ARTICLE_COMMENTS: | ||
| let articlesForUpdate = {...articlesState}; | ||
| let article = articlesForUpdate[payload.articleId]; | ||
| if (!article.comments) { | ||
| article.comments = []; | ||
|
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. не мутируй стейт |
||
| } | ||
| article.comments.push(payload.commentId); | ||
|
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. и здесь не мутируй |
||
| articlesForUpdate[payload.articleId] = article; | ||
| return articlesForUpdate; | ||
|
|
||
| default: | ||
| return articlesState | ||
| } | ||
| } | ||
| } | ||
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.
через мидлвары будет проходить каждый экшин, они должны быть максимально общими, завязывать на конкретные экшины - не лучшее решение