Skip to content

Commit

Permalink
update interface language when logging in
Browse files Browse the repository at this point in the history
  • Loading branch information
hj940709 committed Oct 7, 2024
1 parent 257661b commit 9c2d144
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 13 deletions.
4 changes: 3 additions & 1 deletion client/components/AccessControl/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useHistory, useLocation } from 'react-router'
import { FormattedMessage, useIntl } from 'react-intl'
import { Button, Spinner } from 'react-bootstrap'
import ForgotPassword from './ForgotPassword'
import { localeCodeToName } from 'Utilities/common'

const Login = () => {
const [email, setEmail] = useState('')
Expand All @@ -14,14 +15,15 @@ const Login = () => {
const loginError = useSelector(({ user }) => user.error)
const errorMessage = useSelector(({ user }) => user.errorMessage)
const { user, pending } = useSelector(({ user }) => user)
const { locale, updated } = useSelector(({ locale }) => locale)
const location = useLocation()
const history = useHistory()
const intl = useIntl()

const dispatch = useDispatch()

const login = () => {
dispatch(createRealToken(email, password))
dispatch(createRealToken(email, password, updated && localeCodeToName(locale) || null))
}

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion client/components/AccessControl/Register.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const Register = () => {
accountCreated,
} = useSelector(({ register }) => register)
const userEmail = useSelector(({ user }) => user.data?.user?.email)
const locale = useSelector(({ locale }) => locale)
const { locale } = useSelector(({ locale }) => locale)

const dispatch = useDispatch()

Expand Down
2 changes: 1 addition & 1 deletion client/components/ConnectedIntlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import translations from 'Utilities/translations'
import { localeNameToCode } from '../util/common'

const mapStateToProps = ({ locale, user }) => {
let actualLocale = locale
let actualLocale = locale.locale

if (user.data && user.data.user && user.data.user.interfaceLanguage) {
// If user has logged in, use locale from user object, else use value from localeReducer
Expand Down
2 changes: 1 addition & 1 deletion client/components/LandingPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const LandingPage = () => {
const [registering, setRegistering] = useState(false)

const open = useSelector(({ sidebar }) => sidebar.open)
const locale = useSelector(({ locale }) => locale)
const { locale } = useSelector(({ locale }) => locale)
const { landingPageLangManuallySelected } = useSelector(({ user }) => user)
const { pending, accountCreated } = useSelector(({ register }) => register)

Expand Down
2 changes: 1 addition & 1 deletion client/components/NavBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default function NavBar() {
const open = useSelector(({ sidebar }) => sidebar.open)
const storyLanguage = (storyPending == false) & (story != undefined) ? story.language : undefined
const learningLanguage = useSelector(learningLanguageSelector)
const locale = useSelector(({ locale }) => locale)
const { locale } = useSelector(({ locale }) => locale)
const dispatch = useDispatch()
const history = useHistory()
const smallWindow = useWindowDimensions().width < 700
Expand Down
2 changes: 1 addition & 1 deletion client/components/Profile/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const SettingToggle = ({ translationId, ...props }) => {
const Settings = ({teacherView}) => {
const { data, pending } = useSelector(({ user }) => user)
const { user } = data
const locale = useSelector(({ locale }) => locale)
const { locale } = useSelector(({ locale }) => locale)
const { groups } = useSelector(({ groups }) => groups)
const dispatch = useDispatch()
const intl = useIntl()
Expand Down
2 changes: 1 addition & 1 deletion client/components/Sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function Sidebar({ history }) {
// const irtScore = useSelector(({ user }) => user.irt_dummy_score)
const open = useSelector(({ sidebar }) => sidebar.open)
const learningLanguage = user?.user?.last_used_language
const locale = useSelector(({ locale }) => locale)
const { locale } = useSelector(({ locale }) => locale)
const { hasTests, hasAdaptiveTests } = useSelector(({ metadata }) => metadata)
const [localeDropdownOptions, setLocaleDropdownOptions] = useState([])
const [practiceModalOpen, setPracticeModalOpen] = useState(false)
Expand Down
2 changes: 1 addition & 1 deletion client/components/StaticContent/Help.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Segment } from 'semantic-ui-react'

const Help = () => {
const interfaceLanguage = useSelector(({ user }) => user.data && user.data.user.interfaceLanguage)
const locale = useSelector(({ locale }) => locale)
const { locale } = useSelector(({ locale }) => locale)

const lang = interfaceLanguage || locale

Expand Down
12 changes: 9 additions & 3 deletions client/util/redux/localeReducer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
export const setLocale = locale => ({ type: 'SET_LOCALE', locale })

export default (state = 'en', action) => {
const initialState = {
locale: 'en',
updated: false
}
export default (state=initialState, action) => {
switch (action.type) {
case 'SET_LOCALE':
return action.locale
return {
locale: action.locale,
updated: true
}
default:
return state
}
Expand Down
4 changes: 2 additions & 2 deletions client/util/redux/userReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export const getLatestIRTScore = (language) => {
return callBuilder(route, prefix, 'get')
}

export const createRealToken = (email, password) => {
export const createRealToken = (email, password, locale) => {
const route = '/session/'
const prefix = 'LOGIN'
const payload = { email, password }
const payload = { email, password, interface_language: locale }
return callBuilder(route, prefix, 'post', payload)
}

Expand Down

0 comments on commit 9c2d144

Please sign in to comment.