diff --git a/frontend/src/pages/MainForm.tsx b/frontend/src/pages/MainForm.tsx index f9b767d..0ac10c0 100644 --- a/frontend/src/pages/MainForm.tsx +++ b/frontend/src/pages/MainForm.tsx @@ -4,9 +4,11 @@ import { Checkbox, Button, Alert, + CircularProgress, } from "@mui/material"; import { useTranslation } from "react-i18next"; import { useState } from "react"; +import { submitFormData } from "../utils/sender"; interface Message { type: "error" | "info" | "success" | "warning"; @@ -18,38 +20,105 @@ export const MainForm = () => { const { t } = useTranslation(); const [invoice, setInvoice] = useState(false); const [msg, setMsg] = useState(null); + const [isLoading, setIsLoading] = useState(false); + const [formData, setFormData] = useState({ + firstName: "", + lastName: "", + email: "", + phoneNumber: "", + tickets: 1, + companyName: "", + cmpFirstName: "", + cpmLastName: "", + cpmEmail: "", + cpmPhoneNumber: "", + street: "", + postalCode: "", + paymentMethod: "", + code: "", + }); - const handleSubmit = () => { - // Form submission logic goes here + const handleChange = (e: React.ChangeEvent) => { + setFormData({ ...formData, [e.target.name]: e.target.value }); + }; + + const handleSubmit = async () => { + setIsLoading(true); + try { + const result = await submitFormData(formData); + if (result.success) { + setMsg({ + type: "success", + headline: t("success"), + text: t("form-submitted-successfully"), + }); + } else { + setMsg({ + type: "error", + headline: t("error"), + text: result.error || t("form-submission-failed"), + }); + } + } finally { + setIsLoading(false); + } }; return ( <> -
+ { + e.preventDefault(); + handleSubmit(); + }} + > + - { id="company-name" label={t("company-name")} variant="filled" + value={formData.companyName} + onChange={handleChange} + name="companyName" /> )} @@ -111,14 +203,18 @@ export const MainForm = () => { } label={t("paypal")} /> } label={t("transfer")} /> - + {isLoading && } + + + {msg && ( {msg.headline}: {msg.text} diff --git a/frontend/src/utils/sender.ts b/frontend/src/utils/sender.ts new file mode 100644 index 0000000..3a1904c --- /dev/null +++ b/frontend/src/utils/sender.ts @@ -0,0 +1,37 @@ +interface FormData { + firstName: string; + lastName: string; + email: string; + phoneNumber: string; + tickets: number; + companyName: string; + cmpFirstName: string; + cpmLastName: string; + cpmEmail: string; + cpmPhoneNumber: string; + street: string; + postalCode: string; + paymentMethod: string; + code: string; +} + +export const submitFormData = async (data: FormData) => { + try { + const response = await fetch("http://localhost:8004/default/frontend", { + 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 }; + } +};