Skip to content
Open

Hw 7 #66

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
42 changes: 32 additions & 10 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,46 @@ import ArticlesPage from './components/routes/articles'
import UserForm from './components/user-form'
import Filters from './components/filters'
import Counter from './components/counter'
import HandleLng from './components/handle-lng'
import {GLOSSARY} from './constants'
import 'react-select/dist/react-select.css'
import CommentsPage from './components/routes/comments-page'

class App extends Component {
static propTypes = {
};

componentWillMount() {
this.compileGlossary()
}

static childContextTypes = {
user: PropTypes.string
user: PropTypes.string,
glossary: PropTypes.object
}

state = {
username: ''
username: '',
glossary: {},
lng: 1
}

getChildContext() {
return {
user: this.state.username
user: this.state.username,
glossary: this.state.glossary
}
}

render() {
console.log('---', 'rendering App')
const {glossary} = this.state
return (
<div>
<HandleLng val={this.state.lng} onChange = {this.handleLngChange}/>
<UserForm value = {this.state.username} onChange = {this.handleUserChange}/>
<ul>
<li><NavLink to = "/counter" activeStyle = {{ color: 'red' }}>counter</NavLink></li>
<li><NavLink to = "/filters" activeStyle = {{ color: 'red' }}>filters</NavLink></li>
<li><NavLink to = "/articles" activeStyle = {{ color: 'red' }}>articles</NavLink></li>
<li><NavLink to = "/comments/1" activeStyle = {{ color: 'red' }}>comments</NavLink></li>
<li><NavLink to = "/counter" activeStyle = {{ color: 'red' }}>{glossary.counter}</NavLink></li>
<li><NavLink to = "/filters" activeStyle = {{ color: 'red' }}>{glossary.filters}</NavLink></li>
<li><NavLink to = "/articles" activeStyle = {{ color: 'red' }}>{ glossary.articles}</NavLink></li>
<li><NavLink to = "/comments/1" activeStyle = {{ color: 'red' }}>{glossary.comments}</NavLink></li>
</ul>
<Switch>
<Redirect from = "/" to = "/articles" exact />
Expand All @@ -53,6 +62,19 @@ class App extends Component {
}

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

handleLngChange = ev => {
this.setState({'lng': +ev.target.value})
this.compileGlossary(ev.target.value)
}

compileGlossary = (locale = this.state.lng) => {
let glossary = {}
for (var word in GLOSSARY) {
glossary[word] = GLOSSARY[word][locale]
}
this.setState({ glossary })
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

почему бы не в конструкторе? А еще лучше в Provider вынести

}
}

export default App
10 changes: 7 additions & 3 deletions src/components/article/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Article extends Component {
state = {
error: null
}
static contextTypes = {
glossary: PropTypes.object
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

я правильно понимаю: вынести в контекст, а потом через props-ы декоратора передавать?

}

componentDidCatch(error) {
console.log('---', error)
Expand All @@ -28,6 +31,7 @@ class Article extends Component {
if (this.state.error) return <h2>{this.state.error.message}</h2>

const { isOpen, article, onButtonClick } = this.props
const { glossary } = this.context
if (!article) return null

return (
Expand All @@ -38,10 +42,10 @@ class Article extends Component {
className = "test__article--button"
onClick={() => onButtonClick(article.id)}
>
{isOpen ? 'close' : 'open'}
{isOpen ? glossary['close'] : glossary['show']}
</button>
<button onClick = {this.handleDelete}>
delete me
{glossary['delete']}
</button>
</h2>
<CSSTransition
Expand All @@ -64,7 +68,7 @@ class Article extends Component {
if (article.loading) return <Loader/>

return (
<section className = "test__article--body">
<section key={article.id} className = "test__article--body">
{article.text}
<CommentList article = {article}/>
</section>
Expand Down
6 changes: 4 additions & 2 deletions src/components/comment-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class CommentList extends Component {
static contextTypes = {
store: PropTypes.object,
router: PropTypes.object,
user: PropTypes.string
user: PropTypes.string,
glossary: PropTypes.object
}

componentWillReceiveProps({ isOpen, article, loadArticleComments }) {
Expand All @@ -35,8 +36,9 @@ class CommentList extends Component {

render() {
const {isOpen, toggleOpen} = this.props
const { glossary } = this.context
console.log('---', 'rendering CommentList')
const text = isOpen ? 'hide comments' : 'show comments'
const text = isOpen ? `${glossary['close']} ${glossary['comments']}` : `${glossary['show']} ${glossary['comments']}`
return (
<div>
<button onClick={toggleOpen} className="test__comment-list--btn">{text}</button>
Expand Down
34 changes: 34 additions & 0 deletions src/components/handle-lng.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'

class HandleLng extends Component {
static propTypes = {
val: PropTypes.number,
onChange: PropTypes.func
};

render () {
const langids = ["langEN", "langRU", "langDE"]
const langlbls = ['English', "Русский", "Deutsch"]

const radioElements = langlbls.map( (l, index) => {
const {val, onChange} = this.props
return (
<span key={langids[index]}>
<input type="radio" name="language" value={index} id={langids[index]}
defaultChecked = {val === index}
onClick={onChange}/>
<label htmlFor={langids[index]}>{l}</label>
</span>
)
})

return (
<p>
{radioElements}
</p>
)
}
}

export default HandleLng
16 changes: 11 additions & 5 deletions src/components/loader.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import React from 'react'
import React, {Component} from 'react'
import PropTypes from 'prop-types'

function Loader() {
return (
<h2>Loading...</h2>
)
class Loader extends Component{
static contextTypes = {
glossary: PropTypes.object
}
render () {
return (
<h2>{ this.context.glossary.loading }</h2>
)
}
}

Loader.propTypes = {
Expand Down
9 changes: 6 additions & 3 deletions src/components/routes/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@ import React, { Component } from 'react'
import { Route } from 'react-router-dom'
import ArticleList from '../article-list'
import Article from '../article'
import PropTypes from 'prop-types'

class ArticlesRoute extends Component {
static propTypes = {

};

static contextTypes = {
glossary: PropTypes.object
}
render() {
console.log('---', 'rendering Articles Page')
return (
<div>
<h2>Articles page</h2>
<h2>{ this.context.glossary['articles'] }</h2>
<ArticleList />
<Route path = {`${this.props.match.path}/:id`} children = {this.getArticle}/>
</div>
)
}

getArticle = ({ match }) => {
if (!match) return <h2>Please select an article</h2>
if (!match) return <h2>{ this.context.glossary['Please select an article'] }</h2>

return <Article id = {match.params.id} key = {match.params.id} isOpen />
}
Expand Down
7 changes: 5 additions & 2 deletions src/components/user-form.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'

class UserForm extends Component {
static propTypes = {

};

static contextTypes = {
glossary: PropTypes.object
}
render() {
return (
<div>
username: <input value = {this.props.value} onChange = {this.handleChange}/>
{ this.context.glossary['username'] }: <input value = {this.props.value} onChange = {this.handleChange}/>
</div>
)
}
Expand Down
19 changes: 18 additions & 1 deletion src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,21 @@ export const LOAD_COMMENTS_FOR_PAGE = 'LOAD_COMMENTS_FOR_PAGE'

export const START = '_START'
export const SUCCESS = '_SUCCESS'
export const FAIL = '_FAIL'
export const FAIL = '_FAIL'

export const GLOSSARY = {
'close': ['close', 'закрыть', 'schließen'],
'show': ['show', 'отобразить','anzeigen'],
'comments': ['comments', 'комментарии', 'die Kommentare'],
'articles': ['articles', 'статьи', 'die Artikle'],
'counter': ['counter', 'счетчик', 'der Zähler'],
'filters': ['filters', 'фильтры', 'die Filter'],
'Please select an article': [
'Please select an article',
'Выберите статью',
'Bitte wählen Sie einen Artikel aus'
],
'username': ['username', 'пользователь', 'Nutzername'],
'delete': ['delete me', 'удалить сиё', 'lösche mich'],
'loading': ['Loading...', 'мы ждём перемен..', 'die Beladung...']
}