From e675a90ed6cdaa5158b8480604e5d53d89335072 Mon Sep 17 00:00:00 2001 From: DrMint Date: Mon, 14 Mar 2022 12:25:01 +0100 Subject: [PATCH] Fixed broken content pages if missing fields --- src/components/Content/ThumbnailHeader.tsx | 2 +- src/pages/contents/[slug]/index.tsx | 42 +++++--- src/pages/contents/[slug]/read.tsx | 110 +++++++++++---------- 3 files changed, 85 insertions(+), 69 deletions(-) diff --git a/src/components/Content/ThumbnailHeader.tsx b/src/components/Content/ThumbnailHeader.tsx index b9bfd44..59d11a5 100644 --- a/src/components/Content/ThumbnailHeader.tsx +++ b/src/components/Content/ThumbnailHeader.tsx @@ -65,7 +65,7 @@ export default function ThumbnailHeader(
- {content.type && ( + {content.type.data && (

{langui.type}

diff --git a/src/pages/contents/[slug]/index.tsx b/src/pages/contents/[slug]/index.tsx index e3799bb..241be91 100644 --- a/src/pages/contents/[slug]/index.tsx +++ b/src/pages/contents/[slug]/index.tsx @@ -65,6 +65,31 @@ export default function ContentIndex(props: ContentIndexProps): JSX.Element { ); + let description = ""; + if (content.type.data) { + description += `${langui.type}: `; + if (content.type.data.attributes.titles.length > 0) { + description += content.type.data.attributes.titles[0].title; + } else { + description += prettySlug(content.type.data.attributes.slug); + } + description += "\n"; + } + if (content.categories.data.length > 0) { + description += `${langui.categories}: `; + description += content.categories.data + .map((category) => { + return category.attributes.short; + }) + .join(" | "); + description += "\n"; + } + + if (content.titles.length > 0 && content.titles[0].description) { + description += "\n"; + description += content.titles[0].description; + } + return ( 0 - ? content.type.data.attributes.titles[0].title - : prettySlug(content.type.data.attributes.slug) - } - ${langui.categories}: ${ - content.categories.data.length > 0 && - content.categories.data - .map((category) => { - return category.attributes.short; - }) - .join(" | ") - } - - ${content.titles.length > 0 ? content.titles[0].description : undefined} - `} + description={description} {...props} /> ); diff --git a/src/pages/contents/[slug]/read.tsx b/src/pages/contents/[slug]/read.tsx index 774c85e..09ba673 100644 --- a/src/pages/contents/[slug]/read.tsx +++ b/src/pages/contents/[slug]/read.tsx @@ -49,7 +49,7 @@ export default function ContentRead(props: ContentReadProps): JSX.Element { horizontalLine /> - {content.text_set.length > 0 && ( + {content.text_set.length > 0 && content.text_set[0].source_language.data && (

{content.text_set[0].source_language.data.attributes.code === @@ -175,6 +175,26 @@ export default function ContentRead(props: ContentReadProps): JSX.Element { ); + let description = ""; + if (content.type.data) { + description += `${langui.type}: `; + if (content.type.data.attributes.titles.length > 0) { + description += content.type.data.attributes.titles[0].title; + } else { + description += prettySlug(content.type.data.attributes.slug); + } + description += "\n"; + } + if (content.categories.data.length > 0) { + description += `${langui.categories}: `; + description += content.categories.data + .map((category) => { + return category.attributes.short; + }) + .join(" | "); + description += "\n"; + } + return ( 0 - ? content.type.data.attributes.titles[0].title - : prettySlug(content.type.data.attributes.slug) - } - ${langui.categories}: ${ - content.categories.data.length > 0 && - content.categories.data - .map((category) => { - return category.attributes.short; - }) - .join(" | ") - } - - ${content.titles.length > 0 ? content.titles[0].description : undefined} - `} + description={description} {...props} /> ); @@ -308,42 +313,43 @@ export function useTesting(props: ContentReadProps) { ["content", "text_set"], contentURL ); - } - if (textset.source_language.data.attributes.code === router.locale) { - // This is a transcript - if (textset.transcribers.data.length === 0) { - prettyTestError( - router, - "Missing transcribers attribution", - ["content", "text_set"], - contentURL - ); - } - if (textset.translators.data.length > 0) { - prettyTestError( - router, - "Transcripts shouldn't have translators", - ["content", "text_set"], - contentURL - ); - } } else { - // This is a translation - if (textset.translators.data.length === 0) { - prettyTestError( - router, - "Missing translators attribution", - ["content", "text_set"], - contentURL - ); - } - if (textset.transcribers.data.length > 0) { - prettyTestError( - router, - "Translations shouldn't have transcribers", - ["content", "text_set"], - contentURL - ); + if (textset.source_language.data.attributes.code === router.locale) { + // This is a transcript + if (textset.transcribers.data.length === 0) { + prettyTestError( + router, + "Missing transcribers attribution", + ["content", "text_set"], + contentURL + ); + } + if (textset.translators.data.length > 0) { + prettyTestError( + router, + "Transcripts shouldn't have translators", + ["content", "text_set"], + contentURL + ); + } + } else { + // This is a translation + if (textset.translators.data.length === 0) { + prettyTestError( + router, + "Missing translators attribution", + ["content", "text_set"], + contentURL + ); + } + if (textset.transcribers.data.length > 0) { + prettyTestError( + router, + "Translations shouldn't have transcribers", + ["content", "text_set"], + contentURL + ); + } } } }