Newer
Older
import {
Button,
Checkbox,
Divider,
Grid,
Modal,
Select,
Space,
Text,
Textarea,
TextInput,
} from "@mantine/core";
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
import { DatePicker, TimeRangeInput } from "@mantine/dates";
import { useEditor } from "@tiptap/react";
import { Link } from "@mantine/tiptap";
import StarterKit from "@tiptap/starter-kit";
import Underline from "@tiptap/extension-underline";
import Editor from "../components/editor";
import templates from "../content/eventTemplates.json";
import { gql, useMutation } from "@apollo/client";
const addEventMutation = gql`
mutation addEvent(
$title: String
$speaker: String
$description: String
$date: DateTime
$time: [DateTime]
$place: String
$signUp: String
$isStammtisch: Boolean
) {
addEvent(
title: $title
speaker: $speaker
description: $description
date: $date
time: $time
place: $place
signUp: $signUp
isStammtisch: $isStammtisch
)
}
`;
const editEventMutation = gql`
mutation editEvent(
$id: Int
$title: String
$speaker: String
$description: String
$date: DateTime
$time: [DateTime]
$place: String
$signUp: String
$isStammtisch: Boolean
) {
editEvent(
id: $id
title: $title
speaker: $speaker
description: $description
date: $date
time: $time
place: $place
signUp: $signUp
isStammtisch: $isStammtisch
)
}
`;
export default function EventModal({ open, close, event, refetch }) {
const [addEvent] = useMutation(addEventMutation);
const [editEvent] = useMutation(editEventMutation);
const { t } = useTranslation("common");
const initialValues = {
title: "",
speaker: "",
place: "",
signUp: "",
isStammtisch: false,
};
const form = useForm({
initialValues: initialValues,
validate: {
title: (value) => (value ? null : t("ENotEmpty")),
date: (value) => (value ? null : t("ENotEmpty")),
time: (value) => (value ? null : t("ENotEmpty")),
place: (value) => (value ? null : t("ENotEmpty")),
},
});
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const content = "";
const editor = useEditor({
extensions: [StarterKit, Underline, Link],
content,
});
const templatesFmt = templates.map((template) => ({
label: template.title,
value: template.title,
}));
const setTemplate = (t) => {
const template = templates.filter((temp) => temp.title === t)[0];
let timeArr = [new Date(), new Date()];
if (template.time) {
timeArr[0].setHours(
Number(template.time[0].split(":")[0]),
Number(template.time[0].split(":")[1]),
0
);
timeArr[1].setHours(
Number(template.time[1].split(":")[0]),
Number(template.time[1].split(":")[1]),
0
);
}
form.setValues({
...template,
time: template.time ? [...timeArr] : null,
});
editor.commands.setContent(template.description);
};
form.setValues({
...event,
date: new Date(event.date),
time: [new Date(event.startTime), new Date(event.endTime)],
});
if (editor) editor.commands.setContent(event.description);
if (editor) editor.commands.setContent(content);
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
}
};
useEffect(() => {
setEvent();
}, [open]);
const submit = async (values) => {
if (event) {
const res = await editEvent({
variables: {
id: event.id,
...values,
description: editor.getHTML(),
},
});
if (res.data.editEvent) {
refetch();
close();
}
} else {
const res = await addEvent({
variables: {
...values,
description: editor.getHTML(),
},
});
if (res.data.addEvent) {
refetch();
close();
}
}
};
return (
<Modal opened={open} onClose={close} fz="xl" size="xl">
<Text variant="h2" fw={700}>
{event ? t("editEvent") : t("addEvent")}
</Text>
<Space h="xl" />
<Select
data={templatesFmt}
placeholder={t("loadTemplate")}
onChange={(e) => setTemplate(e)}
/>
<Space h="xl" />
<Divider />
<Space h="xs" />
<form onSubmit={form.onSubmit((values) => submit(values))}>
<Grid>
<Grid.Col sm={12} md={6}>
<TextInput
withAsterisk
label={t("title")}
placeholder="Introduction to Free Software"
{...form.getInputProps("title")}
/>
</Grid.Col>
<Grid.Col sm={12} md={6}>
<TextInput
withAsterisk
label={t("speaker")}
placeholder="Maxime Musterfrau"
{...form.getInputProps("speaker")}
/>
</Grid.Col>
<Grid.Col sm={12}>
</Grid.Col>
<Grid.Col sm={12} md={6}>
<DatePicker
placeholder="January 1, 1970"
label={t("date")}
{...form.getInputProps("date")}
/>
</Grid.Col>
<Grid.Col sm={12} md={6}>
<TimeRangeInput
label={t("time")}
withAsterisk
clearable
{...form.getInputProps("time")}
/>
</Grid.Col>
<Grid.Col sm={12} md={6}>
<TextInput
withAsterisk
label={t("place")}
placeholder="ETH HG F 7"
{...form.getInputProps("place")}
/>
</Grid.Col>
<Grid.Col sm={12} md={6}>
<TextInput
label={t("signUp")}
placeholder="https://..."
{...form.getInputProps("signUp")}
/>
</Grid.Col>
<Grid.Col sm={12} md={3}>
<Checkbox
mt="xl"
label={t("stammtisch")}
{...form.getInputProps("isStammtisch", { type: "checkbox" })}
/>
</Grid.Col>
<Grid.Col sm={12}>
<Button type="submit">{t("submit")}</Button>
</Grid.Col>