Skip to content
Open

Hw 8 #94

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,37 @@ import CounterPage from './routes/Counter'
import CommentsPage from './routes/CommentsPage'
import { Route, Redirect, Switch } from 'react-router-dom'
import Menu, { MenuItem } from './Menu'
import Header from './Header'
import {locales} from '../locales'
import {getLocaleText} from './utils'

class App extends Component {
static childContextTypes = {
user: PropTypes.string
user: PropTypes.string,
locale: PropTypes.object
}

state = {
username: 'Roma'
username: 'Roma',
localeName: null
}

getChildContext() {
return {
user: this.state.username
user: this.state.username,
locale: this.state.localeName != null ? locales[this.state.localeName] : null
}
}

handleUserChange = username => this.setState({ username })

handleLocaleChange = localeName => () => this.setState({ localeName })

render() {
console.log('---', 1)
console.log('---', 1, this.context)
return (
<div>
<h1>App name</h1>
<Header onLocaleChange = {this.handleLocaleChange} />
<Menu>
<MenuItem to = "/articles">Articles</MenuItem>
<MenuItem to = "/filters">Filters</MenuItem>
Expand Down
21 changes: 17 additions & 4 deletions src/components/Article/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {connect} from 'react-redux'
import CommentList from '../CommentList'
import Loader from '../common/Loader'
import {deleteArticle, loadArticle} from '../../AC'
import { articleSelector } from '../../selectors'
import { articleSelector, articlesLoadingSelector, articlesLoadedSelector } from '../../selectors'
import {getLocaleText} from '../utils'
import './style.css'

class Article extends Component {
Expand All @@ -31,11 +32,21 @@ class Article extends Component {
}
}

static contextTypes = {
locale: PropTypes.object
}

componentDidMount() {
const { id, isOpen, article, loadArticle } = this.props
if (isOpen && (!article || !article.text)) loadArticle(id)
}

componentWillReceiveProps({id, isOpen, article, articlesLoaded, loadArticle}) {
if (isOpen && this.props.articlesLoading && articlesLoaded && article && !article.text) {
loadArticle(id)
}
}

render() {
console.log('---', 4)
const {article, isOpen, toggleOpen} = this.props
Expand All @@ -46,10 +57,10 @@ class Article extends Component {
<h2 ref = {this.setTitleRef} onClick = {this.increment}>
{article.title}
<button onClick={toggleOpen}>
{isOpen ? 'close' : 'open'}
{isOpen ? getLocaleText(this)('close') : getLocaleText(this)('open')}
</button>
<button onClick = {this.handleDelete}>
delete
{getLocaleText(this)('delete')}
</button>
</h2>
<CSSTransition
Expand Down Expand Up @@ -106,5 +117,7 @@ class Article extends Component {


export default connect((state, props) => ({
article: articleSelector(state, props)
article: articleSelector(state, props),
articlesLoading: articlesLoadingSelector(state),
articlesLoaded: articlesLoadedSelector(state)
}), { deleteArticle, loadArticle }, null, { pure: false })(Article)
1 change: 1 addition & 0 deletions src/components/ArticleList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {filtratedArticlesSelector, articlesLoadingSelector} from '../selectors'
import {loadAllArticles} from '../AC'
import Loader from './common/Loader'
import {NavLink, withRouter} from 'react-router-dom'
import {getLocaleText} from './utils'

class ArticleList extends Accordion {
componentDidMount() {
Expand Down
12 changes: 9 additions & 3 deletions src/components/CommentForm/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import {addComment} from '../../AC'
import {getLocaleText} from '../utils'
import './style.css'

class CommentForm extends Component {
static propTypes = {
};

static contextTypes = {
locale: PropTypes.object
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут и в других местах: лучше сделай декоратор либо компонент-обертку для локализации, чтоб не обращатся каждый раз к контексту, иначе потом тяжело будет что-либо поменять

}

state = {
user: '',
text: ''
Expand All @@ -15,10 +21,10 @@ class CommentForm extends Component {
render() {
return (
<form onSubmit = {this.handleSubmit}>
user: <input value = {this.state.user}
{getLocaleText(this)('user')}: <input value = {this.state.user}
onChange = {this.handleChange('user')}
className = {this.getClassName('user')} />
comment: <input value = {this.state.text}
{getLocaleText(this)('comment')}: <input value = {this.state.text}
onChange = {this.handleChange('text')}
className = {this.getClassName('text')} />
<input type = "submit" value = "submit" disabled = {!this.isValidForm()}/>
Expand Down Expand Up @@ -63,4 +69,4 @@ const limits = {

export default connect(null, (dispatch, ownProps) => ({
addComment: (comment) => dispatch(addComment(comment, ownProps.articleId))
}))(CommentForm)
}), null, { pure: false })(CommentForm)
9 changes: 6 additions & 3 deletions src/components/CommentList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import CommentForm from './CommentForm'
import Loader from './common/Loader'
import {connect} from 'react-redux'
import {loadArticleComments} from '../AC'
import {getLocaleText} from './utils'

class CommentList extends Component {
static propTypes = {
Expand All @@ -18,7 +19,8 @@ class CommentList extends Component {
static contextTypes = {
store: PropTypes.object,
router: PropTypes.object,
user: PropTypes.string
user: PropTypes.string,
locale: PropTypes.object
}

componentWillReceiveProps({ isOpen, article, loadArticleComments }) {
Expand All @@ -30,11 +32,12 @@ class CommentList extends Component {
render() {
console.log('---', 'context: ', this.context)
const {isOpen, toggleOpen} = this.props
const text = isOpen ? 'hide comments' : 'show comments'
const text = isOpen ? getLocaleText(this)('hide comments') :
getLocaleText(this)('show comments')
return (
<div>
<button onClick={toggleOpen}>{text}</button>
<h2>User: {this.context.user}</h2>
<h2>{getLocaleText(this)('User')}: {this.context.user}</h2>
{this.getBody()}
</div>
)
Expand Down
24 changes: 24 additions & 0 deletions src/components/Header.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {getLocaleText} from './utils'

export default class Header extends Component {
static contextTypes = {
locale: PropTypes.object
}

render() {
const handleLocaleChange = this.props.onLocaleChange

return (
<div>
<div>
{getLocaleText(this)('Select language') + ': '}
<button onClick= {handleLocaleChange()}>{getLocaleText(this)('original')}</button>
<button onClick= {handleLocaleChange('ru')}>{getLocaleText(this)('ru')}</button>
</div>
<h1>{getLocaleText(this)('App name')}</h1>
</div>
)
}
}
22 changes: 15 additions & 7 deletions src/components/Menu/MenuItem.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import React from 'react'
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {NavLink} from 'react-router-dom'
import {getLocaleText} from '../utils'

function MenuItem({ children, to }) {
return (
<div>
<NavLink to = {to} activeStyle = {{ color: 'red' }}>{children}</NavLink>
</div>
)
class MenuItem extends Component {
render() {
const { children, to } = this.props
return (
<div>
<NavLink to = {to} activeStyle = {{ color: 'red' }}>{getLocaleText(this)(children)}</NavLink>
</div>
)
}
}

MenuItem.propTypes = {
}

MenuItem.contextTypes = {
locale: PropTypes.object
}

export default MenuItem
7 changes: 6 additions & 1 deletion src/components/Menu/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import MenuItem from './MenuItem'
import {getLocaleText} from '../utils'

class Menu extends Component {
static propTypes = {

};

static contextTypes = {
locale: PropTypes.object
}

render() {
return (
<div>
<h2>Main menu:</h2>
<h2>{getLocaleText(this)('Main menu')}:</h2>
{this.props.children}
</div>
)
Expand Down
8 changes: 7 additions & 1 deletion src/components/UserForm.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {getLocaleText} from './utils'

class UserForm extends Component {
static propTypes = {

};

static contextTypes = {
locale: PropTypes.object
}

state = {
user: ''
}
Expand All @@ -13,7 +19,7 @@ class UserForm extends Component {
const {value} = this.props
return (
<div>
Username: <input type = "text" value = {value} onChange = {this.handleChange}/>
{getLocaleText(this)('Username')}: <input type = "text" value = {value} onChange = {this.handleChange}/>
</div>
)
}
Expand Down
17 changes: 12 additions & 5 deletions src/components/common/Loader.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import React from 'react'
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import {getLocaleText} from '../utils'

function Loader(props) {
return (
<h2>Loading...</h2>
)
class Loader extends Component {
render() {
return (
<h2>{getLocaleText(this)('Loading') + '...'}</h2>
)
}
}

Loader.propTypes = {
}

Loader.contextTypes = {
locale: PropTypes.object
}

export default Loader
8 changes: 7 additions & 1 deletion src/components/routes/ArticleList.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import ArticleList from '../ArticleList'
import Article from '../Article'
import {Route} from 'react-router-dom'
import {getLocaleText} from '../utils'

class ArticleListPage extends Component {
static propTypes = {

};

static contextTypes = {
locale: PropTypes.object
}

render() {
console.log('---', 2)
return (
<div>
<h2>Article list:</h2>
<h2>{getLocaleText(this)('Article list') + ':'}</h2>
<ArticleList/>
<Route path = {`${this.props.match.path}/:id`} children = {this.getArticle} />
</div>
Expand Down
4 changes: 4 additions & 0 deletions src/components/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const getLocaleText = ctx => text => {
return ctx && ctx.context && ctx.context.locale &&
ctx.context.locale[text] != null ? ctx.context.locale[text] : text
}
25 changes: 25 additions & 0 deletions src/locales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const locales = {
"ru": {
"Select language": "Выберите язык",
"original": "оригинал",
"ru": "ру",
"App name": "Имя приложения",
"Main menu": "Главное меню",
"Username": "Пользователь",
"User": "Пользователь",
"user": "пользователь",
"Article list": "Список статей",
"Please select an article": "Выберите статью",
"Articles": "Статьи",
"Filters": "Фильтры",
"Counter": "Счетчик",
"Comments": "Комментарии",
"comment": "комментарий",
"open": "открыть",
"close": "закрыть",
"delete": "удалить",
"show comments": "показать комментарии",
"hide comments": "скрыть комментарии",
"Loading": "Загрузка"
}
}
1 change: 1 addition & 0 deletions src/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {createSelector} from 'reselect'

export const articlesMapSelector = state => state.articles.entities
export const articlesLoadingSelector = state => state.articles.loading
export const articlesLoadedSelector = state => state.articles.loaded
export const filtersSelector = state => state.filters
export const commentMapSelector = state => state.comments.get('entities')
export const idSelector = (_, props) => props.id
Expand Down