edited frontend for sending messages
This commit is contained in:
@@ -1,13 +1,75 @@
|
|||||||
import { Field, Textarea } from "@chakra-ui/react"
|
import { Field, Textarea, Button, Alert } from "@chakra-ui/react";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
import { useState } from "react";
|
||||||
|
import { API_BASE } from "@/config/api.config";
|
||||||
|
import Cookies from "js-cookie";
|
||||||
|
|
||||||
|
interface Alert {
|
||||||
|
type: "info" | "warning" | "success" | "error" | "neutral";
|
||||||
|
headline: string;
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
|
||||||
export const ContactPage = () => {
|
export const ContactPage = () => {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const [message, setMessage] = useState("");
|
||||||
|
const [alert, setAlert] = useState<Alert | null>(null);
|
||||||
|
|
||||||
|
const sendMessage = async () => {
|
||||||
|
// Logic to send the message
|
||||||
|
const result = await fetch(`${API_BASE}/contact`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${Cookies.get("token") || ""}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Accept: "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ message }),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.ok) {
|
||||||
|
setAlert({
|
||||||
|
type: "success",
|
||||||
|
headline: t("contactPage_successHeadline"),
|
||||||
|
text: t("contactPage_successText"),
|
||||||
|
});
|
||||||
|
setMessage("");
|
||||||
|
} else {
|
||||||
|
setAlert({
|
||||||
|
type: "error",
|
||||||
|
headline: t("contactPage_errorHeadline"),
|
||||||
|
text: t("contactPage_errorText"),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Field.Root invalid>
|
<>
|
||||||
|
<Field.Root invalid={message === ""}>
|
||||||
<Field.Label>
|
<Field.Label>
|
||||||
Comment <Field.RequiredIndicator />
|
{t("contactPage_messageLabel")}
|
||||||
|
<Field.RequiredIndicator />
|
||||||
</Field.Label>
|
</Field.Label>
|
||||||
<Textarea placeholder="Start typing..." variant="subtle" />
|
<Textarea
|
||||||
<Field.ErrorText>Field is required</Field.ErrorText>
|
placeholder={t("contactPage_messagePlaceholder")}
|
||||||
|
variant="subtle"
|
||||||
|
value={message}
|
||||||
|
onChange={(e) => setMessage(e.target.value)}
|
||||||
|
/>
|
||||||
|
{message === "" && (
|
||||||
|
<Field.ErrorText>{t("contactPage_messageErrorText")}</Field.ErrorText>
|
||||||
|
)}
|
||||||
</Field.Root>
|
</Field.Root>
|
||||||
|
{alert && (
|
||||||
|
<Alert.Root status={alert.type}>
|
||||||
|
<Alert.Indicator />
|
||||||
|
<Alert.Content>
|
||||||
|
<Alert.Title>{alert.headline}</Alert.Title>
|
||||||
|
<Alert.Description>{alert.text}</Alert.Description>
|
||||||
|
</Alert.Content>
|
||||||
|
</Alert.Root>
|
||||||
|
)}
|
||||||
|
<Button onClick={sendMessage}>{t("contactPage_sendButton")}</Button>
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user