Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { useState } from "react";
import { useTranslation } from "next-i18next";
import axios from "axios";
import {
Alert,
Button,
Checkbox,
Grid,
Space,
Textarea,
TextInput,
} from "@mantine/core";
import { showNotification } from "@mantine/notifications";
export default function ContactForm() {
const { t } = useTranslation("common");
const [name, setName] = useState("");
const [mail, setMail] = useState("");
const [message, setMessage] = useState("");
const [human, setHuman] = useState(false);
const [robot, setRobot] = useState(false);
const handleSubmit = async () => {
if (!validate()) {
showNotification({
title: t("formIncomplete"),
message: t("formIncompleteText"),
color: "red",
});
return;
}
const response = await axios.post("/api/contact", {
name: name,
mail: mail,
message: message,
human: human,
robot: robot,
});
if (response.data.message == "mailSuccess") {
showNotification({
title: t("mailSuccess"),
message: t("mailSuccessText"),
color: "green",
});
} else {
showNotification({
title: t("mailFailure"),
message: t("mailFailureText"),
color: "red",
});
}
};
const validate = () => {
if (name == "") return false;
if (mail == "") return false;
if (message == "") return false;
return true;
};
return (
<>
<Grid>
<Grid.Col xs={12} sm={6}>
<TextInput
value={name}
onChange={(e) => setName(e.target.value)}
label={t("name")}
withAsterisk
placeholder="Maxime Musterfrau"
/>
</Grid.Col>
<Grid.Col xs={12} sm={6}>
<TextInput
value={mail}
onChange={(e) => setMail(e.target.value)}
label={t("mail")}
withAsterisk
placeholder="maxmu@student.ethz.ch"
/>
</Grid.Col>
<Grid.Col xs={12}>
<Textarea
value={message}
onChange={(e) => setMessage(e.target.value)}
label={t("message")}
withAsterisk
minRows={5}
/>
</Grid.Col>
<Grid.Col xs={12}>
<Checkbox
value={human}
onChange={() => setHuman(!human)}
label={t("human")}
/>
<Space h="xs" />
<Checkbox
value={robot}
onChange={() => setRobot(!robot)}
label={t("robot")}
/>
</Grid.Col>
<Grid.Col xs={12}>
<Button onClick={handleSubmit} variant="contained">
{t("submit")}
</Button>
</Grid.Col>
</Grid>
</>
);
}