-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add project subpage layout to generic list item page
- Loading branch information
1 parent
f389ae0
commit 22e304d
Showing
6 changed files
with
147 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
apps/web/pages/en/p/[slug]/[subSlug]/[genericListItemSlug]/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
apps/web/pages/v/[slug]/[subSlug]/[genericListItemSlug]/index.ts
This file was deleted.
Oops, something went wrong.
124 changes: 124 additions & 0 deletions
124
apps/web/pages/v/[slug]/[subSlug]/[genericListItemSlug]/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { useMemo } from 'react' | ||
import { useRouter } from 'next/router' | ||
|
||
import { ProjectPage } from '@island.is/web/graphql/schema' | ||
import withApollo from '@island.is/web/graphql/withApollo' | ||
import { useLinkResolver } from '@island.is/web/hooks' | ||
import { useI18n, withLocale } from '@island.is/web/i18n' | ||
import { LayoutProps } from '@island.is/web/layouts/main' | ||
import type { GenericListItemPageProps } from '@island.is/web/screens/GenericList/GenericListItem' | ||
import GenericListItemPage from '@island.is/web/screens/GenericList/GenericListItem' | ||
import SubPageLayout, { | ||
type PageProps, | ||
} from '@island.is/web/screens/Project/Project' | ||
import type { Screen as ScreenType } from '@island.is/web/types' | ||
import { CustomNextError } from '@island.is/web/units/errors' | ||
import { getServerSidePropsWrapper } from '@island.is/web/utils/getServerSidePropsWrapper' | ||
|
||
interface ComponentProps { | ||
parentProps: { | ||
layoutProps: LayoutProps | ||
componentProps: PageProps | ||
} | ||
genericListItemProps: GenericListItemPageProps | ||
} | ||
|
||
export const Component: ScreenType<ComponentProps> = ({ | ||
parentProps, | ||
genericListItemProps, | ||
}) => { | ||
const { activeLocale } = useI18n() | ||
const router = useRouter() | ||
const { linkResolver } = useLinkResolver() | ||
const backLinkUrl = useMemo(() => { | ||
const pathname = new URL(router.asPath, 'https://island.is').pathname | ||
return pathname.slice(0, pathname.lastIndexOf('/')) | ||
}, [router.asPath]) | ||
|
||
const projectPage = parentProps.componentProps.projectPage as ProjectPage | ||
const subpage = projectPage.projectSubpages.find( | ||
(subpage) => subpage.slug === router.query.subSlug, | ||
) | ||
|
||
return ( | ||
<SubPageLayout | ||
layoutProps={parentProps.layoutProps} | ||
componentProps={{ | ||
...parentProps.componentProps, | ||
customContentfulIds: [ | ||
projectPage?.id, | ||
subpage?.id, | ||
genericListItemProps.item.id, | ||
], | ||
customBreadcrumbItems: [ | ||
{ | ||
title: 'Ísland.is', | ||
href: linkResolver('homepage').href, | ||
}, | ||
{ | ||
title: projectPage?.title ?? '', | ||
href: linkResolver('projectpage', [projectPage?.slug ?? '']).href, | ||
}, | ||
{ | ||
title: subpage?.title ?? '', | ||
href: backLinkUrl, | ||
isTag: true, | ||
}, | ||
], | ||
projectPage: { | ||
...projectPage, | ||
backLink: { | ||
date: '', | ||
id: '', | ||
text: activeLocale === 'is' ? 'Til baka' : 'Go back', | ||
url: backLinkUrl, | ||
}, | ||
projectSubpages: | ||
projectPage.projectSubpages.map((subpage) => { | ||
if (subpage.slug === router.query.subSlug) { | ||
return { | ||
...subpage, | ||
bottomSlices: [], | ||
slices: [], | ||
content: genericListItemProps.item.content, | ||
title: genericListItemProps.item.title, | ||
showTableOfContents: false, | ||
} | ||
} | ||
return subpage | ||
}) ?? [], | ||
}, | ||
}} | ||
/> | ||
) | ||
} | ||
|
||
Component.getProps = async (ctx) => { | ||
const [parentProps, genericListItemProps] = await Promise.all([ | ||
SubPageLayout.getProps?.(ctx), | ||
GenericListItemPage.getProps?.(ctx), | ||
]) | ||
|
||
if (!parentProps) { | ||
throw new CustomNextError( | ||
404, | ||
'Could not fetch subpage layout for generic list item', | ||
) | ||
} | ||
if (!genericListItemProps) { | ||
throw new CustomNextError(404, 'Could not fetch generic list item props') | ||
} | ||
|
||
return { | ||
parentProps, | ||
genericListItemProps, | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore make web strict | ||
const Screen = withApollo(withLocale('is')(Component)) | ||
|
||
export default Screen | ||
|
||
export const getServerSideProps = getServerSidePropsWrapper(Screen) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters