1- import { defaultEventDuration , getEventBySlug } from "@/sanity/queries/event"
2- import { isFuture , toDateTuple } from "@/utils/dateUtils"
1+ import { getEventBySlug } from "@/sanity/queries/event"
2+ import { isFuture } from "@/utils/dateUtils"
33import { bigIconSize , DateIcon , TimeIcon } from "@/components/icons/icon"
4- import { notFound } from "next/navigation"
4+ import { notFound , redirect } from "next/navigation"
55import SingleInfoCard from "@/components/cards/singleInfoCard"
66import { type Metadata } from "next"
7- import { createEvent } from "ics"
8- import { getEventTypeLabel } from "@/sanity/lib/utils"
9- import type { RootEvent } from "@/sanity/types"
10- import IcsButton from "@/components/buttons/icsButton"
117import { Date , Time } from "@/components/date"
128import { toPlainText } from "@portabletext/react"
9+ import { createIcsEvent } from "@/utils/ics"
10+ import AddToCalendarDropdown from "@/components/dropdown/addToCalendarDropdown"
1311
1412interface Params {
1513 slug : string
@@ -23,6 +21,10 @@ export const revalidate = 30 // 30 sek
2321 * @param params Parametre fra URL
2422 */
2523const EventPage : AsyncPage < Params > = async ( { params } ) => {
24+ if ( params . slug . endsWith ( ".ics" ) ) {
25+ return redirect ( `/api/arrangement/ical/${ params . slug . replace ( ".ics" , "" ) } ` )
26+ }
27+
2628 const event = await getEventBySlug ( params . slug )
2729
2830 if ( ! event ) return notFound ( )
@@ -32,6 +34,7 @@ const EventPage: AsyncPage<Params> = async ({ params }) => {
3234 icsEvent = createIcsEvent ( event )
3335 }
3436
37+ // TODO støtte for å lagre event i kalender uten å subscribe. F.eks på samme måte som https://github.com/add2cal/add-to-calendar-button
3538 return (
3639 < SingleInfoCard
3740 title = { event . title }
@@ -47,10 +50,9 @@ const EventPage: AsyncPage<Params> = async ({ params }) => {
4750 < >
4851 < TimeAndDate startTime = { event . start_time } />
4952 { icsEvent && (
50- < IcsButton
51- filename = { event . title }
52- data = { icsEvent }
53- aria-label = { "Legg til arrangement i kalender" }
53+ < AddToCalendarDropdown
54+ eventUrl = { `${ process . env . NEXT_PUBLIC_BASE_URL } /api/arrangement/ical/${ params . slug } ` }
55+ restrict = { [ "copy" , "ics" ] }
5456 />
5557 ) }
5658 </ >
@@ -81,7 +83,7 @@ const TimeAndDate: Component<{ startTime: string }> = ({ startTime }) => (
8183export async function generateMetadata ( { params } : PageProps < Params > ) : Promise < Metadata > {
8284 const event = await getEventBySlug ( params . slug )
8385
84- if ( ! event ) return notFound ( )
86+ if ( ! event ) return { }
8587
8688 return {
8789 title : `${ event . title } | Root Linjeforening` ,
@@ -90,37 +92,3 @@ export async function generateMetadata({ params }: PageProps<Params>): Promise<M
9092 : "Arrangement arrangert av Root Linjeforening" ,
9193 }
9294}
93-
94- /**
95- * Lager en string på ics format basert på et arrangement.
96- * Dersom sluttidspunkt ikke er definert, settes varigheten til 2 timer.
97- * @param event Arrangementet som skal konverteres til ics format
98- * @returns En string på ics format
99- * @see https://www.npmjs.com/package/ics
100- */
101- function createIcsEvent ( event : RootEvent ) : string | undefined {
102- let icsEvent : string | undefined = undefined
103- const end = event . end_time
104- ? { end : toDateTuple ( event . end_time ) }
105- : { duration : { hours : defaultEventDuration } }
106- createEvent (
107- {
108- title : event . title ,
109- description : event . description_block ? toPlainText ( event . description_block ) : "" ,
110- location : event . address_text ,
111- start : toDateTuple ( event . start_time ) ,
112- ...end ,
113- url : `${ process . env . NEXT_PUBLIC_BASE_URL } /arrangement/${ event . slug . current } ` ,
114- organizer : { name : "Root Linjeforening" , email : process . env . NEXT_PUBLIC_EMAIL } ,
115- categories : [ "Root Linjeforening" , "Arrangement" , getEventTypeLabel ( event . type ) ] ,
116- } ,
117- ( error , value ) => {
118- if ( error ) {
119- console . error ( error )
120- } else {
121- icsEvent = value
122- }
123- } ,
124- )
125- return icsEvent
126- }
0 commit comments