Files
faq_chatbot_frontend/src/app/pages/login/login.page.ts
T
Peyman Honarjoo ff7fcbc4bb build(ui): expand application shell and implement administrative dashboard pages
This commit significantly expands the application's routing, navigation, and administrative capabilities. It introduces several new functional modules and refines the core layout for a more professional, dashboard-oriented experience.

- **Routing & Navigation**:
  - Added routes for `wallboard`, `call-center`, `call-center/calls`, and `chat-questions`.
  - Updated `SidebarComponent` to include all new navigation items.
  - Enhanced `LayoutComponent` with dynamic navigation based on user roles (admin vs. user).
- **New Administrative Pages**:
  - `WallboardPage`: Implements a real-time KPI dashboard with call activity trends and outcome distributions.
  - `CallCenterPage`: Provides a symbolic interface for managing call center connectivity and synchronization.
  - `CallCenterCallsPage`: Implements a call history retrieval interface with filtering capabilities.
  - `ChatQuestionsPage`: Adds a module for reviewing user questions from web and mobile channels.
- **Core Infrastructure & Refinement**:
  - Updated `AppConfig` to include an `APP_INITIALIZER` for validating user identity via `AuthService` on startup.
  - Refined `HomeComponent` layout to a "Claude-style" continuous chat column.
  - Implemented global themed scrollbars and integrated the 'Lahze' font family.
  - Added `proxy.conf.json` and updated `angular.json` to support local backend development.
- **Documentation**:
  - Added comprehensive API handoff guides for frontend-backend connection and MVP API catalog.
2026-10-10 00:20:00 +03:30

199 lines
5.3 KiB
TypeScript

import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Router, NavigationEnd } from '@angular/router';
import { filter, Subscription } from 'rxjs';
import { AuthService, LoginResponse } from '../../services/auth/auth.service';
type LoginField = 'username' | 'password';
type LoginErrors = Partial<Record<LoginField, string>>;
@Component({
selector: 'app-login',
standalone: true,
imports: [CommonModule, FormsModule],
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './login.page.html',
styleUrl: './login.page.css',
})
export class LoginPage {
private readonly authService = inject(AuthService);
private readonly router = inject(Router);
readonly username = signal('');
readonly password = signal('');
readonly rememberMe = signal(false);
readonly showPassword = signal(false);
readonly isLoading = signal(false);
readonly errors = signal<LoginErrors>({});
readonly generalError = signal<string | null>(null);
readonly isShaking = signal(false);
readonly isEntering = signal(true);
readonly mousePosition = signal({ x: 0, y: 0 });
private routerSubscription?: Subscription;
constructor() {
this.routerSubscription = this.router.events
.pipe(filter((event) => event instanceof NavigationEnd))
.subscribe(() => {
this.resetForm();
});
}
ngOnInit(): void {
this.restoreCredentials();
setTimeout(() => this.isEntering.set(false), 1100);
}
ngOnDestroy(): void {
this.routerSubscription?.unsubscribe();
}
private restoreCredentials(): void {
const saved = localStorage.getItem('remembered_credentials');
if (saved) {
try {
const parsed = JSON.parse(saved);
if (parsed?.username) {
this.username.set(parsed.username);
this.rememberMe.set(true);
}
} catch {
// ignore corrupt data
}
}
}
private saveCredentials(): void {
if (this.rememberMe()) {
localStorage.setItem('remembered_credentials', JSON.stringify({ username: this.username() }));
} else {
localStorage.removeItem('remembered_credentials');
}
}
onCardMouseMove(event: MouseEvent): void {
const rect = (event.currentTarget as HTMLElement).getBoundingClientRect();
const x = ((event.clientX - rect.left) / rect.width) * 100;
const y = ((event.clientY - rect.top) / rect.height) * 100;
this.mousePosition.set({ x, y });
}
onCardMouseLeave(): void {
this.mousePosition.set({ x: 50, y: 50 });
}
onFieldBlur(field: LoginField): void {
this.validateField(field);
}
private validateField(field: LoginField): void {
const currentErrors = { ...this.errors() };
switch (field) {
case 'username':
if (!this.username().trim()) {
currentErrors.username = 'نام کاربری الزامی است';
} else {
delete currentErrors.username;
}
break;
case 'password':
if (!this.password()) {
currentErrors.password = 'رمز عبور الزامی است';
} else {
delete currentErrors.password;
}
break;
}
this.errors.set(currentErrors);
}
validateAllFields(): LoginErrors {
const allErrors: LoginErrors = {};
if (!this.username().trim()) {
allErrors.username = 'نام کاربری الزامی است';
}
if (!this.password()) {
allErrors.password = 'رمز عبور الزامی است';
}
this.errors.set(allErrors);
return allErrors;
}
onSubmit(): void {
const allErrors = this.validateAllFields();
if (Object.keys(allErrors).length > 0) {
this.triggerShake();
return;
}
this.isLoading.set(true);
this.generalError.set(null);
this.errors.set({});
const credentials = {
username: this.username().trim(),
password: this.password(),
};
this.authService.login(credentials).subscribe({
next: (response: LoginResponse) => {
this.isLoading.set(false);
if (!response?.user?.username) {
this.generalError.set('نام کاربری یا رمز عبور اشتباه است');
this.triggerShake();
return;
}
this.saveCredentials();
this.router.navigate(['/']);
},
error: (err) => {
this.isLoading.set(false);
// Errors are mapped by backend code instead of showing the raw server message.
this.generalError.set(
AuthService.messageForError(
err,
'خطا در اتصال به سرور. لطفاً دوباره تلاش کنید.',
),
);
this.triggerShake();
},
});
}
private triggerShake(): void {
this.isShaking.set(true);
setTimeout(() => this.isShaking.set(false), 700);
}
togglePasswordVisibility(): void {
this.showPassword.update((v) => !v);
}
onLogoClick(): void {
this.router.navigate(['/login']);
}
private resetForm(): void {
this.username.set('');
this.password.set('');
this.rememberMe.set(false);
this.showPassword.set(false);
this.isLoading.set(false);
this.errors.set({});
this.generalError.set(null);
this.isShaking.set(false);
this.isEntering.set(false);
this.mousePosition.set({ x: 50, y: 50 });
}
}