11import * as React from "react" ;
22import { connect } from "react-redux" ;
33import { Dispatch } from "redux" ;
4+ import { createSelector } from "reselect" ;
5+ import { Translator } from "../models/Translator" ;
6+ import { ITranslator } from "../models/TranslatorInterfaces" ;
47import { IStore } from "../redux/IStore" ;
58import {
69 decrement as decrementActionCreator ,
710 increment as incrementActionCreator
811} from "../redux/modules/counterActionCreators" ;
12+ import { translationsSelector } from "../selectors/translationsSelector" ;
913
1014interface IStateToProps {
1115 count : number ;
16+ translations : {
17+ counter : string ;
18+ decrement : string ;
19+ increment : string ;
20+ } ;
1221}
1322
1423interface IDispatchToProps {
@@ -20,25 +29,38 @@ export interface IProps extends IStateToProps, IDispatchToProps {}
2029
2130class CounterPage extends React . Component < IProps > {
2231 public render ( ) : JSX . Element {
23- const { count, decrement, increment} = this . props ;
32+ const { count, decrement, increment, translations } = this . props ;
2433 return (
2534 < div >
26- < h4 > Counter </ h4 >
35+ < h4 > { translations . counter } </ h4 >
2736 < button name = "incBtn" onClick = { increment } >
28- Increment
37+ { translations . increment }
2938 </ button >
3039 < button name = "decBtn" onClick = { decrement } disabled = { count <= 0 } >
31- Decrement
40+ { translations . increment }
3241 </ button >
3342 < p > { count } </ p >
3443 </ div >
3544 ) ;
3645 }
3746}
3847
39- function mapStateToProps ( state : Pick < IStore , "counter" > ) : IStateToProps {
48+ const componentTranslationsSelector = createSelector (
49+ translationsSelector ,
50+ ( translations ) => {
51+ const translator : ITranslator = new Translator ( translations ) ;
52+ return {
53+ counter : translator . translate ( "Counter" ) ,
54+ decrement : translator . translate ( "Decrement" ) ,
55+ increment : translator . translate ( "Increment" )
56+ } ;
57+ }
58+ ) ;
59+
60+ function mapStateToProps ( state : Pick < IStore , "counter" | "settings" > ) : IStateToProps {
4061 return {
41- count : state . counter . count
62+ count : state . counter . count ,
63+ translations : componentTranslationsSelector ( state )
4264 } ;
4365}
4466
0 commit comments