mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 02:33:01 +00:00
frontend helper functions for timer
This commit is contained in:
@@ -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 = {
|
||||
|
||||
31
packages/frontend/src/lib/server/timer/end.ts
Normal file
31
packages/frontend/src/lib/server/timer/end.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
30
packages/frontend/src/lib/server/timer/get.ts
Normal file
30
packages/frontend/src/lib/server/timer/get.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
4
packages/frontend/src/lib/server/timer/index.ts
Normal file
4
packages/frontend/src/lib/server/timer/index.ts
Normal 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";
|
||||
33
packages/frontend/src/lib/server/timer/list.ts
Normal file
33
packages/frontend/src/lib/server/timer/list.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
31
packages/frontend/src/lib/server/timer/toggle.ts
Normal file
31
packages/frontend/src/lib/server/timer/toggle.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user