Files
pawol.nu/pages/paroles/[slug].js
T

107 lines
3.1 KiB
JavaScript
Raw Normal View History

import PropTypes from 'prop-types'
2022-05-20 02:15:56 +04:00
import {jwennTeks, jwennKomanteEpiTeksId} from '../../lib/oki-api'
import TeksDrawer from '../../components/teks/teks-drawer'
2020-12-15 23:46:05 +01:00
import HeadLayout from '../../components/head-layout'
2022-05-14 03:37:04 +04:00
import Custom500 from '../500'
import Custom404 from '../404'
2022-05-20 02:15:56 +04:00
const jwennAwtis = artiste => {
const alias = artiste.data.map(({attributes}) => attributes.alias)
2022-02-05 14:21:56 +04:00
return new Intl.ListFormat('fr').format(alias)
}
2021-03-07 15:18:58 +01:00
2022-05-20 02:15:56 +04:00
export default function SlugTeks({hasError, errorMessage, paroles, parole, slug, commentaires}) {
2022-05-14 03:37:04 +04:00
if (hasError) {
console.log('⚠️ error :', errorMessage)
return <Custom500 />
}
2022-05-20 02:15:56 +04:00
if (!parole) {
2022-05-14 03:37:04 +04:00
return <Custom404 />
}
2022-05-20 02:15:56 +04:00
const artistes = parole.attributes.artistes.length === 1 ? parole.attributes.artistes[0].data.attributes.alias : jwennAwtis(parole.attributes.artistes)
const {couverture} = parole.attributes
2021-05-28 19:41:13 +02:00
const formatKouveti = () => {
2022-05-20 04:40:17 +04:00
if (!couverture?.data?.attributes) {
2021-05-28 19:41:13 +02:00
return null
}
2022-05-20 04:40:17 +04:00
if (couverture.data.attributes && couverture.data.attributes.formats && couverture.data.attributes.formats.large) {
2022-05-21 16:57:34 +04:00
return couverture.data.attributes.formats.large
2021-05-28 19:41:13 +02:00
}
2022-05-20 04:40:17 +04:00
if (couverture.data.attributes && couverture.data.attributes.formats && couverture.data.attributes.formats.medium) {
2022-05-21 16:57:34 +04:00
return couverture.data.attributes.formats.medium
2021-05-28 20:04:36 +02:00
}
2022-05-20 04:40:17 +04:00
if (couverture.data.attributes && couverture.data.attributes.formats && couverture.data.attributes.formats.small) {
2022-05-20 04:52:30 +04:00
return couverture.data.attributes.formats.small
2021-05-28 20:04:36 +02:00
}
2022-05-20 04:40:17 +04:00
return couverture.data.attributes
2021-05-28 19:41:13 +02:00
}
2021-03-07 15:18:58 +01:00
return (
2021-05-28 18:29:15 +02:00
<HeadLayout
2021-05-28 19:41:13 +02:00
imageUrl={formatKouveti() ? formatKouveti().url : null}
imageWidth={formatKouveti() ? formatKouveti().width : null}
imageHeight={formatKouveti() ? formatKouveti().height : null}
2021-05-28 20:15:26 +02:00
imageMime={formatKouveti() ? formatKouveti().mime : null}
2022-05-20 02:15:56 +04:00
title={`${artistes} - ${parole.attributes.titre} | Paroles et Traductions`} tab={1} slug={`paroles/${slug}`}
2021-05-28 18:29:15 +02:00
>
2022-05-20 02:15:56 +04:00
<TeksDrawer paroles={paroles} parole={parole.attributes} paroleId={parole.id} commentaires={commentaires} />
2020-12-15 23:46:05 +01:00
</HeadLayout>
)
}
export async function getServerSideProps({query}) {
const {slug} = query
2022-05-20 02:15:56 +04:00
let paroles
let parole
2022-05-14 03:37:04 +04:00
let hasError
let errorMessage
2022-05-20 02:15:56 +04:00
let commentaires
2022-05-14 03:37:04 +04:00
try {
2022-05-20 02:15:56 +04:00
paroles = await jwennTeks()
parole = paroles.find(({attributes}) => attributes.slug === slug)
commentaires = paroles.map(({attributes}) => attributes.commentaires)
commentaires = await jwennKomanteEpiTeksId(parole?.id)
2022-05-14 03:37:04 +04:00
} catch (error) {
errorMessage = error
hasError = true
}
return {
props: {
2022-05-14 03:37:04 +04:00
hasError: hasError || null,
errorMessage: errorMessage ? errorMessage.message : null,
2022-05-20 02:15:56 +04:00
paroles: paroles || null,
parole: parole || null,
2021-06-26 12:17:54 +02:00
slug,
2022-05-20 02:15:56 +04:00
commentaires: commentaires || null
}
}
}
2022-05-14 03:37:04 +04:00
SlugTeks.defaultProps = {
hasError: null,
errorMessage: null,
2022-05-20 02:15:56 +04:00
paroles: null,
parole: null,
commentaires: null
2022-05-14 03:37:04 +04:00
}
2020-12-18 22:08:34 +01:00
SlugTeks.propTypes = {
2022-05-14 03:37:04 +04:00
hasError: PropTypes.bool,
errorMessage: PropTypes.string,
2022-05-20 02:15:56 +04:00
paroles: PropTypes.array,
parole: PropTypes.object,
2021-06-26 12:17:54 +02:00
slug: PropTypes.string.isRequired,
2022-05-20 02:15:56 +04:00
commentaires: PropTypes.array
}