Skip to content

Commit

Permalink
feat: static paths from site instead of uri
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgoff committed Aug 27, 2024
1 parent fa4266b commit da936a3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
54 changes: 39 additions & 15 deletions components/global/Header/LanguageSelect/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,59 @@
import { useTransition } from "react";
import PropTypes from "prop-types";
import { useRouter, usePathname } from "next/navigation";
import { useRouter, useSearchParams, usePathname } from "next/navigation";
import { useTranslation } from "react-i18next";
import * as Styled from "./styles";
import { fallbackLng } from "@/lib/i18n/settings";
import { fallbackLng, languages } from "@/lib/i18n/settings";

/** stand-in for useParams() in app router */
const getParams = (pathname) => {
const uriSegments = pathname.split("/").filter((segment) => segment !== "");

if (uriSegments.length === 0) {
return { locale: fallbackLng, uriSegments: [] };
}

if (languages.includes(uriSegments[0])) {
const locale = uriSegments.shift();
return { locale, uriSegments };
} else {
return { locale: fallbackLng, uriSegments: [...uriSegments] };
}
};

const filterSearchParams = (searchParams) => {
const filteredParams = [];

searchParams.forEach((value, key) => {
if (key !== "locale" && key !== "uriSegments") {
filteredParams.push(`${key}=${value}`);
}
});

return `?${filteredParams.join("&")}`;
};

export default function LanguageSelect({ id }) {
const router = useRouter();
const {
t,
i18n: { language, changeLanguage },
} = useTranslation();
const pathname = usePathname();
const { locale, uriSegments } = getParams(pathname);
const searchParams = useSearchParams();
const { t, i18n } = useTranslation();
const [isPending, startTransition] = useTransition();

const isDefaultLocale = fallbackLng.includes(language);
const path = usePathname().split("/");
path.shift();

if (!isDefaultLocale) {
path.shift();
}
const isDefaultLocale = fallbackLng.includes(locale);

const newLocale = () => {
return isDefaultLocale ? "es" : "en";
};

const newRoute = `/${newLocale()}/${path.join("/")}`;
const newRoute = `/${newLocale()}/${uriSegments.join(
"/"
)}${filterSearchParams(searchParams)}`;

const handleClick = () => {
startTransition(() => {
changeLanguage(newLocale());
i18n.changeLanguage(newLocale());
router.push(newRoute, {
scroll: false,
});
Expand Down
13 changes: 7 additions & 6 deletions lib/api/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { glossaryTermFragment } from "@/lib/api/fragments/glossary-term";
import { studentPageFragment } from "./fragments/student-page";
import { educatorPageFragment } from "./fragments/educator-page";
import { investigationLandingPageFragment } from "./fragments/investigation-landing-page";
import { getLocaleString } from "../utils";

function dataListQueryify(fragment) {
return gql`
Expand Down Expand Up @@ -68,20 +69,23 @@ async function getData(ENV) {
) {
uri
level
siteHandle
}
homepage: entries(
site: "*"
section: ["homepage"]
type: ["not", "redirectPage"]
) {
uri
siteHandle
}
search: entries(
site: "*"
section: ["searchResults"]
type: ["not", "redirectPage"]
) {
uri
siteHandle
}
}
`;
Expand All @@ -96,6 +100,7 @@ async function getData(ENV) {
type: ["not", "redirectPage"]
) {
uri
siteHandle
}
}
`;
Expand All @@ -109,16 +114,12 @@ export async function getAllEntries() {
const data = await getData(ENV);
return data.entries
.filter(({ uri }) => uri != null)
.map(({ uri, sectionHandle }) => {
const isEs = uri.indexOf("es/") === 0;
.map(({ uri, sectionHandle, siteHandle }) => {
const segments = uri.split("/");

if (isEs) {
segments.shift();
}
return {
params: {
locale: isEs ? "es" : "en",
locale: getLocaleString(siteHandle),
uriSegments: segments,
uri,
sectionHandle,
Expand Down
4 changes: 0 additions & 4 deletions pages/[locale]/[[...uriSegments]].js
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ export async function getStaticProps({
uri = previewData.uriSegments.join("/");
previewToken = previewData?.previewToken;
} else if (uriSegments && uriSegments.length) {
if (isEspanol) {
uriSegments.unshift("es");
}

uri = uriSegments.join("/");
}

Expand Down

0 comments on commit da936a3

Please sign in to comment.