/* global React, Ic, useT */
const { useState } = React;
const SUPPORT_IDS = ["activity", "restaurant", "transport", "language", "planning", "fixer"];
const GROUP_TYPE_IDS = ["stag", "friends", "corp", "sports", "bday", "event", "other"];
function RequestForm() {
const { t } = useT();
const STEP_TITLES = [t("form.step.basics"), t("form.step.group"), t("form.step.plan"), t("form.step.review")];
const GROUP_TYPES = GROUP_TYPE_IDS.map((id) => ({ id, label: t("form.gt." + id) }));
const SUPPORT_OPTIONS = SUPPORT_IDS.map((id) => ({ id, label: t("form.sup." + id) }));
const LANGUAGES = [
{ v: "English", label: "English" },
{ v: "Latvian", label: "Latvian" },
{ v: "Any", label: t("form.lang.any") },
];
const [step, setStep] = useState(0);
const [done, setDone] = useState(false);
const [submitting, setSubmitting] = useState(false);
const [touched, setTouched] = useState({});
const [data, setData] = useState({
name: "",
contact: "",
date: "",
size: "",
type: "",
activities: "",
support: [],
language: "English",
notes: "",
});
const set = (k, v) => setData((d) => ({ ...d, [k]: v }));
const toggleSupport = (id) =>
setData((d) => ({
...d,
support: d.support.includes(id) ? d.support.filter((x) => x !== id) : [...d.support, id],
}));
// validation per step
const errors = (() => {
const e = {};
if (step === 0) {
if (!data.name.trim()) e.name = t("form.err.name");
if (!data.contact.trim()) e.contact = t("form.err.contact_required");
else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(data.contact) && !/^[+0-9\s\-()]{7,}$/.test(data.contact))
e.contact = t("form.err.contact_format");
if (!data.date) e.date = t("form.err.date");
}
if (step === 1) {
const n = parseInt(data.size, 10);
if (!data.size) e.size = t("form.err.size_required");
else if (isNaN(n) || n < 2) e.size = t("form.err.size_number");
else if (n > 200) e.size = t("form.err.size_max");
if (!data.type) e.type = t("form.err.type");
}
if (step === 2) {
if (!data.activities.trim()) e.activities = t("form.err.acts");
if (data.support.length === 0) e.support = t("form.err.support");
}
return e;
})();
const canAdvance = Object.keys(errors).length === 0;
const next = () => {
const fieldsByStep = [
["name", "contact", "date"],
["size", "type"],
["activities", "support"],
];
const fields = fieldsByStep[step] || [];
setTouched((tt) => ({ ...tt, ...Object.fromEntries(fields.map((f) => [f, true])) }));
if (!canAdvance) return;
if (step < 3) setStep(step + 1);
};
const back = () => { if (step > 0) setStep(step - 1); };
const submit = () => {
setSubmitting(true);
setTimeout(() => {
setSubmitting(false);
setDone(true);
}, 900);
};
const reset = () => {
setDone(false);
setStep(0);
setTouched({});
setData({ name: "", contact: "", date: "", size: "", type: "", activities: "", support: [], language: "English", notes: "" });
};
const groupTypeLabel = data.type ? (GROUP_TYPES.find((g) => g.id === data.type) || {}).label : "";
const supportLabels = data.support.map((id) => SUPPORT_OPTIONS.find((o) => o.id === id).label).join(", ");
return (
{t("form.p1")} {t("form.p2")} {t("form.basics.hint")} {t("form.group.hint")} {t("form.plan.hint")} {t("form.review.hint")}
{t("form.title_l1")}
{t("form.title_l2")}
{t("form.title_em")}
{STEP_TITLES[step]}
{STEP_TITLES[step]}
{STEP_TITLES[step]}
{STEP_TITLES[step]}
{t("form.success.p1")} {data.name.split(" ")[0]} {t("form.success.p2")}{" "} {data.contact.includes("@") ? t("form.success.email") : t("form.success.whatsapp")}{" "} {t("form.success.p3")} {data.size}-{t("form.success.p4")} {groupTypeLabel.toLowerCase()} {t("form.success.p5")} {data.date}.
{t("form.success.p1")} {data.name.split(" ")[0]} {t("form.success.p2")}{" "} {data.contact.includes("@") ? t("form.success.email") : t("form.success.whatsapp")}{" "} {t("form.success.p3")} {data.size}-{t("form.success.p4")} {groupTypeLabel.toLowerCase()} {t("form.success.p5")} {data.date}.