Skip to content
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

hm7 kazmin #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
69 changes: 42 additions & 27 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,60 @@ import Filters from './components/filters'
import Counter from './components/counter'
import Menu, { MenuItem } from './components/menu'
import { Provider as UserProvider } from './context/user'
import { Provider as LanguageProvider } from './context/language'
import LanguageSwitcher from './components/language-switcher'
import t from './i18n'

import { LANGUAGES } from './constants'

class App extends Component {
state = {
username: 'roma'
username: 'roma',
lang: LANGUAGES.ENGLISH
}

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

render() {
return (
<UserProvider value={this.state.username}>
<div>
<Menu>
<MenuItem path="/filters">Filters</MenuItem>
<MenuItem path="/articles">Articles</MenuItem>
<MenuItem path="/counter">Counter</MenuItem>
<MenuItem path="/comments/1">Comments</MenuItem>
</Menu>
<UserForm
value={this.state.username}
onChange={this.handleUserChange}
/>
<Switch>
<Redirect from="/" to="/articles" exact />
<Route path="/counter" component={Counter} exact />
<Route path="/filters" component={Filters} />
<Route
path="/articles/new"
render={() => <h1>Add New Article</h1>}
<LanguageProvider value={this.state.lang}>
<UserProvider value={this.state.username}>
<LanguageSwitcher onLangChange={this.onLangChange} />
<div>
<Menu>
<MenuItem path="/filters">{t('filters')}</MenuItem>
<MenuItem path="/articles">{t('articles')}</MenuItem>
<MenuItem path="/counter">{t('counter')}</MenuItem>
<MenuItem path="/comments/1">{t('comments')}</MenuItem>
</Menu>
<UserForm
value={this.state.username}
onChange={this.handleUserChange}
/>
<Route path="/articles" component={ArticleRoute} />
<Route path="/comments" component={CommentsPage} />
<Route path="/error" render={() => <h1>Some Error</h1>} />
<Route path="/" render={() => <h1>Not Found</h1>} />
</Switch>
</div>
</UserProvider>
<Switch>
<Redirect from="/" to="/articles" exact />
<Route path="/counter" component={Counter} exact />
<Route path="/filters" component={Filters} />
<Route
path="/articles/new"
render={() => <h1>Add New Article</h1>}
/>
<Route path="/articles" component={ArticleRoute} />
<Route path="/comments" component={CommentsPage} />
<Route path="/error" render={() => <h1>Some Error</h1>} />
<Route path="/" render={() => <h1>Not Found</h1>} />
</Switch>
</div>
</UserProvider>
</LanguageProvider>
)
}

onLangChange = (lang) => {
this.setState({
lang
})
}
}

export default App
35 changes: 35 additions & 0 deletions src/components/language-switcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { Consumer as LangConsumer } from '../context/language'

import { LANGUAGES } from '../constants'

class LanguageSwitcher extends Component {
static propTypes = {
onLangChange: PropTypes.func.isRequired
}

render() {
return (
<ul>
<LangConsumer>
{(activeLang) => this.getLanguageList(activeLang)}
</LangConsumer>
</ul>
)
}

getLanguageList(activeLang) {
return Object.values(LANGUAGES).map((lang) => (
<li
onClick={() => this.props.onLangChange(lang)}
style={activeLang === lang ? { color: 'red' } : null}
key={lang}
>
{lang}
</li>
))
}
}

export default LanguageSwitcher
3 changes: 2 additions & 1 deletion src/components/menu/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, { Component } from 'react'
import MenuItem from './menu-item'
import t from '../../i18n'

class Menu extends Component {
static propTypes = {}

render() {
return (
<div>
<h2>Main Menu</h2>
<h2>{t('mainMenu')}</h2>
{this.props.children}
</div>
)
Expand Down
5 changes: 5 additions & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ export const LOAD_COMMENTS_FOR_PAGE = 'LOAD_COMMENTS_FOR_PAGE'
export const START = '_START'
export const SUCCESS = '_SUCCESS'
export const FAIL = '_FAIL'

export const LANGUAGES = {
ENGLISH: 'en',
RUSSIAN: 'ru'
}
5 changes: 5 additions & 0 deletions src/context/language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createContext } from 'react'

const { Provider, Consumer } = createContext()

export { Provider, Consumer }
15 changes: 15 additions & 0 deletions src/i18n/dictionary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const ru = {
mainMenu: 'Главное меню',
filters: 'Фильтры',
articles: 'Статьи',
counter: 'Счетчик',
comments: 'Комментарии'
}

export const en = {
mainMenu: 'Main menu',
filters: 'Filters',
articles: 'Articles',
counter: 'Counters',
comments: 'Comments'
}
11 changes: 11 additions & 0 deletions src/i18n/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react'
import { Consumer } from '../context/language'
import * as dicts from './dictionary'

export default function translate(string) {
return <Consumer>{(lang) => getTranslatedString(string, lang)}</Consumer>
}

function getTranslatedString(string, lang) {
return dicts[lang] && dicts[lang][string] ? dicts[lang][string] : string
}
4 changes: 3 additions & 1 deletion src/reducer/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export default (state = new ReducerRecord(), action) => {

case LOAD_ALL_ARTICLES + SUCCESS:
return state
.set('entities', arrToMap(response, ArticleModel))
.updateIn(['entities'], (entities) =>
arrToMap(response, ArticleModel).mergeDeep(entities)
)
.set('loading', false)
.set('loaded', true)

Expand Down