(DEFAULT_FORM);
const [showSelectUser, setShowSelectUser] = useState(false);
const [QRmodal, setQRmodal] = useState(false);
@@ -106,8 +102,8 @@ export const MainForm = () => {
setMsg({
type: "warning",
headline: t("set-username-headline"),
- text: t("set-username-text")
- })
+ text: t("set-username-text"),
+ });
}
}, []);
@@ -116,43 +112,30 @@ export const MainForm = () => {
queryFn: fetchUsers,
});
- const { data: userData, isSuccess: userDataIsSuccess } = useQuery({
+ const { data: userData } = useQuery({
queryKey: ["user", selectedUser],
enabled: !!selectedUser,
queryFn: () => confirmUser(selectedUser),
});
- const {
- mutate: mutateForm,
- isSuccess: mutateFormIsSuccess,
- isPending: mutateFormIsPending,
- isError: mutateFormIsError,
- } = useMutation({
+ const { mutate: mutateForm, isPending: mutateFormIsPending } = useMutation({
mutationFn: () => submitFormData(formData, selectedUser),
- });
-
- // Redirecting to success page if mutation was successful
- useEffect(() => {
- if (mutateFormIsSuccess) {
+ onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["user", selectedUser] });
document.location.href = `/success?id=${nextID}&tickets=${formData.tickets}`;
- }
-
- if (mutateFormIsError) {
+ },
+ onError: () => {
queryClient.invalidateQueries({ queryKey: ["user", selectedUser] });
setMsg({
type: "danger",
headline: t("error"),
text: t("form-submission-failed"),
});
- }
- }, [mutateFormIsSuccess, mutateFormIsError]);
+ },
+ });
// Setting the nextID after a user is selected
- useEffect(() => {
- if (!userData) return;
- setNextID(userData.nextID);
- }, [userDataIsSuccess]);
+ const nextID = userData?.nextID ?? "N/A";
const handleUserSelection = (username: string | null) => {
if (username == null || username == "") {
@@ -180,50 +163,21 @@ export const MainForm = () => {
}
};
- useEffect(() => {
- if (formData.paymentMethod === "paypal") {
- setQRmodal(true);
- }
- }, [formData.paymentMethod]);
-
// Shorthand so we don't repeat formData + onChange on every Field usage
const fieldProps = { formData, onChange: handleChange };
return (
<>
-
-
- setShowSelectUser(false)} />
- {t("user")}
- {/* User selection */}
- handleUserSelection(value)}
- placeholder={t("user")}
- variant="soft"
- sx={{ borderRadius: "10px" }}
- />
-
-
-
-
- setQRmodal(false)} />
- {t("qr-text")}
-
-
-
+
+
+
{
formData.paymentMethod === method ? "solid" : "soft"
}
color="primary"
- onClick={() =>
+ onClick={() => {
setFormData((prev) => ({
...prev,
paymentMethod: method,
- }))
- }
+ }));
+ if (method === "paypal") {
+ setQRmodal(true);
+ }
+ }}
sx={{
flex: 1,
minWidth: "90px",
diff --git a/frontend/src/utils/api/form.ts b/frontend/src/utils/api/form.ts
index 5c9f095..d74138c 100644
--- a/frontend/src/utils/api/form.ts
+++ b/frontend/src/utils/api/form.ts
@@ -1,7 +1,10 @@
import { API_BASE } from "../../config/api.config";
import type { FormData } from "../../config/interfaces.config";
-export const submitFormData = async (data: FormData, username: string) => {
+export const submitFormData = async (
+ data: FormData,
+ username: string | null,
+) => {
console.warn("submitFormData is fetching!");
// await new Promise((resolve) => setTimeout(resolve, 3000)); // Wait 3 seconds
diff --git a/frontend/src/utils/api/users.ts b/frontend/src/utils/api/users.ts
index 4e0e705..aac1c23 100644
--- a/frontend/src/utils/api/users.ts
+++ b/frontend/src/utils/api/users.ts
@@ -10,7 +10,11 @@ export const fetchUsers = async () => {
return data;
};
-export const confirmUser = async (username: string) => {
+export const confirmUser = async (username: string | null) => {
+ if (!username) {
+ return;
+ }
+
console.warn("confirmUser is fetching!");
const response = await fetch(
`${API_BASE}/default/confirm-user?username=${username}`,