fully implemented tanstack query
This commit is contained in:
@@ -18,14 +18,15 @@ import {
|
|||||||
Modal,
|
Modal,
|
||||||
ModalDialog,
|
ModalDialog,
|
||||||
ModalClose,
|
ModalClose,
|
||||||
|
CircularProgress,
|
||||||
} from "@mui/joy";
|
} from "@mui/joy";
|
||||||
import { submitFormData } from "../utils/sender";
|
import { submitFormData } from "../utils/api/form";
|
||||||
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 { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||||
import { confirmUser, fetchUsers } from "../utils/api/users";
|
import { confirmUser, fetchUsers } from "../utils/api/users";
|
||||||
|
|
||||||
const PAYMENT_METHODS = ["bar", "paypal", "andere"] as const;
|
const PAYMENT_METHODS = ["bar", "paypal", "andere"] as const;
|
||||||
@@ -83,6 +84,7 @@ const Field = ({
|
|||||||
|
|
||||||
export const MainForm = () => {
|
export const MainForm = () => {
|
||||||
const { t, i18n } = useTranslation();
|
const { t, i18n } = useTranslation();
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
const [invoice, setInvoice] = useState(false);
|
const [invoice, setInvoice] = useState(false);
|
||||||
const [msg, setMsg] = useState<Message | null>(null);
|
const [msg, setMsg] = useState<Message | null>(null);
|
||||||
@@ -114,6 +116,32 @@ export const MainForm = () => {
|
|||||||
queryFn: () => confirmUser(selectedUser),
|
queryFn: () => confirmUser(selectedUser),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const {
|
||||||
|
mutate: mutateForm,
|
||||||
|
isSuccess: mutateFormIsSuccess,
|
||||||
|
isPending: mutateFormIsPending,
|
||||||
|
isError: mutateFormIsError,
|
||||||
|
} = useMutation({
|
||||||
|
mutationFn: () => submitFormData(formData, selectedUser),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Redirecting to success page if mutation was successful
|
||||||
|
useEffect(() => {
|
||||||
|
if (mutateFormIsSuccess) {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["user", selectedUser] });
|
||||||
|
document.location.href = `/success?id=${nextID}&tickets=${formData.tickets}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mutateFormIsError) {
|
||||||
|
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
|
// Setting the nextID after a user is selected
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!userData) return;
|
if (!userData) return;
|
||||||
@@ -152,27 +180,6 @@ export const MainForm = () => {
|
|||||||
}
|
}
|
||||||
}, [formData.paymentMethod]);
|
}, [formData.paymentMethod]);
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
|
||||||
try {
|
|
||||||
const result = await submitFormData(formData, selectedUser || "");
|
|
||||||
if (result.success) {
|
|
||||||
document.location.href = `/success?id=${nextID}&tickets=${formData.tickets}`;
|
|
||||||
} else {
|
|
||||||
setMsg({
|
|
||||||
type: "danger",
|
|
||||||
headline: t("form-submission-failed"),
|
|
||||||
text: result.error || t("form-submission-failed"),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
setMsg({
|
|
||||||
type: "danger",
|
|
||||||
headline: t("error"),
|
|
||||||
text: t("form-submission-failed"),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Shorthand so we don't repeat formData + onChange on every Field usage
|
// Shorthand so we don't repeat formData + onChange on every Field usage
|
||||||
const fieldProps = { formData, onChange: handleChange };
|
const fieldProps = { formData, onChange: handleChange };
|
||||||
|
|
||||||
@@ -258,7 +265,7 @@ export const MainForm = () => {
|
|||||||
<form
|
<form
|
||||||
onSubmit={(e) => {
|
onSubmit={(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
handleSubmit();
|
mutateForm();
|
||||||
}}
|
}}
|
||||||
className="flex flex-col gap-4"
|
className="flex flex-col gap-4"
|
||||||
>
|
>
|
||||||
@@ -411,24 +418,29 @@ export const MainForm = () => {
|
|||||||
)}
|
)}
|
||||||
</FormControl>
|
</FormControl>
|
||||||
|
|
||||||
{/* Submit button */}
|
{mutateFormIsPending ? (
|
||||||
<Button
|
<div className="flex items-center justify-center">
|
||||||
type="submit"
|
<CircularProgress />
|
||||||
disabled={!formData.paymentMethod}
|
</div>
|
||||||
size="lg"
|
) : (
|
||||||
sx={{
|
<Button
|
||||||
mt: 2,
|
type="submit"
|
||||||
borderRadius: "14px",
|
disabled={!formData.paymentMethod}
|
||||||
fontWeight: 700,
|
size="lg"
|
||||||
letterSpacing: "0.05em",
|
sx={{
|
||||||
background: "linear-gradient(135deg, #2563eb, #1d4ed8)",
|
mt: 2,
|
||||||
"&:hover": {
|
borderRadius: "14px",
|
||||||
background: "linear-gradient(135deg, #1d4ed8, #1e40af)",
|
fontWeight: 700,
|
||||||
},
|
letterSpacing: "0.05em",
|
||||||
}}
|
background: "linear-gradient(135deg, #2563eb, #1d4ed8)",
|
||||||
>
|
"&:hover": {
|
||||||
{t("submit")}
|
background: "linear-gradient(135deg, #1d4ed8, #1e40af)",
|
||||||
</Button>
|
},
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{t("submit")}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* Alert message */}
|
{/* Alert message */}
|
||||||
{msg && (
|
{msg && (
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
import { API_BASE } from "../../config/api.config";
|
||||||
|
import type { FormData } from "../../config/interfaces.config";
|
||||||
|
|
||||||
|
export const submitFormData = async (data: FormData, username: string) => {
|
||||||
|
console.warn("submitFormData is fetching!");
|
||||||
|
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 3000)); // Wait 3 seconds
|
||||||
|
|
||||||
|
const response = await fetch(
|
||||||
|
`${API_BASE}/default/new-entry?username=${username}`,
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.text();
|
||||||
|
throw new Error(error || "Form submission failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
};
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
import { API_BASE } from "../config/api.config";
|
|
||||||
import type { FormData } from "../config/interfaces.config";
|
|
||||||
|
|
||||||
export const submitFormData = async (
|
|
||||||
data: FormData,
|
|
||||||
username: string | null,
|
|
||||||
) => {
|
|
||||||
if (username == null) {
|
|
||||||
return { success: false, errorCode: "x001" };
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await fetch(
|
|
||||||
`${API_BASE}/default/new-entry?username=${username}`,
|
|
||||||
{
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify(data),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const errorText = await response.text();
|
|
||||||
return { success: false, error: `Server error: ${errorText}` };
|
|
||||||
}
|
|
||||||
|
|
||||||
return { success: true };
|
|
||||||
} catch (error) {
|
|
||||||
return { success: false, error: (error as Error).message };
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user