implemented tanstack query

This commit is contained in:
2026-05-18 18:05:05 +02:00
parent a88c141f28
commit 3832aca12c
10 changed files with 151 additions and 71 deletions
+6 -4
View File
@@ -28,6 +28,10 @@ export const confirmUser = async (username) => {
]); ]);
if (rows.length > 0) { if (rows.length > 0) {
console.log(rows);
const { first_name, last_name } = rows[0];
const fullname = first_name + " " + last_name;
// creating userTicketTable // creating userTicketTable
const d = new Date(); const d = new Date();
@@ -37,8 +41,6 @@ export const confirmUser = async (username) => {
const date = `${day}_${month}_${year}`; const date = `${day}_${month}_${year}`;
const tableName = `${username}_${date}`; const tableName = `${username}_${date}`;
console.log(tableName);
const [createTable] = await pool.query( const [createTable] = await pool.query(
`CREATE TABLE IF NOT EXISTS ?? ( `CREATE TABLE IF NOT EXISTS ?? (
id INT AUTO_INCREMENT PRIMARY KEY, id INT AUTO_INCREMENT PRIMARY KEY,
@@ -70,9 +72,9 @@ export const confirmUser = async (username) => {
nextID = rows.length > 0 ? rows[0].id + 1 : 1; nextID = rows.length > 0 ? rows[0].id + 1 : 1;
}; };
await getNextID(); await getNextID();
return { success: true, nextID, tableName }; return { success: true, nextID, tableName, fullname };
} else { } else {
return { success: false, message: "Table creation failed" }; return { success: false, message: "Table creation failed", fullname };
} }
} else { } else {
return null; return null;
+34 -17
View File
@@ -16,8 +16,10 @@
"@mui/joy": "^5.0.0-beta.52", "@mui/joy": "^5.0.0-beta.52",
"@mui/material": "^9.0.1", "@mui/material": "^9.0.1",
"@tailwindcss/vite": "^4.3.0", "@tailwindcss/vite": "^4.3.0",
"@tanstack/react-query": "^5.100.10",
"i18next": "^26.0.10", "i18next": "^26.0.10",
"js-cookie": "^3.0.5", "js-cookie": "^3.0.5",
"k6": "^0.0.0",
"lucide-react": "^1.14.0", "lucide-react": "^1.14.0",
"react": "^17.0.0 || ^18.0.0 || ^19.0.0", "react": "^17.0.0 || ^18.0.0 || ^19.0.0",
"react-dom": "^17.0.0 || ^18.0.0 || ^19.0.0", "react-dom": "^17.0.0 || ^18.0.0 || ^19.0.0",
@@ -1766,6 +1768,32 @@
"vite": "^5.2.0 || ^6 || ^7 || ^8" "vite": "^5.2.0 || ^6 || ^7 || ^8"
} }
}, },
"node_modules/@tanstack/query-core": {
"version": "5.100.10",
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.100.10.tgz",
"integrity": "sha512-8UR0yJR+GiQ40m3lPhUr0xbfAupe6GSQiksSBSa9SM2NjezFyxXCIA69/lz8cSoNKZLrw1/PktIyQBJcVeMi3w==",
"license": "MIT",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
}
},
"node_modules/@tanstack/react-query": {
"version": "5.100.10",
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.100.10.tgz",
"integrity": "sha512-FLaZf2RCrA/Zgp4aiu5tG3TyasTRO7aZ99skxQpr3Hg/zXOhu6yq5FZCYQ/tRaJtM9ylnoK8tFK7PolXQadv6Q==",
"license": "MIT",
"dependencies": {
"@tanstack/query-core": "5.100.10"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
},
"peerDependencies": {
"react": "^18 || ^19"
}
},
"node_modules/@tybys/wasm-util": { "node_modules/@tybys/wasm-util": {
"version": "0.10.2", "version": "0.10.2",
"resolved": "https://registry.npmjs.org/@tybys/wasm-util/-/wasm-util-0.10.2.tgz", "resolved": "https://registry.npmjs.org/@tybys/wasm-util/-/wasm-util-0.10.2.tgz",
@@ -3042,6 +3070,12 @@
"node": ">=6" "node": ">=6"
} }
}, },
"node_modules/k6": {
"version": "0.0.0",
"resolved": "https://registry.npmjs.org/k6/-/k6-0.0.0.tgz",
"integrity": "sha512-GAQSWayS2+LjbH5bkRi+pMPYyP1JSp7o+4j58ANZ762N/RH/SdlAT3CHHztnn8s/xgg8kYNM24Gd2IPo9b5W+g==",
"license": "AGPL-3.0"
},
"node_modules/keyv": { "node_modules/keyv": {
"version": "4.5.4", "version": "4.5.4",
"resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz",
@@ -4178,23 +4212,6 @@
"dev": true, "dev": true,
"license": "ISC" "license": "ISC"
}, },
"node_modules/yaml": {
"version": "2.8.4",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.8.4.tgz",
"integrity": "sha512-ml/JPOj9fOQK8RNnWojA67GbZ0ApXAUlN2UQclwv2eVgTgn7O9gg9o7paZWKMp4g0H3nTLtS9LVzhkpOFIKzog==",
"license": "ISC",
"optional": true,
"peer": true,
"bin": {
"yaml": "bin.mjs"
},
"engines": {
"node": ">= 14.6"
},
"funding": {
"url": "https://github.com/sponsors/eemeli"
}
},
"node_modules/yocto-queue": { "node_modules/yocto-queue": {
"version": "0.1.0", "version": "0.1.0",
"resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz",
+2
View File
@@ -18,8 +18,10 @@
"@mui/joy": "^5.0.0-beta.52", "@mui/joy": "^5.0.0-beta.52",
"@mui/material": "^9.0.1", "@mui/material": "^9.0.1",
"@tailwindcss/vite": "^4.3.0", "@tailwindcss/vite": "^4.3.0",
"@tanstack/react-query": "^5.100.10",
"i18next": "^26.0.10", "i18next": "^26.0.10",
"js-cookie": "^3.0.5", "js-cookie": "^3.0.5",
"k6": "^0.0.0",
"lucide-react": "^1.14.0", "lucide-react": "^1.14.0",
"react": "^17.0.0 || ^18.0.0 || ^19.0.0", "react": "^17.0.0 || ^18.0.0 || ^19.0.0",
"react-dom": "^17.0.0 || ^18.0.0 || ^19.0.0", "react-dom": "^17.0.0 || ^18.0.0 || ^19.0.0",
+6 -1
View File
@@ -3,9 +3,14 @@ import { createRoot } from "react-dom/client";
import "./utils/i18n/index.ts"; import "./utils/i18n/index.ts";
import "./index.css"; import "./index.css";
import App from "./App.tsx"; import App from "./App.tsx";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
const queryClient = new QueryClient();
createRoot(document.getElementById("root")!).render( createRoot(document.getElementById("root")!).render(
<StrictMode> <StrictMode>
<App /> <QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>
</StrictMode>, </StrictMode>,
); );
+46 -47
View File
@@ -20,12 +20,13 @@ import {
ModalClose, ModalClose,
} from "@mui/joy"; } from "@mui/joy";
import { submitFormData } from "../utils/sender"; import { submitFormData } from "../utils/sender";
import { API_BASE } from "../config/api.config";
import type { FormData, Message } from "../config/interfaces.config"; import type { FormData, Message } from "../config/interfaces.config";
import PersonIcon from "@mui/icons-material/Person"; import PersonIcon from "@mui/icons-material/Person";
import QrCodeIcon from "@mui/icons-material/QrCode"; import QrCodeIcon from "@mui/icons-material/QrCode";
import TranslateIcon from "@mui/icons-material/Translate"; import TranslateIcon from "@mui/icons-material/Translate";
import qrCode from "../assets/PayPal-QR-Code.png"; import qrCode from "../assets/PayPal-QR-Code.png";
import { useQuery } from "@tanstack/react-query";
import { confirmUser, fetchUsers } from "../utils/api/users";
const PAYMENT_METHODS = ["bar", "paypal", "andere"] as const; const PAYMENT_METHODS = ["bar", "paypal", "andere"] as const;
const PAYMENT_LABELS: Record<string, string> = { const PAYMENT_LABELS: Record<string, string> = {
@@ -85,10 +86,8 @@ export const MainForm = () => {
const [invoice, setInvoice] = useState(false); const [invoice, setInvoice] = useState(false);
const [msg, setMsg] = useState<Message | null>(null); const [msg, setMsg] = useState<Message | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [nextID, setNextID] = useState<number | null>(null); const [nextID, setNextID] = useState<number | null>(null);
const [users, setUsers] = useState<string[]>([]); const [selectedUser, setSelectedUser] = useState("");
const [selectedUser, setSelectedUser] = useState<string | null>(null);
const [formData, setFormData] = useState<FormData>(DEFAULT_FORM); const [formData, setFormData] = useState<FormData>(DEFAULT_FORM);
const [showSelectUser, setShowSelectUser] = useState(false); const [showSelectUser, setShowSelectUser] = useState(false);
const [QRmodal, setQRmodal] = useState(false); const [QRmodal, setQRmodal] = useState(false);
@@ -97,23 +96,37 @@ export const MainForm = () => {
setFormData({ ...formData, [e.target.name]: e.target.value }); setFormData({ ...formData, [e.target.name]: e.target.value });
}; };
const confirmUser = async (username: string) => { useEffect(() => {
try { const savedUser = Cookies.get("selectedUser");
const res = await fetch( if (savedUser) {
`${API_BASE}/default/confirm-user?username=${username}`, setSelectedUser(savedUser);
);
const data = await res.json();
setNextID(data.nextID);
} catch (error) {
console.error("Error confirming user:", error);
} }
}; }, []);
const { data: usernameData, isLoading: usernameDataIsLoading } = useQuery({
queryKey: ["users"],
queryFn: fetchUsers,
});
const { data: userData, isSuccess: userDataIsSuccess } = useQuery({
queryKey: ["user", selectedUser],
enabled: !!selectedUser,
queryFn: () => confirmUser(selectedUser),
});
// Setting the nextID after a user is selected
useEffect(() => {
if (!userData) return;
setNextID(userData.nextID);
}, [userDataIsSuccess]);
const handleUserSelection = (username: string | null) => { const handleUserSelection = (username: string | null) => {
if (!username) return; if (username == null || username == "") {
console.log("Username must be set!");
return;
}
setSelectedUser(username); setSelectedUser(username);
confirmUser(username);
Cookies.set("selectedUser", username);
}; };
const changeTranslation = () => { const changeTranslation = () => {
@@ -140,30 +153,7 @@ export const MainForm = () => {
} }
}, [formData.paymentMethod]); }, [formData.paymentMethod]);
useEffect(() => {
(async () => {
try {
const res = await fetch(`${API_BASE}/default/users`);
const data = await res.json();
setUsers(data.users);
} catch {
setMsg({
type: "danger",
headline: t("error"),
text: t("failed-to-load-users"),
});
}
})();
const cookieUser = Cookies.get("selectedUser");
if (cookieUser) {
setSelectedUser(cookieUser);
confirmUser(cookieUser);
}
}, []);
const handleSubmit = async () => { const handleSubmit = async () => {
setIsLoading(true);
try { try {
const result = await submitFormData(formData, selectedUser || ""); const result = await submitFormData(formData, selectedUser || "");
if (result.success) { if (result.success) {
@@ -175,8 +165,8 @@ export const MainForm = () => {
text: result.error || t("form-submission-failed"), text: result.error || t("form-submission-failed"),
}); });
} }
} finally { } catch (err) {
setIsLoading(false); console.log("remove");
} }
}; };
@@ -191,15 +181,14 @@ export const MainForm = () => {
<Typography>{t("user")}</Typography> <Typography>{t("user")}</Typography>
{/* User selection */} {/* User selection */}
<Autocomplete <Autocomplete
options={users} options={usernameData?.users ?? []}
loading={usernameDataIsLoading}
loadingText={t("loading")}
value={selectedUser} value={selectedUser}
onChange={(_, value) => handleUserSelection(value)} onChange={(_, value) => handleUserSelection(value)}
placeholder={t("user")} placeholder={t("user")}
variant="soft" variant="soft"
sx={{ borderRadius: "10px" }} sx={{ borderRadius: "10px" }}
onKeyDown={(e) => {
if (e.key === "Enter") e.preventDefault();
}}
/> />
</ModalDialog> </ModalDialog>
</Modal> </Modal>
@@ -250,6 +239,17 @@ export const MainForm = () => {
<IconButton onClick={changeTranslation}> <IconButton onClick={changeTranslation}>
<TranslateIcon /> <TranslateIcon />
</IconButton> </IconButton>
<Typography
level="title-sm"
textColor="var(--joy-palette-success-plainColor)"
sx={{
fontFamily: "monospace",
opacity: "100%",
alignSelf: "center",
}}
>
{`${t("greeting")} ${userData?.fullname ?? t("loading")}`}
</Typography>
</ButtonGroup> </ButtonGroup>
<form <form
@@ -411,7 +411,6 @@ export const MainForm = () => {
{/* Submit button */} {/* Submit button */}
<Button <Button
type="submit" type="submit"
loading={isLoading}
disabled={!formData.paymentMethod} disabled={!formData.paymentMethod}
size="lg" size="lg"
sx={{ sx={{
+21
View File
@@ -0,0 +1,21 @@
import { API_BASE } from "../../config/api.config";
import Cookies from "js-cookie";
export const fetchUsers = async () => {
console.warn("fetchUsers is fetching!");
const response = await fetch(`${API_BASE}/default/users`);
const data = await response.json();
return data;
};
export const confirmUser = async (username: string) => {
console.warn("confirmUser is fetching!");
const response = await fetch(
`${API_BASE}/default/confirm-user?username=${username}`,
);
const data = await response.json();
Cookies.set("selectedUser", username);
return data;
};
+3 -1
View File
@@ -26,5 +26,7 @@
"thank-you": "Vielen Dank für Ihre Unterstützung der Claudius Akademie! Wir wünschen Ihnen viel Glück mit dem Los.", "thank-you": "Vielen Dank für Ihre Unterstützung der Claudius Akademie! Wir wünschen Ihnen viel Glück mit dem Los.",
"select-payment-method": "Zahlungsmethode auswählen", "select-payment-method": "Zahlungsmethode auswählen",
"return-to-homepage": "Zurück", "return-to-homepage": "Zurück",
"qr-text": "PayPal QR-Code der Claudius Akademie" "qr-text": "PayPal QR-Code der Claudius Akademie",
"loading": "Lädt...",
"greeting": "Hallo,"
} }
+3 -1
View File
@@ -27,5 +27,7 @@
"thank-you": "Thank you for supporting the Claudius Akademie! We wish you the best of luck with your ticket.", "thank-you": "Thank you for supporting the Claudius Akademie! We wish you the best of luck with your ticket.",
"select-payment-method": "Select Payment Method", "select-payment-method": "Select Payment Method",
"return-to-homepage": "Return", "return-to-homepage": "Return",
"qr-text": "PayPal QR-Code from the Claudius Akademie" "qr-text": "PayPal QR-Code from the Claudius Akademie",
"loading": "Loading...",
"greeting": "Hello,"
} }
+15
View File
@@ -0,0 +1,15 @@
import http from "k6/http";
import { sleep } from "k6";
export const options = {
vus: 100, // amount of users
duration: "60s", // duration of the test
};
export default function () {
http.get(
"http://localhost:8004/default/confirm-user?username=TheisGaedigk",
);
http.get("http://localhost:8004/default/users");
sleep(1);
}
+15
View File
@@ -0,0 +1,15 @@
// Before running: Establish VPN connection first
import http from "k6/http";
import { sleep } from "k6";
export const options = {
vus: 100, // amount of users
duration: "60s", // duration of the test
};
export default function () {
http.get("http://backend:8004/default/confirm-user?username=TheisGaedigk");
http.get("http://backend:8004/default/users");
sleep(0.5);
}