Rewrite PointListModal with svelte5 runes. fixes reactivity

This commit is contained in:
ThePetrovich 2025-07-02 18:09:46 +08:00
parent 0f79cefdac
commit 1a89d49e8a
4 changed files with 136 additions and 60 deletions

View file

@ -18,16 +18,37 @@ export async function fetchAPI<T>(endpoint: string, options: RequestInit = {}):
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
let errorText = await response.json();
if (
errorText &&
typeof errorText === "object" &&
("detail" in errorText || "field_errors" in errorText || "non_field_errors" in errorText)
) {
// Handle structured error responses
if ("detail" in errorText) {
errorText = errorText.detail;
} else if ("field_errors" in errorText) {
errorText = Object.values(errorText.field_errors).join(", ");
} else if ("non_field_errors" in errorText) {
errorText = errorText.non_field_errors.join(", ");
}
} else {
errorText = `Unexpected error: ${response.statusText}`;
}
throw new Error(`${errorText}`);
}
if (response.status === 204) {
// No content response
return {} as T; // Return an empty object for 204 responses
}
return await response.json() as T;
return (await response.json()) as T;
} catch (error) {
console.error(`Error fetching ${url}:`, error);
throw error;
if (error instanceof Error) {
// If the error is an instance of Error, rethrow it
return Promise.reject(new Error(`${error.message}`));
}
return Promise.reject(new Error(`${error}`));
}
}
@ -61,4 +82,4 @@ export function deleteAPI<T>(endpoint: string): Promise<T> {
return fetchAPI<T>(endpoint, {
method: "DELETE",
});
}
}