Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get the user country and prefered language #2214

Open
wants to merge 7 commits into
base: testing
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { NextRequest, NextResponse } from 'next/server';

const middleware = async (request: NextRequest) => {
const country = request.geo?.country || 'US';
request.nextUrl.searchParams.set('country', country);
return NextResponse.rewrite(request.nextUrl);
};

export default middleware;
15 changes: 15 additions & 0 deletions src/hooks/useGetUserLanguage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useEffect, useState } from 'react';

const useGetUserLanguage = () => {
const [userLanguage, setUserLanguage] = useState('');

useEffect(() => {
if (navigator.language || navigator.userLanguage) {
const language = navigator.language || navigator.userLanguage;
setUserLanguage(language);
}
}, []);

return { userLanguage };
};
export default useGetUserLanguage;
43 changes: 24 additions & 19 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable i18next/no-literal-string */
/* eslint-disable react/no-multi-comp */
import React from 'react';

import classNames from 'classnames';
import { NextPage, GetStaticProps } from 'next';
import { GetServerSideProps, NextPage } from 'next';
import Head from 'next/head';
import useTranslation from 'next-translate/useTranslation';

Expand All @@ -23,10 +23,29 @@ import ChaptersData from 'types/ChaptersData';
type IndexProps = {
chaptersResponse: ChaptersResponse;
chaptersData: ChaptersData;
country?: string;
};

const Index: NextPage<IndexProps> = ({ chaptersResponse: { chapters } }): JSX.Element => {
export const getServerSideProps: GetServerSideProps = async ({ locale, query }) => {
const allChaptersData = await getAllChaptersData(locale);

return {
props: {
country: query.country,
chaptersData: allChaptersData,
chaptersResponse: {
chapters: Object.keys(allChaptersData).map((chapterId) => {
const chapterData = allChaptersData[chapterId];
return { ...chapterData, id: Number(chapterId) };
}),
},
},
};
};

const Index: NextPage<IndexProps> = ({ chaptersResponse: { chapters }, country }): JSX.Element => {
const { t, lang } = useTranslation('home');

return (
<>
<Head>
Expand All @@ -39,6 +58,8 @@ const Index: NextPage<IndexProps> = ({ chaptersResponse: { chapters } }): JSX.El
/>
<div className={styles.pageContainer}>
<div className={styles.flow}>
{country ? <p>{country}</p> : <p>No country detected</p>}

<HomePageHero />
<div className={classNames(styles.flowItem, styles.fullWidth)}>
<RamadanActivitiesSection />
Expand All @@ -58,20 +79,4 @@ const Index: NextPage<IndexProps> = ({ chaptersResponse: { chapters } }): JSX.El
);
};

export const getStaticProps: GetStaticProps = async ({ locale }) => {
const allChaptersData = await getAllChaptersData(locale);

return {
props: {
chaptersData: allChaptersData,
chaptersResponse: {
chapters: Object.keys(allChaptersData).map((chapterId) => {
const chapterData = allChaptersData[chapterId];
return { ...chapterData, id: Number(chapterId) };
}),
},
},
};
};

export default Index;
4 changes: 4 additions & 0 deletions types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ declare module '*.svg' {
const content: (props: SVGProps<SVGElement>) => ReactElement;
export default content;
}

interface NavigatorLanguage {
userLanguage?: string;
}