/
LocalizeProvider.js
140 lines (118 loc) · 3.46 KB
/
LocalizeProvider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// @flow
import React, { Component } from 'react';
import {
localizeReducer,
getActiveLanguage,
getOptions,
getTranslationsForActiveLanguage,
type LocalizeState,
type Action,
initialize as initializeAC,
INITIALIZE,
InitializePayload
} from './localize';
import {
LocalizeContext,
type LocalizeContextProps,
getContextPropsFromState
} from './LocalizeContext';
import { storeDidChange } from './utils';
type LocalizeProviderState = {
localize: LocalizeState
};
export type LocalizeProviderProps = {
store?: any,
getState?: Function,
initialize?: InitializePayload,
children: any
};
export class LocalizeProvider extends Component<
LocalizeProviderProps,
LocalizeProviderState
> {
unsubscribeFromStore: Function;
getContextPropsSelector: any;
contextProps: LocalizeContextProps;
constructor(props: LocalizeProviderProps) {
super(props);
const dispatch = this.props.store
? this.props.store.dispatch
: this.dispatch.bind(this);
this.getContextPropsSelector = getContextPropsFromState(dispatch);
const initialState =
this.props.initialize !== undefined
? localizeReducer(undefined, {
type: INITIALIZE,
payload: this.props.initialize
})
: localizeReducer(undefined, ({}: any));
this.state = {
localize: initialState
};
}
componentDidMount() {
this.initExternalStore();
this.subscribeToExternalStore();
}
componentWillUnmount() {
this.unsubscribeFromStore && this.unsubscribeFromStore();
}
initExternalStore() {
const { store, initialize } = this.props;
if (store && initialize) {
store.dispatch(initializeAC(initialize));
}
}
subscribeToExternalStore() {
const { store } = this.props;
if (store) {
this.unsubscribeFromStore = storeDidChange(
store,
this.onStateDidChange.bind(this)
);
}
}
onStateDidChange(prevState: LocalizeProviderState) {
if (!this.props.store) {
return;
}
const getState = this.props.getState || (state => state.localize);
const prevLocalizeState = prevState && getState(prevState);
const curLocalizeState = getState(this.props.store.getState());
const prevActiveLanguage =
prevState && getActiveLanguage(prevLocalizeState);
const curActiveLanguage = getActiveLanguage(curLocalizeState);
const prevOptions = prevState && getOptions(prevLocalizeState);
const curOptions = getOptions(curLocalizeState);
const prevTranslations =
prevState && getTranslationsForActiveLanguage(prevLocalizeState);
const curTranslations = getTranslationsForActiveLanguage(curLocalizeState);
const hasActiveLangaugeChanged =
(prevActiveLanguage && prevActiveLanguage.code) !==
(curActiveLanguage && curActiveLanguage.code);
const hasOptionsChanged = prevOptions !== curOptions;
const hasTranslationsChanged = prevTranslations !== curTranslations;
if (
hasActiveLangaugeChanged ||
hasOptionsChanged ||
hasTranslationsChanged
) {
this.setState({ localize: curLocalizeState });
}
}
dispatch(action: any) {
this.setState(prevState => {
return {
localize: localizeReducer(prevState.localize, action)
};
});
}
render() {
this.contextProps = this.getContextPropsSelector(this.state.localize);
return (
<LocalizeContext.Provider value={this.contextProps}>
{this.props.children}
</LocalizeContext.Provider>
);
}
}