feat(cache): add stale get caching for report filters and summaries
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { authFetch } from "./client";
|
||||
import { cachedGetJson, invalidateApiCache } from "./cache";
|
||||
|
||||
interface AuditUser {
|
||||
id: string;
|
||||
@@ -56,10 +57,10 @@ export const getProjects = async (
|
||||
if (params.is_archived !== undefined) queryParams.append("is_archived", params.is_archived.toString());
|
||||
if (params.ordering !== undefined) queryParams.append("ordering", params.ordering.toString());
|
||||
|
||||
const response = await authFetch(`/api/projects/?${queryParams.toString()}`);
|
||||
|
||||
if (!response.ok) throw new Error("Failed to fetch projects");
|
||||
const data = await response.json();
|
||||
const data = await cachedGetJson<any>(`/api/projects/?${queryParams.toString()}`, {
|
||||
ttlMs: 5 * 60 * 1000,
|
||||
namespaces: ["projects"],
|
||||
});
|
||||
if (Array.isArray(data)) return data;
|
||||
if (Array.isArray(data?.items)) {
|
||||
return {
|
||||
@@ -89,12 +90,14 @@ export const createProject = async (
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => null);
|
||||
throw new Error(errorData?.detail || errorData?.message || "Failed to create project");
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => null);
|
||||
throw new Error(errorData?.detail || errorData?.message || "Failed to create project");
|
||||
}
|
||||
const payload = await response.json();
|
||||
invalidateApiCache(["projects", "reports"]);
|
||||
return payload;
|
||||
};
|
||||
|
||||
export const updateProject = async (
|
||||
id: string,
|
||||
@@ -105,25 +108,28 @@ export const updateProject = async (
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => null);
|
||||
throw new Error(errorData?.detail || errorData?.message || "Failed to update project");
|
||||
}
|
||||
return response.json();
|
||||
};
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => null);
|
||||
throw new Error(errorData?.detail || errorData?.message || "Failed to update project");
|
||||
}
|
||||
const payload = await response.json();
|
||||
invalidateApiCache(["projects", "reports"]);
|
||||
return payload;
|
||||
};
|
||||
|
||||
export const deleteProject = async (id: string) => {
|
||||
const response = await authFetch(`/api/projects/${id}/`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => null);
|
||||
throw new Error(errorData?.detail || errorData?.message || "Failed to delete project");
|
||||
}
|
||||
|
||||
if (response.status === 204) return { success: true };
|
||||
return response.json().catch(() => ({ success: true }));
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => null);
|
||||
throw new Error(errorData?.detail || errorData?.message || "Failed to delete project");
|
||||
}
|
||||
invalidateApiCache(["projects", "reports"]);
|
||||
|
||||
if (response.status === 204) return { success: true };
|
||||
return response.json().catch(() => ({ success: true }));
|
||||
};
|
||||
|
||||
export const toggleArchiveProject = async (id: string) => {
|
||||
@@ -131,9 +137,11 @@ export const toggleArchiveProject = async (id: string) => {
|
||||
method: "POST",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => null);
|
||||
throw new Error(errorData?.detail || errorData?.message || `Failed to archive project`);
|
||||
}
|
||||
return response.json();
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => null);
|
||||
throw new Error(errorData?.detail || errorData?.message || `Failed to archive project`);
|
||||
}
|
||||
const payload = await response.json();
|
||||
invalidateApiCache(["projects", "reports"]);
|
||||
return payload;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user