Skip to content
Open

HT7 #65

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
6 changes: 3 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class App extends Component {
};

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

state = {
Expand All @@ -23,7 +23,7 @@ class App extends Component {

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

Expand Down Expand Up @@ -55,4 +55,4 @@ class App extends Component {
handleUserChange = username => this.setState({ username })
}

export default App
export default App
18 changes: 13 additions & 5 deletions src/components/article/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import { connect } from 'react-redux'
import CommentList from '../comment-list'
import Loader from '../loader'
import { deleteArticle, loadArticleById } from '../../AC'
import { articleSelector } from '../../selectors'
// import { articleSelector, translateSelector } from '../../selectors'
import { articleSelector} from '../../selectors'
// import {dictionary} from '../../fixtures'
import { translate } from '../../decorators/translation'
import './style.css'

class Article extends Component {
static contextTypes = {
dictionary: PropTypes.object,
}

state = {
error: null
}
Expand All @@ -20,14 +27,15 @@ class Article extends Component {

componentDidMount() {
const { loadArticleById, article, id } = this.props
console.log('componentDidMount', article)
if (!article || (!article.text && !article.loading)) loadArticleById(id)
}

render() {
console.log('---', 'rendering Article')
if (this.state.error) return <h2>{this.state.error.message}</h2>

const { isOpen, article, onButtonClick } = this.props
const { isOpen, article, onButtonClick, __ } = this.props
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.

Не самое удачное название)

if (!article) return null

return (
Expand All @@ -38,10 +46,10 @@ class Article extends Component {
className = "test__article--button"
onClick={() => onButtonClick(article.id)}
>
{isOpen ? 'close' : 'open'}
{isOpen ? __( 'close') : __('open')}
</button>
<button onClick = {this.handleDelete}>
delete me
{__("delete me")}
</button>
</h2>
<CSSTransition
Expand Down Expand Up @@ -89,4 +97,4 @@ Article.propTypes = {

export default connect((state, props) => ({
article: articleSelector(state, props)
}), { deleteArticle, loadArticleById }, null, { pure: false })(Article)
}), { deleteArticle, loadArticleById }, null, { pure: false })(translate(Article))
7 changes: 4 additions & 3 deletions src/components/comment-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Comment from '../comment'
import CommentForm from '../comment-form'
import Loader from '../loader'
import toggleOpen from '../../decorators/toggleOpen'
import { translate } from '../../decorators/translation'
import {loadArticleComments} from '../../AC'
import './style.css'

Expand Down Expand Up @@ -34,9 +35,9 @@ class CommentList extends Component {
}

render() {
const {isOpen, toggleOpen} = this.props
const {isOpen, toggleOpen, __ } = this.props
console.log('---', 'rendering CommentList')
const text = isOpen ? 'hide comments' : 'show comments'
const text = isOpen ? __('hide comments') : __('show comments')
return (
<div>
<button onClick={toggleOpen} className="test__comment-list--btn">{text}</button>
Expand Down Expand Up @@ -85,4 +86,4 @@ class CommentList extends Component {
}


export default connect(null, { loadArticleComments }, null, { pure: false })(toggleOpen(CommentList))
export default connect(null, { loadArticleComments }, null, { pure: false })(toggleOpen(translate(CommentList)))
37 changes: 37 additions & 0 deletions src/decorators/translation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react'
import PropTypes from 'prop-types'
import { translateSelector } from '../selectors'


export class Translator extends React.Component {

static childContextTypes = {
dictionary: PropTypes.dict
}

getChildContext() {
return {
dictionary: this.props.dictionary
}
}

render() {
return <div> {this.props.children} </div>
}
}


export function translate (OriginalComponent) {
return class Translated extends React.Component {

static contextTypes = {
dictionary: PropTypes.object,
}

translateWord = message => translateSelector(this.context.dictionary, message)

render() {
return <OriginalComponent {...this.props} {...this.state} __ = {this.translateWord}/>
}
}
}
12 changes: 11 additions & 1 deletion src/fixtures.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import App from './App'
import registerServiceWorker from './registerServiceWorker'
import store from './store'
import history from './history'
import {Translator} from './decorators/translation'
import {dictionary} from './fixtures'

ReactDOM.render(
<Provider store = {store}>
<ConnectedRouter history = {history}>
<Translator dictionary = {dictionary} >
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.

Не понял где ты его переключаешь

<App />
</Translator>
</ConnectedRouter>
</Provider>
, document.getElementById('root'))
Expand Down
8 changes: 5 additions & 3 deletions src/reducer/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ export default (articles = new ReducerState(), action) => {

case LOAD_ALL_ARTICLES + SUCCESS:
return articles
.set('entities', arrToMap(response, ArticleRecord))
.setIn(['entities'], arrToMap(response, ArticleRecord).merge(articles.entities))
.set('loading', false)
.set('loaded', true)

case LOAD_ARTICLE + START:
return articles.setIn(['entities', payload.id, 'loading'], true)

case LOAD_ARTICLE + SUCCESS:
return articles.setIn(['entities', payload.id], new ArticleRecord(payload.response))
return articles
.setIn(['entities', payload.id], new ArticleRecord(payload.response))
.setIn(['entities', payload.id, 'loading'], false)

case LOAD_ARTICLE_COMMENTS + START:
return articles.setIn(['entities', payload.articleId, 'commentsLoading'], true)
Expand All @@ -62,4 +64,4 @@ export default (articles = new ReducerState(), action) => {
default:
return articles
}
}
}
7 changes: 6 additions & 1 deletion src/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ export const filtratedArticles = createSelector(articleListSelector, filtersSele

export const articleSelector = createSelector(articlesMapSelector, idSelector, (articles, id) => articles.get(id))

export const createCommentSelector = () => createSelector(commentsSelector, idSelector, (comments, id) => comments.get(id))
export const createCommentSelector = () => createSelector(commentsSelector, idSelector, (comments, id) => comments.get(id))


const entriesSelector = dictionary => dictionary.translateEntries
const patternSelector = (_, message) => message//.toLowerCase()
export const translateSelector = createSelector(entriesSelector, patternSelector, (entries, pattern) => entries[pattern] ? entries[pattern] : pattern)