({});
+ // di atas, bareng useState lainnya
+ const [isVisible, setIsVisible] = useState(false);
+ const [isVisibleSetup, setIsVisibleSetup] = useState([false, false]);
+
+ // fungsi helper
+ const toggleVisibility = () => setIsVisible(!isVisible);
// data user lengkap
const [fullname, setFullname] = useState("");
@@ -54,6 +82,15 @@ export default function SignUp() {
console.error("Error send otp:", err);
MySwal.fire("Error", "Terjadi kesalahan server", "error");
}
+
+ // If role is tenant, handle tenant registration directly
+ if (role === "tenant") {
+ handleTenantRegistration(e);
+ return;
+ }
+
+ // For other roles, proceed with OTP flow
+ setStep("otp");
};
// 🔹 Handle OTP Verification (dummy → lanjut form)
@@ -73,26 +110,25 @@ export default function SignUp() {
const handleRegister = async (e: React.FormEvent) => {
e.preventDefault();
- const payload = {
- address,
- clientId: "web-app", // contoh default
- dateOfBirth,
- email,
- fullName: fullname, // 🔥 fix disini (bukan fullname)
- genderType,
- identityGroup: "",
- identityGroupNumber: "",
- identityNumber: "",
- identityType: "",
- lastEducation: "",
- password,
- phoneNumber,
- userLevelId: 1,
- userRoleId: 1,
- username,
- workType,
-};
-
+ const payload = {
+ address,
+ clientId: "web-app", // contoh default
+ dateOfBirth,
+ email,
+ fullName: fullname, // 🔥 fix disini (bukan fullname)
+ genderType,
+ identityGroup: "",
+ identityGroupNumber: "",
+ identityNumber: "",
+ identityType: "",
+ lastEducation: "",
+ password,
+ phoneNumber,
+ userLevelId: 1,
+ userRoleId: 1,
+ username,
+ workType,
+ };
try {
const res = await createUser(payload);
@@ -117,6 +153,124 @@ export default function SignUp() {
if (value && nextInput) nextInput.focus();
};
+ // Form validation functions
+ const validateEmail = (email: string) => {
+ const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
+ return emailRegex.test(email);
+ };
+
+ const validatePhone = (phone: string) => {
+ const phoneRegex = /^[0-9+\-\s()]+$/;
+ return phoneRegex.test(phone) && phone.length >= 10;
+ };
+
+ const validatePassword = (password: string) => {
+ return password.length >= 8;
+ };
+
+ const validateTenantForm = () => {
+ const errors: any = {};
+
+ if (!firstName.trim()) {
+ errors.firstName = "First name is required";
+ }
+
+ if (!lastName.trim()) {
+ errors.lastName = "Last name is required";
+ }
+
+ if (!email.trim()) {
+ errors.email = "Email is required";
+ } else if (!validateEmail(email)) {
+ errors.email = "Please enter a valid email address";
+ }
+
+ if (!whatsapp.trim()) {
+ errors.whatsapp = "WhatsApp number is required";
+ } else if (!validatePhone(whatsapp)) {
+ errors.whatsapp = "Please enter a valid phone number";
+ }
+
+ if (!namaTenant.trim()) {
+ errors.namaTenant = "Tenant name is required";
+ }
+
+ if (!tenantPassword) {
+ errors.tenantPassword = "Password is required";
+ } else if (!validatePassword(tenantPassword)) {
+ errors.tenantPassword = "Password must be at least 8 characters";
+ }
+
+ if (!confirmTenantPassword) {
+ errors.confirmTenantPassword = "Please confirm your password";
+ } else if (tenantPassword !== confirmTenantPassword) {
+ errors.confirmTenantPassword = "Passwords do not match";
+ }
+
+ setFormErrors(errors);
+ return Object.keys(errors).length === 0;
+ };
+
+ const handleTenantRegistration = async (e: React.FormEvent) => {
+ e.preventDefault();
+
+ if (!validateTenantForm()) {
+ return;
+ }
+
+ setIsLoading(true);
+ setFormErrors({});
+
+ try {
+ const registrationData = {
+ adminUser: {
+ address: "Jakarta", // Default address as per API requirement
+ email: email,
+ fullname: `${firstName} ${lastName}`,
+ password: tenantPassword,
+ phoneNumber: whatsapp,
+ username: `${firstName}-${lastName}`, // Using firstName + lastName as username
+ },
+ client: {
+ clientType: "sub_client", // Hardcoded as per API requirement
+ name: namaTenant,
+ parentClientId: "78356d32-52fa-4dfc-b836-6cebf4e3eead", // Hardcoded as per API requirement
+ },
+ };
+
+ const response = await registerTenant(registrationData);
+
+ if (response.error) {
+ MySwal.fire({
+ title: "Registration Failed",
+ text: response.message || "An error occurred during registration",
+ icon: "error",
+ confirmButtonText: "OK",
+ });
+ } else {
+ MySwal.fire({
+ title: "Registration Successful",
+ text: "Your tenant account has been created successfully!",
+ icon: "success",
+ confirmButtonText: "OK",
+ }).then(() => {
+ // Redirect to login page or dashboard
+ router.push("/auth");
+ });
+ }
+ } catch (error) {
+ console.error("Registration error:", error);
+ MySwal.fire({
+ title: "Registration Failed",
+ text: "An unexpected error occurred. Please try again.",
+ icon: "error",
+ confirmButtonText: "OK",
+ });
+ } finally {
+ setIsLoading(false);
+ }
+ };
+
return (
{/* Left Side Logo */}
@@ -185,12 +339,6 @@ export default function SignUp() {
{/* Step 3: Form Lengkap */}
{step === "form" && (