[NEW] Add setLanguage method in livechat api methods#214
[NEW] Add setLanguage method in livechat api methods#214renatobecker-zz merged 6 commits intoRocketChat:devfrom
Conversation
213950b to
9d593dd
Compare
src/components/App/index.js
Outdated
| await loadConfig(); | ||
| this.handleTriggers(); | ||
| CustomFields.init(); | ||
| Language.init(); |
There was a problem hiding this comment.
Why did you create a class to handle Language stuff instead of just exporting the methods you need to use?
src/lib/hooks.js
Outdated
| @@ -1,5 +1,6 @@ | |||
| import Triggers from './triggers'; | |||
| import CustomFields from './customFields'; | |||
| import Language from './language'; | |||
There was a problem hiding this comment.
You don't need a class to call just one method, you can create a file and export the method there.
src/lib/hooks.js
Outdated
| }, | ||
|
|
||
| setLanguage(language) { | ||
| Language.setLanguage(language); |
There was a problem hiding this comment.
Please, export the setLanguage method rather than instantiate a class to call the method.
src/lib/language.js
Outdated
| @@ -0,0 +1,68 @@ | |||
| import store from '../store'; | |||
|
|
|||
| class Language { | |||
There was a problem hiding this comment.
You don't need all of this stuff. The idea is to export the method(s) you want to call.
Please fix it.
|
Updated changes |
src/lib/language.js
Outdated
| /** | ||
| * To get browser Language of user | ||
| */ | ||
| export const browserLanguage = () => { |
There was a problem hiding this comment.
You don't need to export this method.
src/lib/language.js
Outdated
| * To normalize Language String and return language code | ||
| * @param {String} languageString | ||
| */ | ||
| export const normalizeLanguageString = (languageString) => { |
There was a problem hiding this comment.
You don't need to export this method.
src/components/App/index.js
Outdated
| } | ||
|
|
||
| return countryCode ? `${ languageCode }_${ countryCode }` : languageCode; | ||
| const iframeLanguage = () => { |
There was a problem hiding this comment.
Since you're calling updateWidgetLanguage(), I think you don't need this block of code here, right?
src/components/App/index.js
Outdated
| }; | ||
|
|
||
| I18n.changeLocale(normalizeLanguageString(configLanguage() || browserLanguage())); | ||
| I18n.changeLocale(normalizeLanguageString(iframeLanguage() ? iframeLanguage() : configLanguage() || browserLanguage())); |
There was a problem hiding this comment.
Move this code into updateWidgetLanguage method.
src/components/App/index.js
Outdated
| expanded, | ||
| alerts, | ||
| modal, | ||
| iframe, |
There was a problem hiding this comment.
Is this still necessary?
src/lib/hooks.js
Outdated
|
|
||
| setLanguage(language) { | ||
| setLanguage(language); | ||
| updateWidgetLanguage(); |
There was a problem hiding this comment.
Call this method inside setLanguage method.
src/lib/language.js
Outdated
| */ | ||
| export const setLanguage = (language) => { | ||
| const { iframe } = store.state; | ||
| store.setState({ iframe: { ...iframe, language } }); |
There was a problem hiding this comment.
| store.setState({ iframe: { ...iframe, language } }); | |
| await store.setState({ iframe: { ...iframe, language } }); |
src/lib/language.js
Outdated
| * This will update langauge of widget | ||
| */ | ||
| export const updateWidgetLanguage = () => { | ||
| const { iframe } = store.state; |
There was a problem hiding this comment.
Please, use Object destructuring below as well.
src/lib/language.js
Outdated
| */ | ||
| export const updateWidgetLanguage = () => { | ||
| const { iframe } = store.state; | ||
| if (iframe && iframe.language) { |
There was a problem hiding this comment.
Improve this code. Instead of if/else, prepare the language value before calling I18n.changeLocale.
Please, improve the way you write the code.
src/lib/language.js
Outdated
| * This will update langauge of widget | ||
| */ | ||
| export const updateWidgetLanguage = () => { | ||
| const { iframe: { language } = {} } = store.state; |
There was a problem hiding this comment.
Why not dealing with this information on configLanguage method?
You don't need to read the store so many times since you're reading the store on configLanguage method.
Please, test the iframe data on configLanguage method.
src/lib/language.js
Outdated
| */ | ||
| export const updateWidgetLanguage = () => { | ||
| const { iframe: { language } = {} } = store.state; | ||
| I18n.changeLocale(language ? language : normalizeLanguageString(configLanguage() || browserLanguage())); |
There was a problem hiding this comment.
| I18n.changeLocale(language ? language : normalizeLanguageString(configLanguage() || browserLanguage())); | |
| I18n.changeLocale(normalizeLanguageString(configLanguage() || browserLanguage())); |
Related to the comment I left above..
src/lib/language.js
Outdated
| /** | ||
| * This will update langauge of widget | ||
| */ | ||
| export const updateWidgetLanguage = () => { |
There was a problem hiding this comment.
Let's replace the name of this method by setWidgetLanguage.
243e33d to
9fef76a
Compare
src/lib/language.js
Outdated
| /** | ||
| * To get browser Language of user | ||
| */ | ||
| const browserLanguage = () => { |
There was a problem hiding this comment.
| const browserLanguage = () => { | |
| const browserLanguage = () => (navigator.userLanguage || navigator.language); |
Use the correct arrow function statement. Your function does not have a return.
src/lib/hooks.js
Outdated
| createOrUpdateGuest(data); | ||
| }, | ||
|
|
||
| setLanguage(language) { |
There was a problem hiding this comment.
You don't need this method here, you can set the iframe language on the setLanguage method created on hooks.js.
TODO
iframe