Skip to content

Commit 719b441

Browse files
committed
improve change locale logic
1 parent 6a37cc2 commit 719b441

File tree

8 files changed

+85
-81
lines changed

8 files changed

+85
-81
lines changed

src/App/AppRoutes/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ const AppRoutes = ({ show }) => {
1212
// untill some API fetch to complete
1313
if (show) {
1414
return (
15-
<Switch>
16-
<Route exact path="/" component={Home} />
17-
<Route path="/about" component={About} />
18-
<Route path="/topics" component={Topics} />
19-
<Route component={Page404} />
20-
</Switch>
15+
<Switch>
16+
<Route exact path="/" component={Home} />
17+
<Route path="/about" component={About} />
18+
<Route path="/topics" component={Topics} />
19+
<Route component={Page404} />
20+
</Switch>
2121
);
2222
}
2323

src/App/DemoToBeDeleted/components/DemoSwitchLocale/component.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import intl from 'react-intl-universal';
3+
import { arrayOf, shape, string, func } from 'prop-types';
34

45
const DemoSwitchLocale = ({ locale, selected, changeLocale }) => (
56
<>
@@ -9,7 +10,7 @@ const DemoSwitchLocale = ({ locale, selected, changeLocale }) => (
910
<button
1011
type="button"
1112
disabled={id === selected}
12-
onClick={() => changeLocale({ id })}
13+
onClick={() => changeLocale(id)}
1314
>
1415
{name}
1516
</button>
@@ -18,4 +19,10 @@ const DemoSwitchLocale = ({ locale, selected, changeLocale }) => (
1819
</>
1920
);
2021

22+
DemoSwitchLocale.propTypes = {
23+
locale: arrayOf(shape({ id: string, name: string })).isRequired,
24+
selected: string.isRequired,
25+
changeLocale: func.isRequired
26+
};
27+
2128
export default DemoSwitchLocale;

src/App/DemoToBeDeleted/components/DemoSwitchLocale/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import DemoSwitchLocale from './component';
44

55
const mapState = ({ locale }) => ({
66
locale: locale.data,
7-
selected: locale.selected,
7+
selected: locale.selected.id,
88
});
99

1010
const mapDispatch = ({ locale }) => ({

src/App/DemoToBeDeleted/components/DemoSwitchTheme/component.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import intl from 'react-intl-universal';
3+
import { arrayOf, shape, string, func } from 'prop-types';
34

45
const DemoSwitchTheme = ({ themes, selected, changeTheme }) => (
56
<>
@@ -18,4 +19,10 @@ const DemoSwitchTheme = ({ themes, selected, changeTheme }) => (
1819
</>
1920
);
2021

22+
DemoSwitchTheme.propTypes = {
23+
themes: arrayOf(shape({ id: string, name: string })).isRequired,
24+
selected: string.isRequired,
25+
changeTheme: func.isRequired
26+
};
27+
2128
export default DemoSwitchTheme;

src/App/DemoToBeDeleted/components/DemoSwitchTheme/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import DemoSwitchTheme from './component';
44

55
const mapState = ({ themes }) => ({
66
themes: themes.data,
7-
selected: themes.selected,
7+
selected: themes.selected.id,
88
});
99

1010
const mapDispatch = ({ themes }) => ({

src/App/index.js

Lines changed: 23 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useEffect, useCallback } from 'react';
2-
import PropTypes from 'prop-types';
2+
import { arrayOf, shape, string, func } from 'prop-types';
33
import { connect } from 'react-redux';
44
import { ThemeProvider } from 'styled-components';
55
import intl from 'react-intl-universal';
@@ -12,58 +12,27 @@ import AppScrollToTop from './AppScrollToTop';
1212
import AppRoutes from './AppRoutes';
1313
import DemoToBeDeleted from './DemoToBeDeleted';
1414

15-
const App = ({ themes, changeTheme, locale, changeLocale }) => {
16-
const loadLocale = useCallback(
17-
userLocale => {
18-
const hasLocale = check => locale.data.find(({ id }) => id === check);
19-
20-
if (!!userLocale && !hasLocale(userLocale)) {
21-
return;
22-
}
23-
24-
let currentLocale;
25-
26-
if (!!userLocale && hasLocale(userLocale)) {
27-
currentLocale = userLocale;
28-
} else {
29-
currentLocale = intl.determineLocale({
30-
cookieLocaleKey: 'lang'
31-
});
32-
33-
if (!hasLocale(currentLocale)) {
34-
currentLocale = 'en-US';
35-
}
36-
}
37-
38-
const onChangeLocale = () => {
39-
changeLocale({ id: currentLocale });
40-
};
41-
42-
import(`../../config/locale/${currentLocale}.json`)
43-
.then(res =>
44-
intl.init({
45-
currentLocale,
46-
locales: {
47-
[currentLocale]: res
48-
}
49-
})
50-
)
51-
.then(onChangeLocale)
52-
.catch(onChangeLocale);
53-
},
54-
[changeLocale]
55-
);
15+
const App = ({ selectedTheme, selectedLocale, locale, changeLocale }) => {
5616

17+
// try and detect locale
18+
// just once when the app loads
5719
useEffect(() => {
58-
loadLocale();
59-
}, [loadLocale]);
20+
const currentLocale = intl.determineLocale({
21+
cookieLocaleKey: 'lang'
22+
});
23+
const hasLocale = locale.find(({ id }) => id === currentLocale)
24+
25+
if (currentLocale !== selectedLocale && hasLocale) {
26+
changeLocale(currentLocale);
27+
}
28+
}, [changeLocale]);
6029

6130
const getTheme = useCallback(() => {
6231
let theme = {};
6332

64-
if (themes.selected.id === 'white_theme') {
33+
if (selectedTheme === 'white_theme') {
6534
theme = whitetheme;
66-
} else if (themes.selected.id === 'dark_theme') {
35+
} else if (selectedTheme === 'dark_theme') {
6736
theme = darktheme;
6837
}
6938

@@ -87,10 +56,10 @@ const App = ({ themes, changeTheme, locale, changeLocale }) => {
8756
};
8857

8958
App.propTypes = {
90-
themes: PropTypes.object.isRequired,
91-
locale: PropTypes.object.isRequired,
92-
changeTheme: PropTypes.func.isRequired,
93-
changeLocale: PropTypes.func.isRequired
59+
locale: arrayOf(shape({ id: string, name: string })).isRequired,
60+
selectedLocale: string.isRequired,
61+
selectedTheme: string.isRequired,
62+
changeLocale: func.isRequired
9463
};
9564

9665
/**
@@ -99,12 +68,12 @@ App.propTypes = {
9968
*/
10069

10170
const mapState = ({ themes, locale }) => ({
102-
themes,
103-
locale
71+
locale: locale.data,
72+
selectedLocale: locale.selected.id,
73+
selectedTheme: themes.selected.id
10474
});
10575

106-
const mapDispatch = ({ themes, locale }) => ({
107-
changeTheme: themes.changeTheme,
76+
const mapDispatch = ({ locale }) => ({
10877
changeLocale: locale.changeLocale
10978
});
11079

src/models/locale.js

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,53 @@
1-
const data = [{
2-
id: 'en-US',
3-
value: 'en-US',
4-
name: 'English',
5-
}, {
6-
id: 'fr-FR',
7-
value: 'fr-FR',
8-
name: 'French',
9-
}, {
10-
id: 'ja-JP',
11-
value: 'ja-JP',
12-
name: 'Japanese',
13-
}];
1+
import intl from 'react-intl-universal';
2+
3+
const data = [
4+
{
5+
id: 'en-US',
6+
value: 'en-US',
7+
name: 'English'
8+
},
9+
{
10+
id: 'fr-FR',
11+
value: 'fr-FR',
12+
name: 'French'
13+
},
14+
{
15+
id: 'ja-JP',
16+
value: 'ja-JP',
17+
name: 'Japanese'
18+
}
19+
];
1420

1521
const selected = data[0];
1622

1723
const initialState = {
1824
data,
19-
selected,
25+
selected
2026
};
2127

2228
export const locale = {
2329
state: initialState,
2430
reducers: {
25-
changeLocale(state, payload) {
31+
updateLocale(state, currentLocale) {
2632
return {
2733
...state,
28-
selected: data.find(({ id }) => id === payload.id) || state.selected,
34+
selected: data.find(({ id }) => id === currentLocale) || state.selected
2935
};
30-
},
36+
}
3137
},
38+
effects: dispatch => ({
39+
async changeLocale(currentLocale) {
40+
console.log(currentLocale)
41+
await import(`../../config/locale/${currentLocale}.json`).then(res =>
42+
intl.init({
43+
currentLocale,
44+
locales: {
45+
[currentLocale]: res
46+
}
47+
})
48+
);
49+
50+
dispatch.locale.updateLocale(currentLocale);
51+
}
52+
})
3253
};

src/services/dynamicStyles/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ const dynamicStyles = (uid, styles) => {
2626
}
2727
};
2828

29-
export { dynamicStyles as default };
29+
export default dynamicStyles;

0 commit comments

Comments
 (0)