From 9e4f41ee462db1bdc044cd16db8f2c5fa34988ee Mon Sep 17 00:00:00 2001 From: Christel Westerberg Date: Thu, 16 May 2024 16:57:22 +0200 Subject: [PATCH] fix: redirect users to /refresh on unauth and mod webview links --- .../{[uid] => }/layout.module.css | 0 .../[contentType]/{[uid] => }/layout.tsx | 0 .../[contentType]/{[uid] => }/page.tsx | 0 app/[lang]/webview/loyalty-page/page.tsx | 20 ++---- app/[lang]/webview/my-pages/page.tsx | 45 +------------ app/[lang]/webview/refresh/page.tsx | 3 + components/Loyalty/Blocks/WebView/index.tsx | 66 +++++++++++++++++++ .../MyPages/AccountPage/Webview/Content.tsx | 11 +++- components/MyPages/Breadcrumbs/index.tsx | 14 +++- .../Header/Hamburger/hamburger.module.css | 16 +++++ components/MyPages/Header/Hamburger/index.tsx | 11 ++++ .../MyPages/Header/LanguageSwitcher/index.tsx | 17 +++++ .../LanguageSwitcher/language.module.css | 15 +++++ components/MyPages/Header/Logo/index.tsx | 34 ++++++++++ .../MyPages/Header/Logo/logo.module.css | 4 ++ components/MyPages/Header/header.module.css | 25 +++++++ components/MyPages/Header/index.tsx | 19 ++++++ constants/routes/webviews.ts | 13 ++++ lib/graphql/Query/Logo.graphql | 16 +++++ lib/trpc/server.ts | 11 ++++ middlewares/cmsContent.ts | 3 +- middlewares/webView.ts | 59 ++++++++++------- server/context.ts | 21 ++---- .../routers/contentstack/breadcrumbs/input.ts | 8 +++ .../routers/contentstack/breadcrumbs/query.ts | 2 +- server/trpc.ts | 5 +- types/params.ts | 2 +- types/requests/myPages/logo.ts | 10 +++ utils/webviews.ts | 13 ++++ 29 files changed, 358 insertions(+), 105 deletions(-) rename app/[lang]/(live)/(public)/[contentType]/{[uid] => }/layout.module.css (100%) rename app/[lang]/(live)/(public)/[contentType]/{[uid] => }/layout.tsx (100%) rename app/[lang]/(live)/(public)/[contentType]/{[uid] => }/page.tsx (100%) create mode 100644 app/[lang]/webview/refresh/page.tsx create mode 100644 components/Loyalty/Blocks/WebView/index.tsx create mode 100644 components/MyPages/Header/Hamburger/hamburger.module.css create mode 100644 components/MyPages/Header/Hamburger/index.tsx create mode 100644 components/MyPages/Header/LanguageSwitcher/index.tsx create mode 100644 components/MyPages/Header/LanguageSwitcher/language.module.css create mode 100644 components/MyPages/Header/Logo/index.tsx create mode 100644 components/MyPages/Header/Logo/logo.module.css create mode 100644 components/MyPages/Header/header.module.css create mode 100644 components/MyPages/Header/index.tsx create mode 100644 lib/graphql/Query/Logo.graphql create mode 100644 server/routers/contentstack/breadcrumbs/input.ts create mode 100644 types/requests/myPages/logo.ts create mode 100644 utils/webviews.ts diff --git a/app/[lang]/(live)/(public)/[contentType]/[uid]/layout.module.css b/app/[lang]/(live)/(public)/[contentType]/layout.module.css similarity index 100% rename from app/[lang]/(live)/(public)/[contentType]/[uid]/layout.module.css rename to app/[lang]/(live)/(public)/[contentType]/layout.module.css diff --git a/app/[lang]/(live)/(public)/[contentType]/[uid]/layout.tsx b/app/[lang]/(live)/(public)/[contentType]/layout.tsx similarity index 100% rename from app/[lang]/(live)/(public)/[contentType]/[uid]/layout.tsx rename to app/[lang]/(live)/(public)/[contentType]/layout.tsx diff --git a/app/[lang]/(live)/(public)/[contentType]/[uid]/page.tsx b/app/[lang]/(live)/(public)/[contentType]/page.tsx similarity index 100% rename from app/[lang]/(live)/(public)/[contentType]/[uid]/page.tsx rename to app/[lang]/(live)/(public)/[contentType]/page.tsx diff --git a/app/[lang]/webview/loyalty-page/page.tsx b/app/[lang]/webview/loyalty-page/page.tsx index b173ff4c2..8d63f02ad 100644 --- a/app/[lang]/webview/loyalty-page/page.tsx +++ b/app/[lang]/webview/loyalty-page/page.tsx @@ -1,14 +1,14 @@ -import { notFound, redirect } from "next/navigation" +import { notFound } from "next/navigation" import { serverClient } from "@/lib/trpc/server" -import { Blocks } from "@/components/Loyalty/Blocks" +import { Blocks } from "@/components/Loyalty/Blocks/WebView" import Sidebar from "@/components/Loyalty/Sidebar" import MaxWidth from "@/components/MaxWidth" import styles from "./page.module.css" -import type { LangParams, PageArgs, UriParams } from "@/types/params" +import { LangParams, PageArgs, UriParams } from "@/types/params" export default async function AboutScandicFriends({ params, @@ -18,26 +18,16 @@ export default async function AboutScandicFriends({ return notFound() } - const loyaltyPage = await serverClient({ - onError() { - const returnUrl = new URLSearchParams({ - returnurl: `${params.lang}/webview/${searchParams.uri}`, - }) - - const refreshUrl = `/${params.lang}/webview/refresh?${returnUrl.toString()}` - redirect(refreshUrl) - }, - }).contentstack.loyaltyPage.get({ + const loyaltyPage = await serverClient().contentstack.loyaltyPage.get({ href: searchParams.uri, locale: params.lang, }) - return (
{loyaltyPage.sidebar ? : null} - +
) diff --git a/app/[lang]/webview/my-pages/page.tsx b/app/[lang]/webview/my-pages/page.tsx index 7374147df..e66326643 100644 --- a/app/[lang]/webview/my-pages/page.tsx +++ b/app/[lang]/webview/my-pages/page.tsx @@ -1,10 +1,8 @@ import "@/app/globals.css" import "@scandic-hotels/design-system/style.css" -import { notFound, redirect } from "next/navigation" +import { notFound } from "next/navigation" -import { Lang } from "@/constants/languages" -import { overview } from "@/constants/routes/webviews" import { _ } from "@/lib/translation" import { serverClient } from "@/lib/trpc/server" @@ -13,21 +11,7 @@ import Content from "@/components/MyPages/AccountPage/Webview/Content" import styles from "./page.module.css" -import type { LangParams, PageArgs, UriParams } from "@/types/params" - -function getLink(lang: Lang, uri: string) { - if (uri === overview[lang]) { - return { - title: _("Go to points"), - href: `/${lang}/webview/my-pages/points`, - } - } else { - return { - title: _("Go to membership overview"), - href: `/${lang}/webview/my-pages/overview`, - } - } -} +import { LangParams, PageArgs, UriParams } from "@/types/params" export default async function MyPages({ params, @@ -37,34 +21,11 @@ export default async function MyPages({ return notFound() } - // Check if the access token is valid. If not, redirect to the refresh page. - await serverClient({ - onError(opts) { - const returnUrl = new URLSearchParams({ - returnurl: `${params.lang}/webview/${searchParams.uri}`, - }) - - const refreshUrl = `/${params.lang}/webview/refresh?${returnUrl.toString()}` - redirect(refreshUrl) - }, - }).user.get() - - const accountPage = await serverClient({ - onError() { - const returnUrl = new URLSearchParams({ - returnurl: `${params.lang}/webview/${searchParams.uri}`, - }) - - const refreshUrl = `/${params.lang}/webview/refresh?${returnUrl.toString()}` - redirect(refreshUrl) - }, - }).contentstack.accountPage.get({ + const accountPage = await serverClient().contentstack.accountPage.get({ url: searchParams.uri, lang: params.lang, }) - const link = getLink(params.lang, searchParams.uri) - return ( diff --git a/app/[lang]/webview/refresh/page.tsx b/app/[lang]/webview/refresh/page.tsx new file mode 100644 index 000000000..1fe6ee753 --- /dev/null +++ b/app/[lang]/webview/refresh/page.tsx @@ -0,0 +1,3 @@ +export default function Refresh() { + return
Hey you've been refreshed
+} diff --git a/components/Loyalty/Blocks/WebView/index.tsx b/components/Loyalty/Blocks/WebView/index.tsx new file mode 100644 index 000000000..f5b5fea0f --- /dev/null +++ b/components/Loyalty/Blocks/WebView/index.tsx @@ -0,0 +1,66 @@ +import JsonToHtml from "@/components/JsonToHtml" +import DynamicContentBlock from "@/components/Loyalty/Blocks/DynamicContent" +import Shortcuts from "@/components/MyPages/Blocks/Shortcuts" +import { modWebviewLink } from "@/utils/webviews" + +import CardGrid from "../CardGrid" + +import type { BlocksProps } from "@/types/components/loyalty/blocks" +import { LoyaltyBlocksTypenameEnum } from "@/types/components/loyalty/enums" +import { LangParams } from "@/types/params" + +export function Blocks({ lang, blocks }: BlocksProps & LangParams) { + return blocks.map((block) => { + switch (block.__typename) { + case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksCardGrid: + const cardGrid = { + ...block.card_grid, + cards: block.card_grid.cards.map((card) => { + return { + ...card, + link: card.link + ? { ...card.link, href: modWebviewLink(card.link.href, lang) } + : undefined, + } + }), + } + + return + case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent: + return ( +
+ +
+ ) + case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksDynamicContent: + const dynamicContent = { + ...block.dynamic_content, + linK: block.dynamic_content.link + ? { + ...block.dynamic_content.link, + href: modWebviewLink(block.dynamic_content.link.href, lang), + } + : undefined, + } + + return + case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksShortcuts: + const shortcuts = block.shortcuts.shortcuts.map((shortcut) => ({ + ...shortcut, + url: modWebviewLink(shortcut.url, lang), + })) + return ( + + ) + default: + return null + } + }) +} diff --git a/components/MyPages/AccountPage/Webview/Content.tsx b/components/MyPages/AccountPage/Webview/Content.tsx index 97fd548d9..29cee02f2 100644 --- a/components/MyPages/AccountPage/Webview/Content.tsx +++ b/components/MyPages/AccountPage/Webview/Content.tsx @@ -1,6 +1,7 @@ import JsonToHtml from "@/components/JsonToHtml" import Overview from "@/components/MyPages/Blocks/Overview" import Shortcuts from "@/components/MyPages/Blocks/Shortcuts" +import { modWebviewLink } from "@/utils/webviews" import { AccountPageContentProps, @@ -31,7 +32,7 @@ export default function Content({ lang, content }: ContentProps) { href: item.dynamic_content.link.linkConnection.edges[0].node .original_url || - `/${lang}${item.dynamic_content.link.linkConnection.edges[0].node.url}`, + `/${lang}/webview${item.dynamic_content.link.linkConnection.edges[0].node.url}`, text: item.dynamic_content.link.link_text, } : null @@ -50,9 +51,15 @@ export default function Content({ lang, content }: ContentProps) { /> ) case ContentEntries.AccountPageContentShortcuts: + const shortcuts = item.shortcuts.shortcuts.map((shortcut) => { + return { + ...shortcut, + url: modWebviewLink(shortcut.url, lang), + } + }) return ( diff --git a/components/MyPages/Breadcrumbs/index.tsx b/components/MyPages/Breadcrumbs/index.tsx index f0fa8d566..7ae70c5f4 100644 --- a/components/MyPages/Breadcrumbs/index.tsx +++ b/components/MyPages/Breadcrumbs/index.tsx @@ -1,3 +1,4 @@ +import { Lang } from "@/constants/languages" import { _ } from "@/lib/translation" import { serverClient } from "@/lib/trpc/server" @@ -6,8 +7,17 @@ import BreadcrumbsWithLink from "./BreadcrumbWithLink" import styles from "./breadcrumbs.module.css" -export default async function Breadcrumbs() { - const breadcrumbs = await serverClient().contentstack.breadcrumbs.get() +export default async function Breadcrumbs({ + href, + locale, +}: { + href: string + locale: Lang +}) { + const breadcrumbs = await serverClient().contentstack.breadcrumbs.get({ + href, + locale, + }) return (