1+ import { getJobAdvertBySlug } from "@/sanity/queries/jobAdvert"
2+ import { bigIconSize , DateIcon } from "@/components/icons/icon"
3+ import { notFound } from "next/navigation"
4+ import SingleInfoCard from "@/components/cards/singleInfoCard"
5+ import { type Metadata } from "next"
6+ import { Date } from "@/components/date"
7+ import { toPlainText } from "@portabletext/react"
8+
9+ interface Params {
10+ slug : string
11+ }
12+
13+ export const revalidate = 30 // 30 sek
14+
15+ /**
16+ * Side for en enkelt stillingsannose. Siden er dynamisk basert på stillingens slug variabel.
17+ * Dersom slug ikke finnes, returneres en 404 side.
18+ * @param params Parametre fra URL
19+ */
20+ const JobAdvertPage : AsyncPage < Params > = async ( { params } ) => {
21+ const ad = await getJobAdvertBySlug ( params . slug )
22+
23+ if ( ! ad ) return notFound ( )
24+
25+ return (
26+ < SingleInfoCard
27+ title = { ad . title }
28+ descriptionBlock = { ad . description_block }
29+ image = { ad . image }
30+ maxParticipants = {
31+ ad . number_of_positions
32+ ? `Antall stillinger er ${ ad . number_of_positions } `
33+ : undefined
34+ }
35+ buttonText = { "Søk" }
36+ buttonUrl = { ad . link } >
37+ < Deadline deadline = { ad . deadline } > Søknadsfrist:</ Deadline >
38+ </ SingleInfoCard >
39+ )
40+ }
41+
42+ export default JobAdvertPage
43+
44+ const Deadline : Component < { deadline ?: string } & ChildProps > = ( { deadline, children } ) => {
45+ if ( ! deadline ) {
46+ return (
47+ < DateIcon title = { `Søknadsfrist` } width = { bigIconSize } >
48+ Opptak skjer fortløpende
49+ </ DateIcon >
50+ )
51+ }
52+ return (
53+ < DateIcon title = { `Søknadsfrist ${ deadline } ` } width = { bigIconSize } >
54+ { children } < Date date = { deadline } />
55+ </ DateIcon >
56+ )
57+ }
58+
59+ /**
60+ * Genererer metadata for en enkelt stillingsannonse.
61+ * Kjøres ved bygging av nettsiden.
62+ * @param props Props som sendes til siden
63+ * @returns Metadata for siden
64+ * @see https://nextjs.org/docs/app/building-your-application/optimizing/metadata#dynamic-metadata
65+ */
66+ export async function generateMetadata ( props : PageProps < Params > ) : Promise < Metadata > {
67+ const ad = await getJobAdvertBySlug ( props . params . slug )
68+
69+ if ( ! ad ) return notFound ( )
70+
71+ return {
72+ title : `${ ad . title } | Root Linjeforening` ,
73+ description : ad . description_block ? toPlainText ( ad . description_block ) : "Stillingsannonse" ,
74+ }
75+ }
0 commit comments