From 127464fec9afe67651b95252c441fedd967d0054 Mon Sep 17 00:00:00 2001 From: Mikael Siidorow Date: Wed, 21 Aug 2024 11:12:07 +0300 Subject: [PATCH] feat: ilmomasiina signup countdown [wip] --- .../events/[slug]/client-components.tsx | 100 ++++++++++++++++++ .../[locale]/(main)/events/[slug]/page.tsx | 47 ++++---- apps/web/src/locales/en.ts | 2 + apps/web/src/locales/fi.ts | 2 + 4 files changed, 133 insertions(+), 18 deletions(-) create mode 100644 apps/web/src/app/[locale]/(main)/events/[slug]/client-components.tsx diff --git a/apps/web/src/app/[locale]/(main)/events/[slug]/client-components.tsx b/apps/web/src/app/[locale]/(main)/events/[slug]/client-components.tsx new file mode 100644 index 00000000..92c51ee2 --- /dev/null +++ b/apps/web/src/app/[locale]/(main)/events/[slug]/client-components.tsx @@ -0,0 +1,100 @@ +"use client"; + +import { Button, type ButtonProps } from "@tietokilta/ui"; +import React, { useEffect, useState } from "react"; +import { + I18nProviderClient, + useCurrentLocale, + useScopedI18n, +} from "@/locales/client"; + +export function AutoEnableButton({ + startDate, + endDate, + ...props +}: React.PropsWithChildren< + Omit & { startDate: string; endDate: string } +>) { + const [isDisabled, setIsDisabled] = useState(false); + + useEffect(() => { + const tick = () => { + console.log("button tick", props.children); + const hasStarted = new Date(startDate) < new Date(); + const hasEnded = new Date(endDate) < new Date(); + setIsDisabled(!hasStarted || hasEnded); + }; + + if (new Date(startDate) < new Date()) { + setIsDisabled(false); + return; + } + + tick(); + const interval = setInterval(tick, 500); + + return () => { + clearInterval(interval); + }; + }, [startDate, endDate, props.children]); + + return