frontend helper functions for timer

This commit is contained in:
Oliver Bryan
2026-01-09 22:25:03 +00:00
parent d6604d2843
commit d64470da06
6 changed files with 130 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
export * as issue from "@/lib/server/issue";
export * as organisation from "@/lib/server/organisation";
export * as project from "@/lib/server/project";
export * as timer from "@/lib/server/timer";
export * as user from "@/lib/server/user";
export type ServerQueryInput = {

View File

@@ -0,0 +1,31 @@
import { getCsrfToken, getServerURL } from "@/lib/utils";
import type { ServerQueryInput } from "..";
export async function end({
issueId,
onSuccess,
onError,
}: {
issueId: number;
} & ServerQueryInput) {
const url = new URL(`${getServerURL()}/timer/end`);
url.searchParams.set("issueId", `${issueId}`);
const csrfToken = getCsrfToken();
const headers: HeadersInit = {};
if (csrfToken) headers["X-CSRF-Token"] = csrfToken;
const res = await fetch(url.toString(), {
method: "POST",
headers,
credentials: "include",
});
if (!res.ok) {
const error = await res.text();
onError?.(error || `failed to end timer (${res.status})`);
} else {
const data = await res.json();
onSuccess?.(data, res);
}
}

View File

@@ -0,0 +1,30 @@
import { getCsrfToken, getServerURL } from "@/lib/utils";
import type { ServerQueryInput } from "..";
export async function get({
issueId,
onSuccess,
onError,
}: {
issueId: number;
} & ServerQueryInput) {
const url = new URL(`${getServerURL()}/timer/get`);
url.searchParams.set("issueId", `${issueId}`);
const csrfToken = getCsrfToken();
const headers: HeadersInit = {};
if (csrfToken) headers["X-CSRF-Token"] = csrfToken;
const res = await fetch(url.toString(), {
headers,
credentials: "include",
});
if (!res.ok) {
const error = await res.text();
onError?.(error || `failed to get timer (${res.status})`);
} else {
const data = await res.json();
onSuccess?.(data, res);
}
}

View File

@@ -0,0 +1,4 @@
export { end } from "@/lib/server/timer/end";
export { get } from "@/lib/server/timer/get";
export { list } from "@/lib/server/timer/list";
export { toggle } from "@/lib/server/timer/toggle";

View File

@@ -0,0 +1,33 @@
import { getCsrfToken, getServerURL } from "@/lib/utils";
import type { ServerQueryInput } from "..";
export async function list({
limit,
offset,
onSuccess,
onError,
}: {
limit?: number;
offset?: number;
} & ServerQueryInput) {
const url = new URL(`${getServerURL()}/timers`);
if (limit != null) url.searchParams.set("limit", `${limit}`);
if (offset != null) url.searchParams.set("offset", `${offset}`);
const csrfToken = getCsrfToken();
const headers: HeadersInit = {};
if (csrfToken) headers["X-CSRF-Token"] = csrfToken;
const res = await fetch(url.toString(), {
headers,
credentials: "include",
});
if (!res.ok) {
const error = await res.text();
onError?.(error || `failed to get timers (${res.status})`);
} else {
const data = await res.json();
onSuccess?.(data, res);
}
}

View File

@@ -0,0 +1,31 @@
import { getCsrfToken, getServerURL } from "@/lib/utils";
import type { ServerQueryInput } from "..";
export async function toggle({
issueId,
onSuccess,
onError,
}: {
issueId: number;
} & ServerQueryInput) {
const url = new URL(`${getServerURL()}/timer/toggle`);
url.searchParams.set("issueId", `${issueId}`);
const csrfToken = getCsrfToken();
const headers: HeadersInit = {};
if (csrfToken) headers["X-CSRF-Token"] = csrfToken;
const res = await fetch(url.toString(), {
method: "POST",
headers,
credentials: "include",
});
if (!res.ok) {
const error = await res.text();
onError?.(error || `failed to toggle timer (${res.status})`);
} else {
const data = await res.json();
onSuccess?.(data, res);
}
}