Skip to content

Commit

Permalink
start login modal
Browse files Browse the repository at this point in the history
  • Loading branch information
sacom14 committed Feb 22, 2024
1 parent 13597ae commit 8966499
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 43 deletions.
76 changes: 34 additions & 42 deletions src/app/modules/modals/login-modal/login-modal.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,72 +5,64 @@ import { FormBuilder, Validators } from '@angular/forms';
import { AuthService } from './../../../services/auth.service';
import { Router } from '@angular/router';
import { User } from "src/app/models/user.model";
import { ValidatiorsService } from 'src/app/services/validatiors.service';

@Component({
selector: 'app-login-modal',
templateUrl: './login-modal.component.html',
styleUrls: ['./login-modal.component.scss']
})
export class LoginModalComponent {
loginError: string = "";

loginForm = this.formBuilder.group({
dni: ['', Validators.required],
password: ['', Validators.required]
});

constructor(private modalService: NgbModal,
private formBuilder: FormBuilder,
private authService: AuthService,
private validatorsService: ValidatiorsService,
private router: Router) { }

loginError: string = "";

login() {
loginForm = this.formBuilder.group({
dni: ['', Validators.required],
password: ['', Validators.required]
});

public login(): void {
this.loginForm.markAllAsTouched();
if (this.loginForm.valid) {
const dni = this.loginForm.get("dni")?.value || "";
const password = this.loginForm.get("password")?.value || "";
let user: User = {
idUser: '',
dni: `${this.loginForm.get('dni')?.value}`,
password: `${this.loginForm.get('password')?.value}`,
}

console.log('*************', this.loginForm)
let registerResp = this.authService.login(user)
.then( (res) => {this.openSuccessfulLoginModal(res)})
.catch( (err) => this.notifyErrorLogin(err));
}
};
public isValidField(field: string) {
return this.validatorsService.isValidField(this.loginForm, field);
};

/* this.authService.login(dni, password).subscribe({
next: (userData) => {
console.log(userData, "login")
// actions like redirecting user to another page
},
error: (errorData) => {
console.error(errorData);
if (typeof errorData.error === 'string') {
// Si errorData.error es una cadena de texto, es probable que sea el mensaje de error.
this.loginError = errorData.error;
} else if (errorData.error && errorData.error.message) {
// Si errorData.error.message existe, asumimos que es el mensaje de error.
this.loginError = errorData.error.message;
} else {
// Si no se puede determinar el mensaje de error, mostrar un mensaje genérico.
this.loginError = 'Hubo un error en el inicio de sesión';
}
},
complete: () => {
console.log('Login Completo')
// this.router.navigateByUrl('/')
this.closeModal();
}
});*/
public openSuccessfulLoginModal(res: any) {
//TODO create routing to the page after success login
alert('Success login');
}

}
else {
// alert('error al ingresar datos')
this.loginForm.markAllAsTouched()
public notifyErrorLogin(err: any) {
if ((typeof err.message) === "string") {
this.loginError = err.message;
} else {
this.loginError = 'Error en el registro';
}
}



closeModal() {
this.modalService.dismissAll();
}
openRegisterModal(){
openRegisterModal() {
this.closeModal();
this.modalService.open(RegisterModalComponent, { centered : true, size : 'lg' })
this.modalService.open(RegisterModalComponent, { centered: true, size: 'lg' })
}
}
2 changes: 1 addition & 1 deletion src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class AuthService {
}
})
}

public register(user: User): Promise<any> {
return new Promise((resolve, reject) => {
this.registerRequest(user).subscribe({
Expand Down
16 changes: 16 additions & 0 deletions src/app/services/validatiors.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { ValidatiorsService } from './validatiors.service';

describe('ValidatiorsService', () => {
let service: ValidatiorsService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ValidatiorsService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
21 changes: 21 additions & 0 deletions src/app/services/validatiors.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@angular/core';
import { FormGroup } from '@angular/forms';

@Injectable({
providedIn: 'root'
})
export class ValidatiorsService {

//dni
public dniPattern: string = '^[a-zA-Z]{2,}(?: [a-zA-Z]+)*$';

//password
public passwordPattern: string = '^[a-zA-Z0-9]{6,}$';



//validations control
public isValidField(form: FormGroup, field: string) {
return form.controls[field].errors && form.controls[field].touched;
};
}

0 comments on commit 8966499

Please sign in to comment.