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

feat(web): Organization parent subpage #17022

Merged
merged 8 commits into from
Nov 26, 2024
Merged
Prev Previous commit
Next Next commit
Handle if there is only a single child link
  • Loading branch information
RunarVestmann committed Nov 26, 2024
commit cd67a469254e74db9c29a72661284aea53a37352
1 change: 0 additions & 1 deletion apps/web/pages/s/[...slugs]/index.tsx
Original file line number Diff line number Diff line change
@@ -166,7 +166,6 @@ Component.getProps = async (context) => {

const STANDALONE_THEME = 'standalone'

// Frontpage
if (slugs.length === 1) {
if (organizationPage.theme === STANDALONE_THEME) {
return {
94 changes: 38 additions & 56 deletions apps/web/screens/Organization/Standalone/ParentSubpage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useMemo } from 'react'
import { useRouter } from 'next/router'

import {
@@ -21,7 +20,7 @@ import {
QueryGetOrganizationParentSubpageArgs,
QueryGetOrganizationSubpageArgs,
} from '@island.is/web/graphql/schema'
import { useLinkResolver, useNamespace } from '@island.is/web/hooks'
import { useLinkResolver } from '@island.is/web/hooks'
import { useI18n } from '@island.is/web/i18n'
import { StandaloneLayout } from '@island.is/web/layouts/organization/standalone'
import type { Screen, ScreenContext } from '@island.is/web/types'
@@ -64,25 +63,16 @@ const StandaloneParentSubpage: Screen<
tableOfContentHeadings,
namespace,
}) => {
const organizationNamespace = useMemo(() => {
return JSON.parse(organizationPage.organization?.namespace?.fields || '{}')
}, [organizationPage.organization?.namespace?.fields])

const n = useNamespace(organizationNamespace)

const router = useRouter()
const { activeLocale } = useI18n()
const { linkResolver } = useLinkResolver()

return (
<StandaloneLayout
organizationPage={organizationPage}
bannerTitle={n('bannerTitle', '')}
>
<StandaloneLayout organizationPage={organizationPage}>
<GridContainer>
<GridRow>
<GridColumn span={['9/9', '9/9', '7/9']} offset={['0', '0', '1/9']}>
<Stack space={4}>
<Stack space={3}>
<Breadcrumbs
items={[
{
@@ -93,35 +83,41 @@ const StandaloneParentSubpage: Screen<
},
]}
/>
<Text variant="h1" as="h1">
{parentSubpage.title}
</Text>
<TableOfContents
headings={tableOfContentHeadings}
onClick={(headingId) => {
const href = tableOfContentHeadings.find(
(heading) => heading.headingId === headingId,
)?.href
if (href) {
router.push(href)
}
}}
tableOfContentsTitle={
namespace?.['standaloneTableOfContentsTitle'] ??
activeLocale === 'is'
? 'Efnisyfirlit'
: 'Table of contents'
}
selectedHeadingId={selectedHeadingId}
/>
{parentSubpage.childLinks.length > 1 && (
<Stack space={4}>
<Text variant="h1" as="h1">
{parentSubpage.title}
</Text>
<TableOfContents
headings={tableOfContentHeadings}
onClick={(headingId) => {
const href = tableOfContentHeadings.find(
(heading) => heading.headingId === headingId,
)?.href
if (href) {
router.push(href)
}
}}
tableOfContentsTitle={
namespace?.['standaloneTableOfContentsTitle'] ??
activeLocale === 'is'
? 'Efnisyfirlit'
: 'Table of contents'
}
selectedHeadingId={selectedHeadingId}
/>
</Stack>
)}
</Stack>
</GridColumn>
</GridRow>
<SubPageContent
namespace={namespace}
organizationPage={organizationPage}
subpage={subpage}
subpageTitleVariant="h2"
subpageTitleVariant={
parentSubpage.childLinks.length > 1 ? 'h2' : 'h1'
}
/>
</GridContainer>
</StandaloneLayout>
@@ -200,12 +196,17 @@ StandaloneParentSubpage.getProps = async ({
const index = getOrganizationParentSubpage.childLinks.findIndex(
(link) => link.href.split('/').pop() === subpageSlug,
)
if (index > 0) {
if (index >= 0) {
selectedIndex = index
} else {
throw new CustomNextError(
404,
'Subpage belonging to an organization parent subpage was not found',
)
}
}

let subpage = (
const subpage = (
await apolloClient.query<Query, QueryGetOrganizationSubpageArgs>({
query: GET_ORGANIZATION_SUBPAGE_QUERY,
variables: {
@@ -220,25 +221,6 @@ StandaloneParentSubpage.getProps = async ({
})
).data.getOrganizationSubpage

// Try getting the first subpage in case others can't be found
if (!subpage && selectedIndex > 0) {
selectedIndex = 0
subpage = (
await apolloClient.query<Query, QueryGetOrganizationSubpageArgs>({
query: GET_ORGANIZATION_SUBPAGE_QUERY,
variables: {
input: {
organizationSlug: organizationPageSlug,
slug: getOrganizationParentSubpage.childLinks[selectedIndex].href
.split('/')
.pop() as string,
lang: locale as ContentLanguage,
},
},
})
).data.getOrganizationSubpage
}

if (!subpage) {
throw new CustomNextError(
404,
5 changes: 0 additions & 5 deletions libs/cms/src/lib/models/organizationParentSubpage.model.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@ import { CacheField } from '@island.is/nest/graphql'
import { Field, ID, ObjectType } from '@nestjs/graphql'
import { IOrganizationParentSubpage } from '../generated/contentfulTypes'
import { getOrganizationPageUrlPrefix } from '@island.is/shared/utils'
import { Image, mapImage } from './image.model'

@ObjectType()
class OrganizationSubpageLink {
@@ -23,9 +22,6 @@ export class OrganizationParentSubpage {

@CacheField(() => [OrganizationSubpageLink])
childLinks!: OrganizationSubpageLink[]

@CacheField(() => Image, { nullable: true })
image?: Image | null
}

export const mapOrganizationParentSubpage = ({
@@ -42,6 +38,5 @@ export const mapOrganizationParentSubpage = ({
page.fields.organizationPage.fields.slug
}/${fields.slug}/${page.fields.slug}`,
})) ?? [],
image: fields.image ? mapImage(fields.image) : null,
}
}