From e85d1ce94ec45d8f5d086722cfd88e0e2c5b4bb6 Mon Sep 17 00:00:00 2001 From: TheCatLady <52870424+TheCatLady@users.noreply.github.com> Date: Sun, 16 May 2021 21:13:52 -0400 Subject: [PATCH] fix(locale): properly restore display language upon page refresh (#1646) * fix(locale): properly restore display language upon page refresh * refactor: dedupe logic --- server/middleware/auth.ts | 23 +++++++++++------------ src/components/Layout/index.tsx | 17 +---------------- src/pages/_app.tsx | 8 ++++++++ 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/server/middleware/auth.ts b/server/middleware/auth.ts index f8f7b9ade..68869222f 100644 --- a/server/middleware/auth.ts +++ b/server/middleware/auth.ts @@ -5,36 +5,35 @@ import { getSettings } from '../lib/settings'; export const checkUser: Middleware = async (req, _res, next) => { const settings = getSettings(); + let user: User | undefined; if (req.header('X-API-Key') === settings.main.apiKey) { const userRepository = getRepository(User); let userId = 1; // Work on original administrator account - // If a User ID is provided, we will act on that users behalf + // If a User ID is provided, we will act on that user's behalf if (req.header('X-API-User')) { userId = Number(req.header('X-API-User')); } - const user = await userRepository.findOne({ where: { id: userId } }); - if (user) { - req.user = user; - } + user = await userRepository.findOne({ where: { id: userId } }); } else if (req.session?.userId) { const userRepository = getRepository(User); - const user = await userRepository.findOne({ + user = await userRepository.findOne({ where: { id: req.session.userId }, }); + } - if (user) { - req.user = user; - req.locale = user.settings?.locale - ? user.settings?.locale - : settings.main.locale; - } + if (user) { + req.user = user; } + req.locale = user?.settings?.locale + ? user.settings.locale + : settings.main.locale; + next(); }; diff --git a/src/components/Layout/index.tsx b/src/components/Layout/index.tsx index 2f2f8be1a..662868354 100644 --- a/src/components/Layout/index.tsx +++ b/src/components/Layout/index.tsx @@ -3,9 +3,6 @@ import { ArrowLeftIcon, InformationCircleIcon } from '@heroicons/react/solid'; import { useRouter } from 'next/router'; import React, { useEffect, useState } from 'react'; import { defineMessages, useIntl } from 'react-intl'; -import { AvailableLocale } from '../../context/LanguageContext'; -import useLocale from '../../hooks/useLocale'; -import useSettings from '../../hooks/useSettings'; import { Permission, useUser } from '../../hooks/useUser'; import SearchInput from './SearchInput'; import Sidebar from './Sidebar'; @@ -19,21 +16,9 @@ const messages = defineMessages({ const Layout: React.FC = ({ children }) => { const [isSidebarOpen, setSidebarOpen] = useState(false); const [isScrolled, setIsScrolled] = useState(false); - const { user, hasPermission } = useUser(); + const { hasPermission } = useUser(); const router = useRouter(); const intl = useIntl(); - const { currentSettings } = useSettings(); - const { setLocale } = useLocale(); - - useEffect(() => { - if (setLocale) { - setLocale( - (user?.settings?.locale - ? user.settings.locale - : currentSettings.locale) as AvailableLocale - ); - } - }, [setLocale, currentSettings.locale, user]); useEffect(() => { const updateScrolled = () => { diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index 4e73f9bb1..592d1dcb8 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -90,6 +90,14 @@ const CoreApp: Omit = ({ const [loadedMessages, setMessages] = useState(messages); const [currentLocale, setLocale] = useState(locale); + useEffect(() => { + setLocale( + (user?.settings?.locale + ? user.settings.locale + : currentSettings.locale) as AvailableLocale + ); + }, [currentSettings.locale, user?.settings?.locale]); + useEffect(() => { loadLocaleData(currentLocale).then(setMessages); }, [currentLocale]);