File size: 9,185 Bytes
f8794d4 6dc3ffe f8794d4 6dc3ffe f8794d4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
import { Component, Output, EventEmitter, ChangeDetectorRef, Input, OnInit, OnDestroy } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators, AbstractControl, ValidationErrors } from '@angular/forms';
import { Router, RouterLink } from '@angular/router';
import { SignUpService } from './sign-up.service'; // Import the SignUpService
import { trigger, transition, style, animate } from '@angular/animations';
import { BrandService } from '../shared/brand.service'; // <-- Add this import
export function nameValidator(control: AbstractControl): ValidationErrors | null {
const value = control.value || '';
// Only allow alphabets and spaces, min2 chars
if (!/^[A-Za-z ]{2,}$/.test(value)) {
return { invalidName: true };
}
return null;
}
@Component({
selector: 'app-sign-up',
standalone: true,
imports: [CommonModule, ReactiveFormsModule, RouterLink],
templateUrl: './sign-up.component.html',
styleUrls: ['./sign-up.component.css'],
animations: [
trigger('fadeInOut', [
transition(':enter', [
style({ opacity:0 }),
animate('600ms', style({ opacity:1 }))
]),
transition(':leave', [
animate('600ms', style({ opacity:0 }))
])
])
]
})
export class SignUpComponent implements OnInit, OnDestroy {
@Input() embedded = false; // when true, render only inner panel (for embedding in auth-card)
@Output() switchToSignIn = new EventEmitter<void>();
form: FormGroup;
private isSubmitting = false;
// Added: state & handlers for role info popover used in template
showRoleInfo = false;
toggleRoleInfo(ev?: Event) { ev?.stopPropagation(); this.showRoleInfo = !this.showRoleInfo; }
hideRoleInfo() { this.showRoleInfo = false; }
@Output() close = new EventEmitter<void>();
showPassword = false;
showConfirmPassword = false;
errorMessage = '';
isSignUpActive = true; // ← Added state for sign-up panel activation
public loading = false; // Used to disable the button during sign-up
submitted = false; // Track form submission status
// Added: terms & conditions error handling
termsError: string = '';
facts: string[] = [
'🎯 Master grammar with adaptive quizzes.',
'📖 Build comprehension with AI reading practice.',
'🗣️ Improve listening and pronunciation with feedback.'
];
currentFact: string = this.facts[0];
private factIndex =0;
private factInterval: any;
showInfo = false;
constructor(
private fb: FormBuilder,
private router: Router,
private signUpService: SignUpService,
private cdr: ChangeDetectorRef,
public brand: BrandService
) {
this.form = this.fb.group({
name: ['', [Validators.required, Validators.minLength(2), nameValidator]],
lastName: ['', [Validators.required, Validators.minLength(2), nameValidator]],
email: ['', [
Validators.required,
Validators.pattern(/(^[^@]+@[^@]+\.[^@]+$)|(^\+?\d[\d\-\s]{8,14}\d$)/)
]],
password: ['', [Validators.required, Validators.minLength(8), passwordPolicyValidator]],
confirmPassword: ['', [Validators.required]],
role: ['', [Validators.required]],
terms: [false, Validators.requiredTrue] // Added terms control with requiredTrue validator
}, { validators: [this.passwordsMatchValidator] });
// Close popover when clicking anywhere in document (capture phase not needed here)
document.addEventListener('click', () => this.hideRoleInfo());
}
ngOnInit() {
this.startFactRotation();
}
ngOnDestroy() {
if (this.factInterval) {
clearInterval(this.factInterval);
}
}
startFactRotation() {
this.factInterval = setInterval(() => {
this.factIndex = (this.factIndex +1) % this.facts.length;
this.currentFact = this.facts[this.factIndex];
},5000);
}
control(path: string): AbstractControl | null { return this.form.get(path); }
controlHasError(path: string, error?: string): boolean {
const c = this.control(path);
if (!c) return false;
return error ? !!(c.touched && c.errors?.[error]) : !!(c.touched && c.invalid);
}
showPwdMismatch(): boolean {
const pw = this.control('password');
const cpw = this.control('confirmPassword');
const groupMismatch = this.form.errors?.['passwordMismatch'];
return !!(pw && cpw && (cpw.touched || pw.touched) && groupMismatch);
}
passwordsMatchValidator(group: AbstractControl) {
const pw = group.get('password')?.value;
const cpw = group.get('confirmPassword')?.value;
return pw && cpw && pw === cpw ? null : { passwordMismatch: true };
}
togglePasswordVisibility() {
this.showPassword = !this.showPassword;
}
toggleConfirmPasswordVisibility() {
this.showConfirmPassword = !this.showConfirmPassword;
}
submit() {
// Confirm button click
alert("Sign-Up button clicked!");
console.log("Sign-Up button clicked!");
this.submitted = true; // Track the form submission attempt
// Mark all form controls as touched to trigger validation
this.form.markAllAsTouched();
// Check if the form is invalid
if (this.form.invalid) {
console.log("Form is invalid:");
// Log individual control states for debugging
Object.keys(this.form.controls).forEach(controlName => {
const control = this.form.get(controlName);
if (control?.invalid) {
console.log(`${controlName} is invalid. Errors:`, control?.errors);
}
});
return;
}
// Check terms & conditions acceptance
if (!this.form.get('terms')?.value) {
this.termsError = 'Please accept Terms & Conditions.';
return;
}
this.termsError = '';
this.loading = true; // Set loading to true when starting submission
try {
// Prepare the payload to send to the backend
const payload = {
name: this.control('name')?.value,
lastName: this.control('lastName')?.value,
email: this.control('email')?.value,
password: this.control('password')?.value,
role: this.control('role')?.value
};
console.log("Form data before submission:", this.form.value);
console.log("Payload to send:", payload);
// Make the HTTP request
this.signUpService.signUp(payload).subscribe(
(response) => {
this.errorMessage = '';
console.log("Sign-up request sent successfully!");
this.loading = false; // Reset loading on success
// On success: if embedded inside SignIn card, flip back to sign-in; else go to /login
if (this.embedded) {
this.switchToSignIn.emit();
} else {
this.router.navigate(['/login']);
}
},
(error) => {
if (error && error.status === 400) {
this.errorMessage = 'This email or username is already registered.';
} else {
this.errorMessage = 'An error occurred. Please try again.';
}
this.loading = false; // Reset loading on error
this.cdr.markForCheck();
setTimeout(() => {
this.errorMessage = '';
this.cdr.markForCheck();
}, 3000);
}
);
} catch (error) {
console.error("Error occurred during sign-up:", error); // Log any errors from the API call
this.loading = false; // Reset loading on exception
}
}
navigateHome() { this.router.navigateByUrl('/'); }
goToLogin() {
this.switchToSignIn.emit(); // ← Emit event instead of router navigation
}
closePopup() {
try {
// dispatch a global event so parent or other listeners always can close modals
window.dispatchEvent(new CustomEvent('auth-close'));
} catch (e) {
// ignore
}
this.close.emit();
// Defensive: remove modal/backdrop if parent didn't hide them
try {
const modal = document.querySelector('.modal');
if (modal && modal.parentElement) modal.parentElement.removeChild(modal);
const backdrop = document.querySelector('.modal-backdrop');
if (backdrop && backdrop.parentElement) backdrop.parentElement.removeChild(backdrop);
} catch (e) {
console.warn('Failed to remove modal/backdrop DOM elements', e);
}
// Ensure change detection updates
this.cdr.markForCheck();
// Navigate to home after closing
this.router.navigate(['/home']);
}
tr(key: string): string {
const map: Record<string, string> = { title: 'Create your account', subtitle: 'Join to continue' };
return map[key] || '';
}
goToSignIn() {
// Emit to parent when embedded so the card can slide back
this.switchToSignIn.emit();
}
goToSignUp() {
// no-op when embedded
}
}
function passwordPolicyValidator(control: AbstractControl): ValidationErrors | null {
const value = control.value || '';
// Policy: min 8 chars, 1 uppercase, 1 lowercase, 1 number, 1 special char
const policy = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=[\]{};':"\\|,.<>\/?]).{8,}$/;
if (!policy.test(value)) {
return { passwordPolicy: true };
}
return null;
}
|