85 lines
2.4 KiB
TypeScript
85 lines
2.4 KiB
TypeScript
// * Packages
|
|
import { router, usePage } from "@inertiajs/react";
|
|
import { route } from "ziggy-js";
|
|
import { LoaderCircleIcon, SendIcon } from "lucide-react";
|
|
import { useState } from "react";
|
|
// * Other
|
|
import BtnOptionnalLinkWithTooltip from "@/back/Components/BtnOptionnalLinkWithTooltip";
|
|
import useTrans from "@/hooks/use-translations";
|
|
import { useToast } from "@/hooks/use-toast";
|
|
import { AuthProp, ConfigBackend, UserModel } from "@/back/types/global";
|
|
import { GetTranslation } from "@/lib/utils";
|
|
|
|
export default function BtnResetPassword({ model }: { model: UserModel }) {
|
|
const { user: userModel } = usePage().props.auth as AuthProp;
|
|
const { routes } = usePage().props.configBackend as ConfigBackend;
|
|
const enumUserRole = usePage().props.enumUserRole as UserModel["role"][];
|
|
const { toast } = useToast();
|
|
|
|
const [isSending, setIsSending] = useState(false);
|
|
|
|
const getEnumRoleValue = (name: string) =>
|
|
enumUserRole.find((role) => role.name === name)?.value;
|
|
|
|
// Translations.
|
|
const transResetPassword = useTrans("auth.reset_password");
|
|
const transMailSent = useTrans("auth.mail_sent");
|
|
const transMailSentDesc = useTrans("auth.mail_sent_user_desc", {
|
|
params: { email: model.email },
|
|
});
|
|
|
|
const handleSendResetLink = () => {
|
|
setIsSending(true);
|
|
router.post(
|
|
route(`${routes.name}users.send-reset-password-link`, model),
|
|
{},
|
|
{
|
|
onSuccess: () => {
|
|
toast({
|
|
title: transMailSent,
|
|
description: transMailSentDesc,
|
|
});
|
|
},
|
|
onError: () => {
|
|
toast({
|
|
title: "nope",
|
|
description: "nope",
|
|
variant: "destructive",
|
|
});
|
|
},
|
|
onFinish: () => {
|
|
setIsSending(false);
|
|
},
|
|
},
|
|
);
|
|
};
|
|
|
|
return (
|
|
userModel.role.value === getEnumRoleValue("conceptor") && (
|
|
<form
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
handleSendResetLink();
|
|
}}
|
|
>
|
|
<BtnOptionnalLinkWithTooltip
|
|
content={
|
|
<>
|
|
{transResetPassword}
|
|
{isSending ? (
|
|
<LoaderCircleIcon className="animate-spin" />
|
|
) : (
|
|
<SendIcon />
|
|
)}
|
|
</>
|
|
}
|
|
tooltipContent={GetTranslation("auth.send_link")}
|
|
disabled={isSending}
|
|
variant="secondary"
|
|
size="default"
|
|
type="submit"
|
|
/>
|
|
</form>
|
|
)
|
|
);
|
|
}
|