Newer
Older
goodnight / src / frontend / index.ts
function setCurrentDate(inputField:HTMLInputElement): void
{
    const local = new Date();
    local.setMinutes(local.getMinutes() - local.getTimezoneOffset());
    inputField.valueAsDate = local;
}
function setCurrentTime(inputField:HTMLInputElement): void
{
    const local = new Date();
    local.setMinutes(local.getMinutes() - local.getTimezoneOffset());
    const timeString = local.toISOString().split("T")[1].slice(0,5);
    inputField.value = timeString;
}

function sendDateTimeToServer(dateTime:string, endpoint:string): Promise<Response>
{
    return fetch(endpoint, {
        body: JSON.stringify({dateTime}),
        headers: {
            "Content-Type": "application/json; charset=UTF-8",
        },
        method: "post",
    });
}

function onInstantLogClick(checkElement:HTMLElement, url:string)
{
    return () => {
        const local = new Date();
        // local.setMinutes(local.getMinutes() - local.getTimezoneOffset());
        const timeString = local.toISOString();
        sendDateTimeToServer(timeString, url)
            .then(res => {
                if(res.status === 201){
                    checkElement.style.visibility = "visible";
                }
            });
    };
}

document.addEventListener("DOMContentLoaded", function pageInit(event:Event){
    const inDate = document.getElementById("inDate") as HTMLInputElement;
    setCurrentDate(inDate);

    const inTime = document.getElementById("inTime") as HTMLInputElement;
    setCurrentTime(inTime);

    const btnGoToBedNow = document.getElementById("btnGoToBedNow") as HTMLInputElement;
    const spnGoToBedNowCheck = document.getElementById("spnGoToBedNowCheck");
    btnGoToBedNow.addEventListener("click", onInstantLogClick(spnGoToBedNowCheck, "/logbedtime"));

    const btnWakeUpNow = document.getElementById("btnWakeUpNow") as HTMLInputElement;
    const spnWakeUpNowCheck = document.getElementById("spnWakeUpNowCheck");
    btnWakeUpNow.addEventListener("click", onInstantLogClick(spnWakeUpNowCheck, "/logwakeuptime"));
});