-
Notifications
You must be signed in to change notification settings - Fork 20
homework4 #47
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?
homework4 #47
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import React from "react"; | ||
| import {addComment} from "../../AC/index"; | ||
| import {connect} from "react-redux"; | ||
|
|
||
| class CommentInput extends React.Component{ | ||
| state={ | ||
| newComment:"" | ||
| } | ||
|
|
||
| render(){ | ||
| return ( | ||
| <div> | ||
| <h3>Add comment</h3> | ||
| <span>text: | ||
| <input value={this.state.newComment} onChange={(e)=>{this.setState({newComment:e.target.value})}}/> | ||
| </span> | ||
| <button onClick={()=>{ | ||
| this.props.addComment(this.props.articleId,this.props.username,this.state.newComment); | ||
| this.setState({newComment:""})} | ||
| }>add comment</button> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const mapStateToProps = state => { | ||
| return {username:state.user.username} | ||
| } | ||
|
|
||
| export default connect(mapStateToProps,{addComment})(CommentInput); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import {ADD_COMMENT} from "../constants/index"; | ||
|
|
||
| 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. через мидлвары будет проходить каждый экшин, они должны быть максимально общими, завязывать на конкретные экшины - не лучшее решение |
||
| action.payload.newCommentId = Math.random().toString(36).slice(2); | ||
|
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,13 +1,27 @@ | ||
| import { normalizedArticles as defaultArticles } from '../fixtures' | ||
| import { normalizedArticles } from '../fixtures' | ||
| import { DELETE_ARTICLE } from '../constants' | ||
| import {ADD_COMMENT} from "../constants/index"; | ||
|
|
||
| const defaultArticles = normalizedArticles.reduce((acc,article)=>({ | ||
| ...acc, | ||
| [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) | ||
| case ADD_COMMENT:{ | ||
|
|
||
| const {articleId,newCommentId} = action.payload; | ||
| const article = {...articlesState[articleId]} | ||
| article.comments.push(newCommentId) | ||
|
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. здесь ты мутируешь стейт. Возвращаешь новый объект, но внутри меняешь объекты по ссылке |
||
| return { | ||
| ...articlesState, | ||
| [articleId]:article | ||
| } | ||
| } | ||
| default: | ||
| return articlesState | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { CHANGE_USERNAME} from "../constants/index"; | ||
|
|
||
| const initState = { | ||
| username:"" | ||
| } | ||
|
|
||
| export default (state = initState, action) => { | ||
| const { type, payload } = action | ||
|
|
||
| switch (type) { | ||
| case CHANGE_USERNAME: | ||
| return {username:payload.username} | ||
|
|
||
| default: | ||
| return state | ||
| } | ||
| } |
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.
Сделай селектор