mirror of
https://github.com/hex248/sprint.git
synced 2026-02-08 02:33:01 +00:00
use ServerQueryInput properly
This commit is contained in:
@@ -1,29 +1,28 @@
|
||||
import type { TimerEndRequest, TimerState } from "@issue/shared";
|
||||
import { toast } from "sonner";
|
||||
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}`);
|
||||
|
||||
export async function end(request: TimerEndRequest & ServerQueryInput<TimerState>) {
|
||||
const { onSuccess, onError, ...body } = request;
|
||||
const csrfToken = getCsrfToken();
|
||||
const headers: HeadersInit = {};
|
||||
if (csrfToken) headers["X-CSRF-Token"] = csrfToken;
|
||||
|
||||
const res = await fetch(url.toString(), {
|
||||
const res = await fetch(`${getServerURL()}/timer/end`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...(csrfToken ? { "X-CSRF-Token": csrfToken } : {}),
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
credentials: "include",
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const error = await res.text();
|
||||
onError?.(error || `failed to end timer (${res.status})`);
|
||||
const error = await res.json().catch(() => res.text());
|
||||
const message =
|
||||
typeof error === "string" ? error : error.error || `failed to end timer (${res.status})`;
|
||||
toast.error(message);
|
||||
onError?.(error);
|
||||
} else {
|
||||
const data = await res.json();
|
||||
onSuccess?.(data, res);
|
||||
|
||||
Reference in New Issue
Block a user