fix(plan): make mutations throw on non-2xx so errors surface as alerts
This commit is contained in:
@@ -30,6 +30,28 @@ function addDays(d: Date, n: number): Date {
|
||||
return r;
|
||||
}
|
||||
|
||||
async function apiCall(
|
||||
url: string,
|
||||
options: RequestInit = {},
|
||||
): Promise<unknown> {
|
||||
const res = await apiFetch(url, options);
|
||||
if (!res.ok) {
|
||||
let message = "Chyba serveru";
|
||||
try {
|
||||
const body = await res.json();
|
||||
if (body && typeof body.error === "string") message = body.error;
|
||||
} catch {
|
||||
// body wasn't JSON; use default
|
||||
}
|
||||
throw new Error(message);
|
||||
}
|
||||
try {
|
||||
return await res.json();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export interface UsePlanWorkArgs {
|
||||
initialDate?: Date;
|
||||
canEdit: boolean;
|
||||
@@ -72,7 +94,7 @@ export function usePlanWork({ initialDate, canEdit }: UsePlanWorkArgs) {
|
||||
// --- Mutations ---
|
||||
const createEntry = useMutation({
|
||||
mutationFn: (body: any) =>
|
||||
apiFetch("/api/admin/plan/entries", {
|
||||
apiCall("/api/admin/plan/entries", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
@@ -90,7 +112,7 @@ export function usePlanWork({ initialDate, canEdit }: UsePlanWorkArgs) {
|
||||
body: any;
|
||||
force?: boolean;
|
||||
}) =>
|
||||
apiFetch(`/api/admin/plan/entries/${id}${force ? "?force=1" : ""}`, {
|
||||
apiCall(`/api/admin/plan/entries/${id}${force ? "?force=1" : ""}`, {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify(body),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
@@ -100,7 +122,7 @@ export function usePlanWork({ initialDate, canEdit }: UsePlanWorkArgs) {
|
||||
|
||||
const deleteEntry = useMutation({
|
||||
mutationFn: ({ id, force }: { id: number; force?: boolean }) =>
|
||||
apiFetch(`/api/admin/plan/entries/${id}${force ? "?force=1" : ""}`, {
|
||||
apiCall(`/api/admin/plan/entries/${id}${force ? "?force=1" : ""}`, {
|
||||
method: "DELETE",
|
||||
}),
|
||||
onSuccess: invalidate,
|
||||
@@ -108,7 +130,7 @@ export function usePlanWork({ initialDate, canEdit }: UsePlanWorkArgs) {
|
||||
|
||||
const createOverride = useMutation({
|
||||
mutationFn: (body: any) =>
|
||||
apiFetch("/api/admin/plan/overrides", {
|
||||
apiCall("/api/admin/plan/overrides", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
@@ -126,7 +148,7 @@ export function usePlanWork({ initialDate, canEdit }: UsePlanWorkArgs) {
|
||||
body: any;
|
||||
force?: boolean;
|
||||
}) =>
|
||||
apiFetch(`/api/admin/plan/overrides/${id}${force ? "?force=1" : ""}`, {
|
||||
apiCall(`/api/admin/plan/overrides/${id}${force ? "?force=1" : ""}`, {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify(body),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
@@ -136,7 +158,7 @@ export function usePlanWork({ initialDate, canEdit }: UsePlanWorkArgs) {
|
||||
|
||||
const deleteOverride = useMutation({
|
||||
mutationFn: ({ id, force }: { id: number; force?: boolean }) =>
|
||||
apiFetch(`/api/admin/plan/overrides/${id}${force ? "?force=1" : ""}`, {
|
||||
apiCall(`/api/admin/plan/overrides/${id}${force ? "?force=1" : ""}`, {
|
||||
method: "DELETE",
|
||||
}),
|
||||
onSuccess: invalidate,
|
||||
|
||||
Reference in New Issue
Block a user