feat(settings):add all settings pages

This commit is contained in:
2025-08-28 10:13:44 +05:30
parent eda57aae71
commit 3444291e25
5 changed files with 850 additions and 168 deletions

View File

@@ -1,235 +1,265 @@
"use client";
import React, { useEffect, useState } from "react";
import {
TextInput,
PasswordInput,
Button,
Title,
Paper,
Box,
Text,
Group,
Grid,
} from "@mantine/core";
import { TextInput, PasswordInput, Button, Title, Paper, Group, Box } from "@mantine/core";
import { notifications } from "@mantine/notifications";
import { IconEye, IconEyeOff, IconLock } from "@tabler/icons-react";
// import CaptchaImage from "@/app/SetPassword/CaptchaImage";
import { IconLock } from "@tabler/icons-react";
import { generateCaptcha } from "@/app/captcha";
const ChangePassword: React.FC = () => {
export default function ChangePassword() {
const [oldPassword, setOldPassword] = useState("");
const [newPassword, setNewPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [captcha, setCaptcha] = useState("");
const [showOld, setShowOld] = useState(false);
const [showNew, setShowNew] = useState(false);
const [showConfirm, setShowConfirm] = useState(false);
// const [captchaCode] = useState("X7pK9");
const [inputCaptcha, setInputCaptcha] = useState("");
const [captchaInput, setCaptchaInput] = useState('');
const [captchaError, setCaptchaError] = useState('');
const [captchaInput, setCaptchaInput] = useState("");
const [otp, setOtp] = useState("");
const [otpValidated, setOtpValidated] = useState(false);
const [step, setStep] = useState<"form" | "otp" | "final">("form"); // ✅ steps control
const [passwordHistory] = useState(["Pass@1234", "OldPass@123", "MyPass#2023"]);
const icon = <IconLock size={18} stroke={1.5} />;
useEffect(() => {
const loadCaptcha = async () => {
const newCaptcha = await generateCaptcha();
setCaptcha(newCaptcha);
};
loadCaptcha();
regenerateCaptcha();
}, []);
const regenerateCaptcha = () => {
// setCaptcha(generateCaptcha());
const loadCaptcha = async () => {
const newCaptcha = await generateCaptcha();
setCaptcha(newCaptcha);
};
loadCaptcha();
setInputCaptcha("");
const regenerateCaptcha = async () => {
const newCaptcha = await generateCaptcha();
setCaptcha(newCaptcha);
setCaptchaInput("");
};
const validatePasswordPolicy = (password: string) => {
return /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*])[A-Za-z\d!@#$%^&*]{8,}$/.test(password);
};
const handleSubmit = () => {
// 1. Check all mandatory fields
if (!oldPassword || !newPassword || !confirmPassword || !captcha) {
// Step 1 → validate form
if (step === "form") {
if (!oldPassword || !newPassword || !confirmPassword || !captchaInput) {
notifications.show({
title: "Missing Field",
message: "Please fill all mandatory fields.",
color: "red",
});
return;
}
const actualOldPassword = "Pass@123";
if (oldPassword !== actualOldPassword) {
notifications.show({
title: "Old Password Incorrect",
message: "Entered old password does not match.",
color: "red",
});
return;
}
if (!validatePasswordPolicy(newPassword)) {
notifications.show({
title: "Invalid Password",
message:
"Password must be at least 8 characters and contain alphanumeric and special characters.",
color: "red",
});
return;
}
if (passwordHistory.includes(newPassword)) {
notifications.show({
title: "Password Reused",
message: "New password must be different from the last 3 passwords.",
color: "red",
});
return;
}
if (newPassword !== confirmPassword) {
notifications.show({
title: "Password Mismatch",
message: "Confirm password does not match new password.",
color: "red",
});
return;
}
if (captchaInput !== captcha) {
notifications.show({
title: "Invalid Captcha",
message: "Please enter correct Captcha",
color: "red",
});
regenerateCaptcha();
return;
}
// ✅ Passed → move to OTP
setStep("otp");
notifications.show({
title: "Missing Field",
message: "Please fill all mandatory fields.",
color: "red",
title: "OTP Sent",
message: "An OTP has been sent to your registered mobile.",
color: "blue",
});
return;
}
// 2. Validate old password (simulate real check)
const actualOldPassword = "OldPass@123"; // Simulated stored password
if (oldPassword !== actualOldPassword) {
// Step 2 → validate OTP
if (step === "otp") {
if (otp !== "123456") {
notifications.show({
title: "Invalid OTP",
message: "Please enter the correct OTP.",
color: "red",
});
return;
}
setOtpValidated(true);
setStep("final");
notifications.show({
title: "Old Password Incorrect",
message: "Entered old password does not match.",
color: "red",
title: "OTP Verified",
message: "OTP has been successfully verified.",
color: "green",
});
return;
}
// 3. New password validation
if (!validatePasswordPolicy(newPassword)) {
// Step 3 → Final Change Password
if (step === "final") {
notifications.show({
title: "Invalid Password",
message:
"Password must be at least 8 characters and contain alphanumeric and special characters.",
color: "red",
title: "Password Changed",
message: "Your password has been successfully updated.",
color: "green",
});
return;
resetForm();
}
};
if (passwordHistory.includes(newPassword)) {
notifications.show({
title: "Password Reused",
message: "New password must be different from the last 3 passwords.",
color: "red",
});
return;
}
// 4. Confirm password check
if (newPassword !== confirmPassword) {
notifications.show({
title: "Password Mismatch",
message: "Confirm password does not match new password.",
color: "red",
});
return;
}
// else if (captchaInput !== captcha) {
// setCaptchaError("Incorrect CAPTCHA.");
// return;
// }
// // Success
// notifications.show({
// title: "Success",
// message: "Your password has been reset successfully.",
// color: "green",
// });
if (captchaInput !== captcha) {
setCaptchaError("Incorrect CAPTCHA.");
return;
}
// ✅ Clear error if CAPTCHA is correct
setCaptchaError("");
// Success
notifications.show({
title: "Success",
message: "Your password has been reset successfully.",
color: "green",
});
const resetForm = () => {
setOldPassword("");
setNewPassword("");
setConfirmPassword("");
setCaptchaInput("");
setOtp("");
setOtpValidated(false);
setStep("form");
regenerateCaptcha();
};
return (
<Paper shadow="sm" radius="md" p="md" withBorder h={400}>
<Title order={3} mb="sm" >
<Paper shadow="sm" radius="md" p="md" withBorder>
<Title order={3} mb="sm">
Change Login Password
</Title>
{/* Scrollable form section */}
<div style={{ overflowY: 'auto', maxHeight: '310px', paddingRight: 8 }}>
{/* Scrollable form area */}
<div style={{ overflowY: "auto", maxHeight: "280px", paddingRight: 8 }}>
<PasswordInput
label="Old Password"
placeholder="Enter old password"
value={oldPassword}
onChange={(e) => setOldPassword(e.currentTarget.value)}
visible={showOld}
onVisibilityChange={setShowOld}
required
withAsterisk
mb="xs"
readOnly={step !== "form"}
/>
<Group grow>
<PasswordInput
label="New Password"
placeholder="Enter new password"
value={newPassword}
onChange={(e) => setNewPassword(e.currentTarget.value)}
withAsterisk
mb="xs"
readOnly={step !== "form"}
/>
<PasswordInput
label="New Password"
placeholder="Enter new password"
value={newPassword}
onChange={(e) => setNewPassword(e.currentTarget.value)}
visible={showNew}
onVisibilityChange={setShowNew}
required
mb="xs"
/>
<PasswordInput
label="Confirm Password"
placeholder="Re-enter new password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.currentTarget.value)}
withAsterisk
rightSection={icon}
mb="sm"
readOnly={step !== "form"}
onCopy={(e) => e.preventDefault()}
onPaste={(e) => e.preventDefault()}
onCut={(e) => e.preventDefault()}
/>
<PasswordInput
label="Confirm Password"
placeholder="Re-enter new password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.currentTarget.value)}
visible={showConfirm}
onVisibilityChange={setShowConfirm}
required
rightSection={icon}
mb="sm"
onCopy={(e) => e.preventDefault()}
onPaste={(e) => e.preventDefault()}
onCut={(e) => e.preventDefault()}
/>
</Group>
{/* CAPTCHA */}
<div style={{ marginTop: 5 }}>
<label style={{ display: 'block', marginBottom: 4, fontSize: '14px' }}>Enter CAPTCHA</label>
<div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 5 }}>
{/* CAPTCHA Image */}
<div style={{ display: 'flex', alignItems: 'center', height: 30 }}>
{/* <CaptchaImage text={captcha} /> */}
<div style={{ marginTop: 5 }}>
<label style={{ display: "block", marginBottom: 4, fontSize: "14px" }}>
Enter CAPTCHA <span style={{ color: "red" }}>*</span>
</label>
<div style={{ display: "flex", alignItems: "center", gap: 10, marginBottom: 5 }}>
<div
style={{
fontSize: "18px",
letterSpacing: "3px",
background: "#f3f4f6",
padding: "6px 12px",
borderRadius: "6px",
border: "1px solid #d1d5db",
userSelect: "none",
textDecoration: "line-through",
fontFamily: "cursive",
}}
>
{captcha}
</div>
{/* Refresh Button */}
<Button
size="xs"
variant="outline"
onClick={regenerateCaptcha}
style={{ height: 30, padding: '0 10px', lineHeight: '1' }}
style={{ height: 30, padding: "0 10px", lineHeight: "1" }}
disabled={step !== "form"}
>
Refresh
</Button>
{/* TextInput */}
<TextInput
placeholder="Enter above text"
value={captchaInput}
onChange={(e) => setCaptchaInput(e.currentTarget.value)}
required
style={{ height: 30, flexGrow: 1 }}
withAsterisk
style={{ flexGrow: 1 }}
readOnly={step !== "form"}
/>
</div>
<p style={{ color: captchaError ? 'red' : 'transparent', fontSize: '12px', textAlign: 'center', minHeight: 16 }}>
{captchaError || '\u00A0'}
</p>
</div>
{step !== "form" && (
<PasswordInput
label="Enter OTP"
placeholder="Enter 6-digit OTP"
value={otp}
onChange={(e) => setOtp(e.currentTarget.value)}
withAsterisk
mt="sm"
maxLength={6}
readOnly={otpValidated}
/>
)}
</div>
{/* Fixed Save Button */}
<Button onClick={handleSubmit}>
Submit
</Button>
{/* Buttons */}
<Group mt="md" gap="sm">
<Button onClick={handleSubmit}>
{step === "form" && "Submit"}
{step === "otp" && "Validate OTP"}
{step === "final" && "Change Password"}
</Button>
<Button variant="outline" color="gray" onClick={resetForm}>
Reset
</Button>
</Group>
</Paper>
);
};
export default ChangePassword;
}