2024-05-03 09:22:45 -05:00
|
|
|
import toast from "react-hot-toast";
|
|
|
|
import Modal from "../Modal";
|
|
|
|
import TextInput from "../TextInput";
|
|
|
|
import { FormEvent, useState } from "react";
|
2024-06-09 08:27:16 -05:00
|
|
|
import { useTranslation, Trans } from "next-i18next";
|
2024-08-01 15:54:19 -05:00
|
|
|
import { useAddUser } from "@/hooks/store/admin/users";
|
2024-05-03 09:22:45 -05:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
onClose: Function;
|
|
|
|
};
|
|
|
|
|
|
|
|
type FormData = {
|
|
|
|
name: string;
|
|
|
|
username?: string;
|
|
|
|
email?: string;
|
|
|
|
password: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
const emailEnabled = process.env.NEXT_PUBLIC_EMAIL_PROVIDER === "true";
|
|
|
|
|
|
|
|
export default function NewUserModal({ onClose }: Props) {
|
2024-06-09 08:27:16 -05:00
|
|
|
const { t } = useTranslation();
|
2024-08-01 15:54:19 -05:00
|
|
|
|
|
|
|
const addUser = useAddUser();
|
|
|
|
|
2024-05-03 09:22:45 -05:00
|
|
|
const [form, setForm] = useState<FormData>({
|
|
|
|
name: "",
|
|
|
|
username: "",
|
|
|
|
email: emailEnabled ? "" : undefined,
|
|
|
|
password: "",
|
|
|
|
});
|
|
|
|
const [submitLoader, setSubmitLoader] = useState(false);
|
|
|
|
|
|
|
|
async function submit(event: FormEvent<HTMLFormElement>) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
if (!submitLoader) {
|
2024-10-21 12:59:05 -05:00
|
|
|
if (form.password.length < 8)
|
|
|
|
return toast.error(t("password_length_error"));
|
|
|
|
|
2024-05-03 09:22:45 -05:00
|
|
|
const checkFields = () => {
|
|
|
|
if (emailEnabled) {
|
|
|
|
return form.name !== "" && form.email !== "" && form.password !== "";
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
form.name !== "" && form.username !== "" && form.password !== ""
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (checkFields()) {
|
|
|
|
setSubmitLoader(true);
|
|
|
|
|
2024-08-01 15:54:19 -05:00
|
|
|
await addUser.mutateAsync(form, {
|
|
|
|
onSuccess: () => {
|
|
|
|
onClose();
|
|
|
|
},
|
2024-10-26 09:58:27 -05:00
|
|
|
onSettled: () => {
|
|
|
|
setSubmitLoader(false);
|
|
|
|
},
|
2024-08-01 15:54:19 -05:00
|
|
|
});
|
2024-05-03 09:22:45 -05:00
|
|
|
} else {
|
2024-06-09 08:27:16 -05:00
|
|
|
toast.error(t("fill_all_fields_error"));
|
2024-05-03 09:22:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Modal toggleModal={onClose}>
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="text-xl font-thin">{t("create_new_user")}</p>
|
2024-05-03 09:22:45 -05:00
|
|
|
|
|
|
|
<div className="divider mb-3 mt-1"></div>
|
|
|
|
|
|
|
|
<form onSubmit={submit}>
|
|
|
|
<div className="grid sm:grid-cols-2 gap-3">
|
|
|
|
<div>
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="mb-2">{t("display_name")}</p>
|
2024-05-03 09:22:45 -05:00
|
|
|
<TextInput
|
2024-06-09 08:27:16 -05:00
|
|
|
placeholder={t("placeholder_johnny")}
|
2024-05-03 09:22:45 -05:00
|
|
|
className="bg-base-200"
|
|
|
|
onChange={(e) => setForm({ ...form, name: e.target.value })}
|
|
|
|
value={form.name}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2024-07-22 22:34:36 -05:00
|
|
|
{emailEnabled && (
|
2024-05-03 09:22:45 -05:00
|
|
|
<div>
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="mb-2">{t("email")}</p>
|
2024-05-03 09:22:45 -05:00
|
|
|
<TextInput
|
2024-06-09 08:27:16 -05:00
|
|
|
placeholder={t("placeholder_email")}
|
2024-05-03 09:22:45 -05:00
|
|
|
className="bg-base-200"
|
2024-05-12 23:27:29 -05:00
|
|
|
onChange={(e) => setForm({ ...form, email: e.target.value })}
|
|
|
|
value={form.email}
|
2024-05-03 09:22:45 -05:00
|
|
|
/>
|
|
|
|
</div>
|
2024-07-22 22:34:36 -05:00
|
|
|
)}
|
2024-05-03 09:22:45 -05:00
|
|
|
|
|
|
|
<div>
|
2024-05-12 23:27:29 -05:00
|
|
|
<p className="mb-2">
|
2024-06-09 08:27:16 -05:00
|
|
|
{t("username")}{" "}
|
2024-05-12 23:27:29 -05:00
|
|
|
{emailEnabled && (
|
2024-06-09 08:27:16 -05:00
|
|
|
<span className="text-xs text-neutral">{t("optional")}</span>
|
2024-05-12 23:27:29 -05:00
|
|
|
)}
|
|
|
|
</p>
|
2024-05-03 09:22:45 -05:00
|
|
|
<TextInput
|
2024-06-09 08:27:16 -05:00
|
|
|
placeholder={t("placeholder_john")}
|
2024-05-03 09:22:45 -05:00
|
|
|
className="bg-base-200"
|
2024-05-12 23:27:29 -05:00
|
|
|
onChange={(e) => setForm({ ...form, username: e.target.value })}
|
|
|
|
value={form.username}
|
2024-05-03 09:22:45 -05:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div>
|
2024-06-09 08:27:16 -05:00
|
|
|
<p className="mb-2">{t("password")}</p>
|
2024-05-03 09:22:45 -05:00
|
|
|
<TextInput
|
|
|
|
placeholder="••••••••••••••"
|
|
|
|
className="bg-base-200"
|
|
|
|
onChange={(e) => setForm({ ...form, password: e.target.value })}
|
|
|
|
value={form.password}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2024-05-03 16:08:58 -05:00
|
|
|
<div role="note" className="alert alert-note mt-5">
|
|
|
|
<i className="bi-exclamation-triangle text-xl" />
|
|
|
|
<span>
|
2024-06-09 09:41:06 -05:00
|
|
|
<Trans
|
|
|
|
i18nKey="password_change_note"
|
|
|
|
components={[<b key={0} />]}
|
|
|
|
/>
|
2024-05-03 16:08:58 -05:00
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
|
2024-05-03 09:22:45 -05:00
|
|
|
<div className="flex justify-between items-center mt-5">
|
|
|
|
<button
|
|
|
|
className="btn btn-accent dark:border-violet-400 text-white ml-auto"
|
|
|
|
type="submit"
|
|
|
|
>
|
2024-06-09 08:27:16 -05:00
|
|
|
{t("create_user")}
|
2024-05-03 09:22:45 -05:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|