From 58be44ad9edb31bc05244f301c4f2e4ff7e34dd2 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 21 Jun 2018 13:51:39 +0100 Subject: [PATCH] factor out conditional LanguageSelector as functional component and reuse in Login, ForgotPassword and Registration views Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../structures/login/ForgotPassword.js | 4 +- .../structures/login/LanguageSelector.js | 38 +++++++++++++++++++ src/components/structures/login/Login.js | 25 ++---------- .../structures/login/Registration.js | 3 ++ 4 files changed, 48 insertions(+), 22 deletions(-) create mode 100644 src/components/structures/login/LanguageSelector.js diff --git a/src/components/structures/login/ForgotPassword.js b/src/components/structures/login/ForgotPassword.js index ca50b9db6e..cf054a4e6e 100644 --- a/src/components/structures/login/ForgotPassword.js +++ b/src/components/structures/login/ForgotPassword.js @@ -1,6 +1,6 @@ /* Copyright 2015, 2016 OpenMarket Ltd -Copyright 2017 New Vector Ltd +Copyright 2017, 2018 New Vector Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ import MatrixClientPeg from "../../../MatrixClientPeg"; import SdkConfig from "../../../SdkConfig"; import PasswordReset from "../../../PasswordReset"; +import makeLanguageSelector from "./LanguageSelector"; module.exports = React.createClass({ displayName: 'ForgotPassword', @@ -233,6 +234,7 @@ module.exports = React.createClass({ { _t('Create an account') } + { makeLanguageSelector() } diff --git a/src/components/structures/login/LanguageSelector.js b/src/components/structures/login/LanguageSelector.js new file mode 100644 index 0000000000..1e576bdbb8 --- /dev/null +++ b/src/components/structures/login/LanguageSelector.js @@ -0,0 +1,38 @@ +/* +Copyright 2018 New Vector Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import SdkConfig from "../../../SdkConfig"; +import {getCurrentLanguage} from "../../../languageHandler"; +import SettingsStore, {SettingLevel} from "../../../settings/SettingsStore"; +import PlatformPeg from "../../../PlatformPeg"; +import sdk from '../../../index'; +import React from 'react'; + +function onChange(newLang) { + if (getCurrentLanguage() !== newLang) { + SettingsStore.setValue("language", null, SettingLevel.DEVICE, newLang); + PlatformPeg.get().reload(); + } +} + +export default function makeLanguageSelector() { + if (SdkConfig.get()['disable_login_language_selector']) return
; + + const LanguageDropdown = sdk.getComponent('views.elements.LanguageDropdown'); + return
+ +
; +} diff --git a/src/components/structures/login/Login.js b/src/components/structures/login/Login.js index 1332f52f97..60e4e51f04 100644 --- a/src/components/structures/login/Login.js +++ b/src/components/structures/login/Login.js @@ -1,6 +1,7 @@ /* Copyright 2015, 2016 OpenMarket Ltd Copyright 2017 Vector Creations Ltd +Copyright 2018 New Vector Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -20,12 +21,11 @@ limitations under the License. import React from 'react'; import PropTypes from 'prop-types'; import { _t } from '../../../languageHandler'; -import * as languageHandler from '../../../languageHandler'; import sdk from '../../../index'; import Login from '../../../Login'; -import PlatformPeg from '../../../PlatformPeg'; import SdkConfig from '../../../SdkConfig'; -import SettingsStore, {SettingLevel} from "../../../settings/SettingsStore"; +import SettingsStore from "../../../settings/SettingsStore"; +import makeLanguageSelector from "./LanguageSelector"; // For validating phone numbers without country codes const PHONE_NUMBER_REGEX = /^[0-9\(\)\-\s]*$/; @@ -370,23 +370,6 @@ module.exports = React.createClass({ ); }, - _onLanguageChange: function(newLang) { - if (languageHandler.getCurrentLanguage() !== newLang) { - SettingsStore.setValue("language", null, SettingLevel.DEVICE, newLang); - PlatformPeg.get().reload(); - } - }, - - _renderLanguageSetting: function() { - const LanguageDropdown = sdk.getComponent('views.elements.LanguageDropdown'); - return
- -
; - }, - render: function() { const Loader = sdk.getComponent("elements.Spinner"); const LoginPage = sdk.getComponent("login.LoginPage"); @@ -461,7 +444,7 @@ module.exports = React.createClass({ { loginAsGuestJsx } { returnToAppJsx } - { !SdkConfig.get().disable_login_language_selector ? this._renderLanguageSetting() : '' } + { makeLanguageSelector() }
diff --git a/src/components/structures/login/Registration.js b/src/components/structures/login/Registration.js index 62a3ee4f68..377cffc576 100644 --- a/src/components/structures/login/Registration.js +++ b/src/components/structures/login/Registration.js @@ -1,6 +1,7 @@ /* Copyright 2015, 2016 OpenMarket Ltd Copyright 2017 Vector Creations Ltd +Copyright 2018 New Vector Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -29,6 +30,7 @@ import RtsClient from '../../../RtsClient'; import { _t } from '../../../languageHandler'; import SdkConfig from '../../../SdkConfig'; import SettingsStore from "../../../settings/SettingsStore"; +import makeLanguageSelector from "./LanguageSelector"; const MIN_PASSWORD_LENGTH = 6; @@ -432,6 +434,7 @@ module.exports = React.createClass({ { signIn } { errorText } { returnToAppJsx } + { makeLanguageSelector() }