ldc-supportweb/resources/ts/back/Components/BtnResetPassword.tsx
2026-06-22 15:06:21 +02:00

87 lines
2.4 KiB
TypeScript

import { router, usePage } from "@inertiajs/react";
import { LoaderCircleIcon, SendIcon } from "lucide-react";
import { useState } from "react";
import BtnOptionnalLinkWithTooltip from "@/back/Components/BtnOptionnalLinkWithTooltip";
import {
type AuthProp,
type ConfigBackend,
type UserModel,
} from "@/back/types/global";
import { useToast } from "@/hooks/use-toast";
import useTrans from "@/hooks/use-translations";
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`, { id: model.id }),
{},
{
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>
)
);
}