fixed bug: Now you dont have to reload the page twice, after creating a loan
This commit is contained in:
16
frontend/package-lock.json
generated
16
frontend/package-lock.json
generated
@@ -9,7 +9,7 @@
|
|||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tailwindcss/vite": "^4.1.11",
|
"@tailwindcss/vite": "^4.1.11",
|
||||||
"@tanstack/react-query": "^5.85.0",
|
"@tanstack/react-query": "^5.85.5",
|
||||||
"js-cookie": "^3.0.5",
|
"js-cookie": "^3.0.5",
|
||||||
"lucide-react": "^0.539.0",
|
"lucide-react": "^0.539.0",
|
||||||
"primeicons": "^7.0.0",
|
"primeicons": "^7.0.0",
|
||||||
@@ -1836,9 +1836,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@tanstack/query-core": {
|
"node_modules/@tanstack/query-core": {
|
||||||
"version": "5.85.3",
|
"version": "5.85.5",
|
||||||
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.85.3.tgz",
|
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.85.5.tgz",
|
||||||
"integrity": "sha512-9Ne4USX83nHmRuEYs78LW+3lFEEO2hBDHu7mrdIgAFx5Zcrs7ker3n/i8p4kf6OgKExmaDN5oR0efRD7i2J0DQ==",
|
"integrity": "sha512-KO0WTob4JEApv69iYp1eGvfMSUkgw//IpMnq+//cORBzXf0smyRwPLrUvEe5qtAEGjwZTXrjxg+oJNP/C00t6w==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"funding": {
|
"funding": {
|
||||||
"type": "github",
|
"type": "github",
|
||||||
@@ -1846,12 +1846,12 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@tanstack/react-query": {
|
"node_modules/@tanstack/react-query": {
|
||||||
"version": "5.85.3",
|
"version": "5.85.5",
|
||||||
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.85.3.tgz",
|
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.85.5.tgz",
|
||||||
"integrity": "sha512-AqU8TvNh5GVIE8I+TUU0noryBRy7gOY0XhSayVXmOPll4UkZeLWKDwi0rtWOZbwLRCbyxorfJ5DIjDqE7GXpcQ==",
|
"integrity": "sha512-/X4EFNcnPiSs8wM2v+b6DqS5mmGeuJQvxBglmDxl6ZQb5V26ouD2SJYAcC3VjbNwqhY2zjxVD15rDA5nGbMn3A==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tanstack/query-core": "5.85.3"
|
"@tanstack/query-core": "5.85.5"
|
||||||
},
|
},
|
||||||
"funding": {
|
"funding": {
|
||||||
"type": "github",
|
"type": "github",
|
||||||
|
@@ -11,7 +11,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tailwindcss/vite": "^4.1.11",
|
"@tailwindcss/vite": "^4.1.11",
|
||||||
"@tanstack/react-query": "^5.85.0",
|
"@tanstack/react-query": "^5.85.5",
|
||||||
"js-cookie": "^3.0.5",
|
"js-cookie": "^3.0.5",
|
||||||
"lucide-react": "^0.539.0",
|
"lucide-react": "^0.539.0",
|
||||||
"primeicons": "^7.0.0",
|
"primeicons": "^7.0.0",
|
||||||
@@ -41,4 +41,4 @@
|
|||||||
"typescript-eslint": "^8.39.0",
|
"typescript-eslint": "^8.39.0",
|
||||||
"vite": "^7.1.0"
|
"vite": "^7.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,9 @@
|
|||||||
import React, { useEffect, useState } from "react";
|
import React from "react";
|
||||||
import { Trash, ArrowLeftRight } from "lucide-react";
|
import { Trash, ArrowLeftRight } from "lucide-react";
|
||||||
import { handleDeleteLoan } from "../utils/userHandler";
|
import { handleDeleteLoan } from "../utils/userHandler";
|
||||||
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||||
|
import Cookies from "js-cookie";
|
||||||
|
import { queryClient } from "../utils/queryClient";
|
||||||
|
|
||||||
type Loan = {
|
type Loan = {
|
||||||
id: number;
|
id: number;
|
||||||
@@ -22,40 +25,39 @@ const formatDate = (iso: string | null) => {
|
|||||||
return d.toLocaleString("de-DE", { dateStyle: "short", timeStyle: "short" });
|
return d.toLocaleString("de-DE", { dateStyle: "short", timeStyle: "short" });
|
||||||
};
|
};
|
||||||
|
|
||||||
const readUserLoansFromStorage = (): Loan[] => {
|
async function fetchUserLoans(): Promise<Loan[]> {
|
||||||
const raw = localStorage.getItem("userLoans");
|
const res = await fetch("http://localhost:8002/api/userLoans", {
|
||||||
if (!raw || raw === '"No loans found for this user"') return [];
|
method: "GET",
|
||||||
try {
|
headers: { Authorization: `Bearer ${Cookies.get("token") || ""}` },
|
||||||
const parsed = JSON.parse(raw);
|
});
|
||||||
return Array.isArray(parsed) ? (parsed as Loan[]) : [];
|
if (!res.ok) throw new Error("Failed to fetch user loans");
|
||||||
} catch {
|
const data = await res.json();
|
||||||
return [];
|
if (data === "No loans found for this user") return [];
|
||||||
}
|
return Array.isArray(data) ? (data as Loan[]) : [];
|
||||||
};
|
}
|
||||||
|
|
||||||
const Form4: React.FC = () => {
|
const Form4: React.FC = () => {
|
||||||
const [userLoans, setUserLoans] = useState<Loan[]>(() =>
|
const { data: userLoans = [], isFetching } = useQuery({
|
||||||
readUserLoansFromStorage()
|
queryKey: ["userLoans"],
|
||||||
);
|
queryFn: fetchUserLoans,
|
||||||
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
const deleteMutation = useMutation({
|
||||||
const onStorage = (e: StorageEvent) => {
|
mutationFn: (loanID: number) => handleDeleteLoan(loanID),
|
||||||
if (e.key === "userLoans") {
|
onSuccess: () => {
|
||||||
setUserLoans(readUserLoansFromStorage());
|
queryClient.invalidateQueries({ queryKey: ["userLoans"] });
|
||||||
}
|
},
|
||||||
};
|
});
|
||||||
window.addEventListener("storage", onStorage);
|
|
||||||
return () => window.removeEventListener("storage", onStorage);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const onDelete = async (loanID: number) => {
|
const onDelete = (loanID: number) => deleteMutation.mutate(loanID);
|
||||||
const ok = await handleDeleteLoan(loanID);
|
|
||||||
if (ok) {
|
if (isFetching) {
|
||||||
setUserLoans((prev) =>
|
return (
|
||||||
prev.filter((l) => Number(l.id) !== Number(loanID))
|
<div className="rounded-xl border border-slate-200 bg-white p-6 text-center text-slate-600 shadow-sm">
|
||||||
);
|
<p>Lade Ausleihen…</p>
|
||||||
}
|
</div>
|
||||||
};
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (userLoans.length === 0) {
|
if (userLoans.length === 0) {
|
||||||
return (
|
return (
|
||||||
|
@@ -4,22 +4,26 @@ import "./index.css";
|
|||||||
import App from "./App.tsx";
|
import App from "./App.tsx";
|
||||||
import { ToastContainer } from "react-toastify";
|
import { ToastContainer } from "react-toastify";
|
||||||
import "react-toastify/dist/ReactToastify.css";
|
import "react-toastify/dist/ReactToastify.css";
|
||||||
|
import { QueryClientProvider } from "@tanstack/react-query";
|
||||||
|
import { queryClient } from "./utils/queryClient";
|
||||||
|
|
||||||
createRoot(document.getElementById("root")!).render(
|
createRoot(document.getElementById("root")!).render(
|
||||||
<StrictMode>
|
<StrictMode>
|
||||||
<App />
|
<QueryClientProvider client={queryClient}>
|
||||||
<ToastContainer
|
<App />
|
||||||
position="top-right"
|
<ToastContainer
|
||||||
autoClose={3000}
|
position="top-right"
|
||||||
hideProgressBar={false}
|
autoClose={3000}
|
||||||
newestOnTop
|
hideProgressBar={false}
|
||||||
closeOnClick
|
newestOnTop
|
||||||
rtl={false}
|
closeOnClick
|
||||||
pauseOnFocusLoss={false}
|
rtl={false}
|
||||||
draggable
|
pauseOnFocusLoss={false}
|
||||||
pauseOnHover
|
draggable
|
||||||
theme="colored"
|
pauseOnHover
|
||||||
style={{ zIndex: 9999 }}
|
theme="colored"
|
||||||
/>
|
style={{ zIndex: 9999 }}
|
||||||
|
/>
|
||||||
|
</QueryClientProvider>
|
||||||
</StrictMode>
|
</StrictMode>
|
||||||
);
|
);
|
||||||
|
11
frontend/src/utils/queryClient.ts
Normal file
11
frontend/src/utils/queryClient.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import { QueryClient } from "@tanstack/react-query";
|
||||||
|
|
||||||
|
// Central QueryClient instance so utilities (e.g. file upload) can invalidate queries.
|
||||||
|
export const queryClient = new QueryClient({
|
||||||
|
defaultOptions: {
|
||||||
|
queries: {
|
||||||
|
refetchOnWindowFocus: false,
|
||||||
|
retry: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
@@ -1,5 +1,6 @@
|
|||||||
import { myToast } from "./toastify";
|
import { myToast } from "./toastify";
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
|
import { queryClient } from "./queryClient";
|
||||||
|
|
||||||
export const handleDeleteLoan = async (loanID: number): Promise<boolean> => {
|
export const handleDeleteLoan = async (loanID: number): Promise<boolean> => {
|
||||||
try {
|
try {
|
||||||
@@ -92,5 +93,10 @@ export const createLoan = async (startDate: string, endDate: string) => {
|
|||||||
removeArr = [];
|
removeArr = [];
|
||||||
Cookies.set("removeArr", "[]");
|
Cookies.set("removeArr", "[]");
|
||||||
myToast("Ausleihe erfolgreich erstellt!", "success");
|
myToast("Ausleihe erfolgreich erstellt!", "success");
|
||||||
|
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["userLoans"] });
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["allLoans"] });
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["borrowableItems"] });
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user