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