Skip to content

Commit

Permalink
DIGG-512: Initital update to update paths to readable url
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaMunterud committed Jan 7, 2025
1 parent d738195 commit 2b31cbf
Show file tree
Hide file tree
Showing 34 changed files with 766 additions and 318 deletions.
3 changes: 3 additions & 0 deletions env/env-settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export abstract class EnvSettings {
abstract PRODUCTION_BASE_URL: string;
abstract SANDBOX_BASE_URL: string;

abstract ENTRYSCAPE_DATASETS_PATH: string;
abstract ENTRYSCAPE_SPECS_PATH: string;
abstract ENTRYSCAPE_TERMS_PATH: string;
Expand Down
5 changes: 3 additions & 2 deletions env/settings.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { generateRandomKey } from "../utilities";
import { EnvSettings } from "./env-settings";

export class Settings_Dev extends EnvSettings {
ENTRYSCAPE_DATASETS_PATH = "admin.dataportal.se";
// ENTRYSCAPE_DATASETS_PATH="sandbox.admin.dataportal.se"
PRODUCTION_BASE_URL = "https://dataportal.se";
SANDBOX_BASE_URL = "https://www-sandbox.dataportal.se";

ENTRYSCAPE_DATASETS_PATH = "admin.dataportal.se";
ENTRYSCAPE_SPECS_PATH = "admin.dataportal.se";
ENTRYSCAPE_TERMS_PATH = "editera.dataportal.se";
ENTRYSCAPE_MQA_PATH = "admin.dataportal.se";
Expand Down
3 changes: 3 additions & 0 deletions env/settings.prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { generateRandomKey } from "../utilities";
import { EnvSettings } from "./env-settings";

export class Settings_Prod extends EnvSettings {
PRODUCTION_BASE_URL = "https://dataportal.se";
SANDBOX_BASE_URL = "https://www-sandbox.dataportal.se";

ENTRYSCAPE_DATASETS_PATH = "admin.dataportal.se";
ENTRYSCAPE_SPECS_PATH = "admin.dataportal.se";
ENTRYSCAPE_TERMS_PATH = "editera.dataportal.se";
Expand Down
3 changes: 3 additions & 0 deletions env/settings.sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { generateRandomKey } from "../utilities";
import { EnvSettings } from "./env-settings";

export class Settings_Sandbox extends EnvSettings {
PRODUCTION_BASE_URL = "https://dataportal.se";
SANDBOX_BASE_URL = "https://www-sandbox.dataportal.se";

ENTRYSCAPE_DATASETS_PATH = "sandbox.admin.dataportal.se";
ENTRYSCAPE_SPECS_PATH = "sandbox.admin.dataportal.se";
ENTRYSCAPE_TERMS_PATH = "sandbox.editera.dataportal.se";
Expand Down
3 changes: 3 additions & 0 deletions env/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { generateRandomKey } from "../utilities";
import { EnvSettings } from "./env-settings";

export class Settings_Test extends EnvSettings {
PRODUCTION_BASE_URL = "https://dataportal.se";
SANDBOX_BASE_URL = "https://www-sandbox.dataportal.se";

ENTRYSCAPE_DATASETS_PATH = "admin.dataportal.se";
ENTRYSCAPE_SPECS_PATH = "editera.dataportal.se";
ENTRYSCAPE_TERMS_PATH = "editera.dataportal.se";
Expand Down
8 changes: 5 additions & 3 deletions features/entryscape/mqa-category-page/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { usePathname } from "next/navigation";
import useTranslation from "next-translate/useTranslation";
import { FC, useContext, useEffect } from "react";

import { Container } from "@/components/layout/container";
Expand All @@ -9,13 +10,14 @@ import { linkBase } from "@/utilities";

export const MQACategoryPage: FC = () => {
const entry = useContext(EntrystoreContext);
const { lang, t } = useTranslation();
const { setBreadcrumb } = useContext(SettingsContext);
const pathname = usePathname();

useEntryScapeBlocks({
entrystoreBase: entry.env.ENTRYSCAPE_MQA_PATH,
env: entry.env,
lang: "sv",
lang: lang,
pageType: "mqa",
context: entry.context,
esId: entry.esId,
Expand All @@ -27,10 +29,10 @@ export const MQACategoryPage: FC = () => {
crumbs: [
{ name: "start", link: { ...linkBase, link: "/" } },
{
name: "Metadatakvalitet per katalog",
name: t("routes|metadata$title"),
link: {
...linkBase,
link: `/metadatakvalitet`,
link: `/${t(`routes|metadata$path`)}`,
},
},
],
Expand Down
4 changes: 2 additions & 2 deletions features/entryscape/mqa-page/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { linkBase } from "@/utilities";

export const MQAPage: FC = () => {
const { env, setBreadcrumb } = useContext(SettingsContext);
const { lang } = useTranslation();
const { lang, t } = useTranslation();
const pathname = usePathname();
const pageTitle = "Metadatakvalitet per katalog";
const pageTitle = t("routes|metadata$title");

useEntryScapeBlocks({
entrystoreBase: `https://${env.ENTRYSCAPE_MQA_PATH}/store`,
Expand Down
7 changes: 6 additions & 1 deletion features/search/search-page/search-page-entryscape/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SearchPageSelector } from "@/features/search/search-page-selector";
import { SearchResults } from "@/features/search/search-results";
import SearchProvider, { SearchContext } from "@/providers/search-provider";
import { SettingsContext } from "@/providers/settings-provider";
import { linkBase } from "@/utilities";
import { handleLocale, linkBase } from "@/utilities";

import { createSearchProviderSettings } from "./search-page-provider-settings";

Expand All @@ -29,6 +29,11 @@ export const SearchPageEntryscape: FC<SearchPageEntryscapeProps> = ({
const [query, setQuery] = useState("");
const router = useRouter();

// Remove locale from path if it's the default locale
useEffect(() => {
handleLocale(window.location.pathname, lang, router.asPath, router);
}, [router.asPath]);

useEffect(() => {
if (typeof window === "undefined") return;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { EnvSettings } from "@/env";
import { SearchSortOrder } from "@/providers/search-provider";
import { ESRdfType, ESType } from "@/types/entrystore-core";
import {
specsPathResolver,
conceptsPathResolver,
} from "@/utilities/entrystore/entrystore-helpers";

interface FacetConfig {
resource: string;
Expand Down Expand Up @@ -285,6 +289,7 @@ export function createSearchProviderSettings(env: EnvSettings, lang: string) {
path: `/specifications/`,
titleResource: "dcterms:title",
descriptionResource: "dcterms:description",
pathResolver: specsPathResolver,
},
},
facetSpecification: {
Expand Down Expand Up @@ -372,6 +377,7 @@ export function createSearchProviderSettings(env: EnvSettings, lang: string) {
path: `/concepts/`,
titleResource: "http://www.w3.org/2004/02/skos/core#prefLabel",
descriptionResource: "http://www.w3.org/2004/02/skos/core#definition",
pathResolver: conceptsPathResolver,
},
},
facetSpecification: {
Expand Down
4 changes: 4 additions & 0 deletions locales/en/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
"path": "sv",
"title": "Svenska"
},
"metadata": {
"path": "metadatakvalitet",
"title": "Metadata quality per catalog"
},
"news": {
"path": "",
"title": "News"
Expand Down
54 changes: 54 additions & 0 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

import i18n from "./i18n";

function getLocale(request: NextRequest): string {
const acceptLanguage = request.headers.get("accept-language");
if (acceptLanguage) {
const [browserLocale] = acceptLanguage.split(",");
if (i18n.locales.includes(browserLocale as string)) {
return browserLocale;
}
}
return i18n.defaultLocale;
}

export function middleware(request: NextRequest) {
const pathname = request.nextUrl.pathname;

// Check if the pathname already has a locale
const pathnameHasLocale = i18n.locales.some(
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`,
);

if (pathnameHasLocale) {
// If it's the default locale, redirect to remove it from URL
if (pathname.startsWith(`/${i18n.defaultLocale}/`)) {
const newPathname = pathname.replace(`/${i18n.defaultLocale}`, "") || "/";
const newUrl = new URL(newPathname, request.url);
newUrl.search = request.nextUrl.search;
return NextResponse.redirect(newUrl);
}
return NextResponse.next();
}

const locale = getLocale(request);

// Only add locale to URL if it's not the default locale
if (locale !== i18n.defaultLocale) {
const newUrl = new URL(`/${locale}${pathname}`, request.url);
newUrl.search = request.nextUrl.search;
return NextResponse.redirect(newUrl);
}

// For default locale, just continue without modification
return NextResponse.next();
}

export const config = {
matcher: [
"/((?!api|_next/static|_next/image|favicon.ico|__ENV.js|manifest.json|.*\\.(?:jpg|jpeg|gif|png|svg|woff|woff2)).*)",
"/",
],
};
24 changes: 0 additions & 24 deletions middlewere.ts

This file was deleted.

62 changes: 48 additions & 14 deletions pages/concepts/[concept]/[param].tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,53 @@
import { GetServerSidePropsContext } from "next/types";
import { useRouter } from "next/router";
import { useContext, useEffect, useState } from "react";

import { ConceptPage } from "@/features/entryscape/concept-page";
import { EntrystoreProvider } from "@/providers/entrystore-provider";
import { SettingsContext } from "@/providers/settings-provider";
import { handleEntryStoreRedirect } from "@/utilities/entrystore/entrystore-redirect";

export default function Concept() {
return null;
}
const { env } = useContext(SettingsContext);
const router = useRouter();
const { concept, param } = router.query || {};
const [resourceUri, setResourceUri] = useState<string | null>(null);

useEffect(() => {
const fetchEntryStoreProps = async () => {
if (!concept || !param) return;
const isSandbox = window.location.host.includes("sandbox");

const data = await handleEntryStoreRedirect(
{
pathPrefix: "/concepts",
redirectPath: "/concepts",
entrystorePathKey: "ENTRYSCAPE_TERMS_PATH",
param: concept,
secondParam: param as string,
},
router,
router.locale || "sv",
isSandbox,
);

if (data?.resourceUri) {
setResourceUri(data.resourceUri);
}
};

export const getServerSideProps = async (
context: GetServerSidePropsContext,
) => {
return handleEntryStoreRedirect(context, {
pathPrefix: "/concepts",
redirectPath: "/concepts",
entrystorePathKey: "ENTRYSCAPE_TERMS_PATH",
paramName: "concept",
secondParamName: "param",
});
};
fetchEntryStoreProps();
}, [concept, param]);

if (!resourceUri) return null;

return (
<EntrystoreProvider
env={env}
rUri={resourceUri}
entrystoreUrl={env.ENTRYSCAPE_TERMS_PATH}
pageType="concept"
>
<ConceptPage />
</EntrystoreProvider>
);
}
64 changes: 49 additions & 15 deletions pages/concepts/[concept]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useRouter } from "next/router";
import { GetServerSidePropsContext } from "next/types";
import { useContext } from "react";
import { useContext, useEffect, useState } from "react";

import { ConceptPage } from "@/features/entryscape/concept-page";
import { EntrystoreProvider } from "@/providers/entrystore-provider";
Expand All @@ -9,8 +8,54 @@ import { handleEntryStoreRedirect } from "@/utilities/entrystore/entrystore-redi

export default function Concept() {
const { env } = useContext(SettingsContext);
const { query } = useRouter() || {};
const { concept } = query || {};
const router = useRouter();
const { concept } = router.query || {};
const [resourceUri, setResourceUri] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(true);

useEffect(() => {
const fetchEntryStoreProps = async () => {
if (!concept) return;
const isSandbox = window.location.host.includes("sandbox");

const data = await handleEntryStoreRedirect(
{
pathPrefix: "/concepts",
redirectPath: "/concepts",
entrystorePathKey: "ENTRYSCAPE_TERMS_PATH",
param: concept,
},
router,
router.locale || "sv",
isSandbox,
);

if (data?.resourceUri) {
setResourceUri(data.resourceUri);
}
setIsLoading(false);
};

fetchEntryStoreProps();
}, [concept]);

if (isLoading) {
return null;
}

if (resourceUri) {
return (
<EntrystoreProvider
env={env}
rUri={resourceUri}
entrystoreUrl={env.ENTRYSCAPE_TERMS_PATH}
pageType="concept"
>
<ConceptPage />
</EntrystoreProvider>
);
}

const ids = (typeof concept === "string" && concept.split("_")) || [];
const eid = ids.pop() || "";
const cid = ids.join("_");
Expand All @@ -27,14 +72,3 @@ export default function Concept() {
</EntrystoreProvider>
);
}

export const getServerSideProps = async (
context: GetServerSidePropsContext,
) => {
return handleEntryStoreRedirect(context, {
pathPrefix: "/concepts",
redirectPath: "/concepts",
entrystorePathKey: "ENTRYSCAPE_TERMS_PATH",
paramName: "concept",
});
};
Loading

0 comments on commit 2b31cbf

Please sign in to comment.