"use client"; import { useState } from "react"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; import { Input } from "@heroui/input"; import { EyeFilledIcon, EyeSlashFilledIcon } from "@/components/icons"; import { Button } from "@heroui/button"; import PasswordChecklist from "react-password-checklist"; import { savePassword } from "@/services/master-user"; import { close, error, loading } from "@/config/swal"; export default function PasswordForm(props: { doFetch: () => void }) { const MySwal = withReactContent(Swal); const [isVisible, setIsVisible] = useState([false, false]); const [passwordConf, setPasswordConf] = useState(""); const [password, setPassword] = useState(""); const [isValidPassword, setIsValidPassword] = useState(false); const onSubmit = async () => { loading(); const data = { password: password, confirmPassword: passwordConf, }; const res = await savePassword(data); if (res?.error) { error(res.message); return false; } close(); props.doFetch(); MySwal.fire({ title: "Sukses", icon: "success", confirmButtonColor: "#3085d6", confirmButtonText: "OK", }).then((result) => { if (result.isConfirmed) { } }); }; const generatePassword = () => { const length = Math.floor(Math.random() * 9) + 8; const upperCaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const lowerCaseChars = "abcdefghijklmnopqrstuvwxyz"; const numberChars = "0123456789"; const specialChars = "!@#$%^&*"; const allChars = upperCaseChars + lowerCaseChars + numberChars + specialChars; let generatedPassword = ""; generatedPassword += upperCaseChars[Math.floor(Math.random() * upperCaseChars.length)]; generatedPassword += specialChars[Math.floor(Math.random() * specialChars.length)]; generatedPassword += numberChars[Math.floor(Math.random() * numberChars.length)]; generatedPassword += lowerCaseChars[Math.floor(Math.random() * lowerCaseChars.length)]; for (let i = generatedPassword.length; i < length; i++) { generatedPassword += allChars[Math.floor(Math.random() * allChars.length)]; } generatedPassword = generatedPassword .split("") .sort(() => 0.5 - Math.random()) .join(""); setPassword(generatedPassword); }; return (
); }