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;
|
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 {
|
export interface UsePlanWorkArgs {
|
||||||
initialDate?: Date;
|
initialDate?: Date;
|
||||||
canEdit: boolean;
|
canEdit: boolean;
|
||||||
@@ -72,7 +94,7 @@ export function usePlanWork({ initialDate, canEdit }: UsePlanWorkArgs) {
|
|||||||
// --- Mutations ---
|
// --- Mutations ---
|
||||||
const createEntry = useMutation({
|
const createEntry = useMutation({
|
||||||
mutationFn: (body: any) =>
|
mutationFn: (body: any) =>
|
||||||
apiFetch("/api/admin/plan/entries", {
|
apiCall("/api/admin/plan/entries", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
@@ -90,7 +112,7 @@ export function usePlanWork({ initialDate, canEdit }: UsePlanWorkArgs) {
|
|||||||
body: any;
|
body: any;
|
||||||
force?: boolean;
|
force?: boolean;
|
||||||
}) =>
|
}) =>
|
||||||
apiFetch(`/api/admin/plan/entries/${id}${force ? "?force=1" : ""}`, {
|
apiCall(`/api/admin/plan/entries/${id}${force ? "?force=1" : ""}`, {
|
||||||
method: "PATCH",
|
method: "PATCH",
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
@@ -100,7 +122,7 @@ export function usePlanWork({ initialDate, canEdit }: UsePlanWorkArgs) {
|
|||||||
|
|
||||||
const deleteEntry = useMutation({
|
const deleteEntry = useMutation({
|
||||||
mutationFn: ({ id, force }: { id: number; force?: boolean }) =>
|
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",
|
method: "DELETE",
|
||||||
}),
|
}),
|
||||||
onSuccess: invalidate,
|
onSuccess: invalidate,
|
||||||
@@ -108,7 +130,7 @@ export function usePlanWork({ initialDate, canEdit }: UsePlanWorkArgs) {
|
|||||||
|
|
||||||
const createOverride = useMutation({
|
const createOverride = useMutation({
|
||||||
mutationFn: (body: any) =>
|
mutationFn: (body: any) =>
|
||||||
apiFetch("/api/admin/plan/overrides", {
|
apiCall("/api/admin/plan/overrides", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
@@ -126,7 +148,7 @@ export function usePlanWork({ initialDate, canEdit }: UsePlanWorkArgs) {
|
|||||||
body: any;
|
body: any;
|
||||||
force?: boolean;
|
force?: boolean;
|
||||||
}) =>
|
}) =>
|
||||||
apiFetch(`/api/admin/plan/overrides/${id}${force ? "?force=1" : ""}`, {
|
apiCall(`/api/admin/plan/overrides/${id}${force ? "?force=1" : ""}`, {
|
||||||
method: "PATCH",
|
method: "PATCH",
|
||||||
body: JSON.stringify(body),
|
body: JSON.stringify(body),
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
@@ -136,7 +158,7 @@ export function usePlanWork({ initialDate, canEdit }: UsePlanWorkArgs) {
|
|||||||
|
|
||||||
const deleteOverride = useMutation({
|
const deleteOverride = useMutation({
|
||||||
mutationFn: ({ id, force }: { id: number; force?: boolean }) =>
|
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",
|
method: "DELETE",
|
||||||
}),
|
}),
|
||||||
onSuccess: invalidate,
|
onSuccess: invalidate,
|
||||||
|
|||||||
Reference in New Issue
Block a user