From 9ba29ab347b98afa8f90b6e28c8443a283fc9c3e Mon Sep 17 00:00:00 2001
From: Alex van Andel
Date: Sat, 29 May 2021 21:50:54 +0000
Subject: [PATCH] Fixes #225 by correctly applying the timezone whilst keeping
the original date in UTC
---
pages/success.tsx | 29 +++++++++++++++++++----------
1 file changed, 19 insertions(+), 10 deletions(-)
diff --git a/pages/success.tsx b/pages/success.tsx
index c60f2a1c..c36ab173 100644
--- a/pages/success.tsx
+++ b/pages/success.tsx
@@ -1,32 +1,41 @@
import Head from 'next/head';
import Link from 'next/link';
import prisma from '../lib/prisma';
+import {useEffect, useState} from "react";
import { useRouter } from 'next/router';
import { CheckIcon } from '@heroicons/react/outline';
import { ClockIcon, CalendarIcon, LocationMarkerIcon } from '@heroicons/react/solid';
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
+import toArray from 'dayjs/plugin/toArray';
+import timezone from 'dayjs/plugin/timezone';
import { createEvent } from 'ics';
dayjs.extend(utc);
+dayjs.extend(toArray);
+dayjs.extend(timezone);
export default function Success(props) {
const router = useRouter();
- const { date, location } = router.query;
+ const { location } = router.query;
+
+ const [ is24h, setIs24h ] = useState(false);
+ const [ date, setDate ] = useState(dayjs.utc(router.query.date));
+
+ useEffect( () => {
+ setDate(date.tz(localStorage.getItem('timeOption.preferredTimeZone') || dayjs.tz.guess()));
+ setIs24h(!!localStorage.getItem('timeOption.is24hClock'));
+ }, []);
function eventLink(): string {
- const start = Array.prototype.concat(...date.split('T').map(
- (parts) => parts.split('-').length > 1 ? parts.split('-').map( (n) => parseInt(n, 10) ) : parts.split(':').map( (n) => parseInt(n, 10) )
- ));
-
let optional = {};
if (location) {
optional['location'] = location;
}
const event = createEvent({
- start,
+ start: date.utc().toArray().slice(0, 6),
startInputType: 'utc',
title: props.eventType.title + ' with ' + props.user.name,
description: props.eventType.description,
@@ -78,7 +87,7 @@ export default function Success(props) {
}
- {dayjs(date).format("hh:mma, dddd DD MMMM YYYY")}
+ {date.format((is24h ? 'H:mm' : 'h:mma') + ", dddd DD MMMM YYYY")}
@@ -86,17 +95,17 @@ export default function Success(props) {