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

fix(contentful-apps): Handle org subpage and parent subpage preview link routing #17369

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions apps/contentful-apps/pages/sidebars/preview-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EntryProps, KeyValueMap } from 'contentful-management'
import { CMAClient, SidebarExtensionSDK } from '@contentful/app-sdk'
import { Button, Text } from '@contentful/f36-components'
import { useCMA, useSDK } from '@contentful/react-apps-toolkit'

import {
CONTENTFUL_ENVIRONMENT,
CONTENTFUL_SPACE,
Expand Down Expand Up @@ -40,12 +41,30 @@ const previewLinkHandler = {
) => {
const organizationPageId =
entry.fields.organizationPage?.[DEFAULT_LOCALE]?.sys?.id
const organizationPage = await cma.entry.get({
entryId: organizationPageId,
environmentId: CONTENTFUL_ENVIRONMENT,
spaceId: CONTENTFUL_SPACE,
})
return `${DEV_WEB_BASE_URL}/s/${organizationPage?.fields?.slug?.[DEFAULT_LOCALE]}/${entry.fields.slug[DEFAULT_LOCALE]}`
const [organizationPage, organizationParentSubpageResponse] =
await Promise.all([
cma.entry.get({
entryId: organizationPageId,
environmentId: CONTENTFUL_ENVIRONMENT,
spaceId: CONTENTFUL_SPACE,
}),
cma.entry.getMany({
query: {
content_type: 'organizationParentSubpage',
include: 1,
links_to_entry: entry.sys.id,
'sys.archivedAt[exists]': false,
},
}),
])

const orgPageSlug = organizationPage?.fields?.slug?.[DEFAULT_LOCALE]
const slug = entry.fields.slug[DEFAULT_LOCALE]

if (!organizationParentSubpageResponse?.items?.length) {
return `${DEV_WEB_BASE_URL}/s/${orgPageSlug}/${slug}`
}
return `${DEV_WEB_BASE_URL}/s/${orgPageSlug}/${organizationParentSubpageResponse.items[0].fields.slug[DEFAULT_LOCALE]}/${slug}`
RunarVestmann marked this conversation as resolved.
Show resolved Hide resolved
},
anchorPage: (entry: EntryProps<KeyValueMap>) => {
const middlePart =
Expand All @@ -64,6 +83,21 @@ const previewLinkHandler = {
manual: (entry: EntryProps<KeyValueMap>) => {
return `${DEV_WEB_BASE_URL}/handbaekur/${entry.fields.slug[DEFAULT_LOCALE]}`
},
organizationParentSubpage: async (
entry: EntryProps<KeyValueMap>,
cma: CMAClient,
) => {
const organizationPageId =
entry.fields.organizationPage?.[DEFAULT_LOCALE]?.sys?.id
const organizationPage = await cma.entry.get({
entryId: organizationPageId,
environmentId: CONTENTFUL_ENVIRONMENT,
spaceId: CONTENTFUL_SPACE,
})
const orgPageSlug = organizationPage?.fields?.slug?.[DEFAULT_LOCALE]

return `${DEV_WEB_BASE_URL}/s/${orgPageSlug}/${entry.fields.slug[DEFAULT_LOCALE]}`
},
}

const PreviewLinkSidebar = () => {
Expand Down
Loading