Skip to content

Commit

Permalink
feat: Stay on current page when changing lang
Browse files Browse the repository at this point in the history
  • Loading branch information
rosethorn999 committed Oct 27, 2023
1 parent 1db5c00 commit 94443ed
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Link from 'next/link';
import { useTranslation } from '../../i18n';

const FooterBar = async ({ lng }) => {
export default async function FooterBar({ lng }: { lng: string }) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const { t } = await useTranslation(lng, 'footer');
return (
Expand Down Expand Up @@ -80,6 +80,4 @@ const FooterBar = async ({ lng }) => {
</div>
</footer>
);
};

export default FooterBar;
}
10 changes: 8 additions & 2 deletions src/app/[lng]/components/HeaderBar/HeaderBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faMagnifyingGlass, faLanguage } from '@fortawesome/free-solid-svg-icons';
import Cookies from 'js-cookie';
import { languages } from '../../../i18n/settings';
import { useRouter } from 'next/navigation';
import { useRouter, useSelectedLayoutSegments, useSearchParams } from 'next/navigation';
import gymPoolLogo from '../../../../../public/gymPoolLogo.svg';

export const HeaderBase = ({ t, lng }) => {
Expand All @@ -15,6 +15,8 @@ export const HeaderBase = ({ t, lng }) => {
const [isLoggedIn, setIsLoggedIn] = useState();
const token = Cookies.get('token');
const router = useRouter();
const SearchParams = useSearchParams();
const SelectedLayoutSegments = useSelectedLayoutSegments();

useEffect(() => {
setIsLoggedIn(!!token);
Expand All @@ -28,7 +30,11 @@ export const HeaderBase = ({ t, lng }) => {
}
function clickLangSwitcher() {
const switchToLang = languages[(languages.indexOf(lng) + 1) % languages.length];
router.push(`/${switchToLang}/`);
const segments = SelectedLayoutSegments.join('/');
const queries = new URLSearchParams(SearchParams).toString();

const newUrl = `/${switchToLang}/${segments}?${queries}`;
router.push(newUrl);
}
function triggerMobileMenu(toStatus = false) {
toStatus = !isMobileMenuOpened;
Expand Down

0 comments on commit 94443ed

Please sign in to comment.