Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement validations in the login form #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<main>
<section class="form-section">
<img src="/assets/svg/logo.svg"/>
<h2>{{ title }}</h2>
<ng-content></ng-content>
<div class="btn-wrapper">
<button
class="btn-primary"
(click)="submit()"
[disabled]="disablePrimaryBtn"
<h2>{{ title }}</h2>
<ng-content></ng-content>
<div class="btn-wrapper">
<button
class="btn-primary"
(click)="submit()"
>
{{ primaryBtnText }}
</button>
Expand All @@ -17,7 +16,7 @@ <h2>{{ title }}</h2>
<div></div>
</div>
<button class="btn-secondary" (click)="navigate()">{{ secondaryBtnText }}</button>
</div>
</div>
</section>
<section class="main-section">
<h1>You Should, MoveIt!</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export class DefaultLoginLayoutComponent {
@Input() title: string = "";
@Input() primaryBtnText: string = "";
@Input() secondaryBtnText: string = "";
@Input() disablePrimaryBtn: boolean = true;
@Output("submit") onSubmit = new EventEmitter();

@Output("navigate") onNavigate = new EventEmitter();
Expand Down
10 changes: 5 additions & 5 deletions src/app/components/primary-input/primary-input.component.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<div class="input-wrapper">
<label [for]="inputName">{{ label }}</label>
<div class="input-content">
<input
[type]="type"
[placeholder]="placeholder"
[value]="value"
(input)="onInput($event)"
<input
[type]="type"
[placeholder]="placeholder"
[value]="value"
(input)="onInput($event)"
/>
<div class="icon">
<ng-content></ng-content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,4 @@ export class PrimaryInputComponent implements ControlValueAccessor {
this.onTouched = fn
}

setDisabledState(isDisabled: boolean): void {}
}
39 changes: 20 additions & 19 deletions src/app/pages/login/login.component.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
<app-default-login-layout
title="Login into your account"
primaryBtnText="Login Now"
secondaryBtnText="Signup Now"
(submit)="submit()"
(navigate)="navigate()"
[disablePrimaryBtn]="!loginForm.valid"
title="Login into your account"
primaryBtnText="Login Now"
secondaryBtnText="Signup Now"
(submit)="submit()"
(navigate)="navigate()"
>
<form [formGroup]="loginForm">
<app-primary-input
formControlName="email"
inputName="email"
type="email"
label="Email"
placeholder="[email protected]"
formControlName="email"
inputName="email"
type="email"
label="Email"
placeholder="[email protected]"
>
<img src="/assets/svg/email-icon.svg"/>
</app-primary-input>

<app-primary-input
formControlName="password"
inputName="password"
type="password"
label="Your Password"
>
<img src="/assets/svg/password-icon.svg"/>
</app-primary-input>
<span>Forgot password?</span>
formControlName="password"
inputName="password"
type="password"
label="Your Password">
<img src="/assets/svg/password-icon.svg"/>
</app-primary-input>
<div class="mensage-error-class" *ngIf="showErroMenssage">* Fill in the email and password correctly</div>

<span>Forgot password?</span>
</form>
</app-default-login-layout>
31 changes: 19 additions & 12 deletions src/app/pages/login/login.component.scss
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
@import "../../../styles/variables.scss";

form {
width: 100%;
display: flex;
flex-direction: column;
gap: 24px;
margin-bottom: 34px;
width: 100%;
display: flex;
flex-direction: column;
gap: 24px;
margin-bottom: 34px;

span {
font-size: 14px;
text-decoration: underline;
color: $secondary-color;
text-align: end;
}
}
span {
font-size: 14px;
text-decoration: underline;
color: $secondary-color;
text-align: end;
}

.mensage-error-class {
margin-top: -15px;
padding-left: 5px;
font-size: smaller;
color: red;
}
}
98 changes: 57 additions & 41 deletions src/app/pages/login/login.component.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,68 @@
import { Component } from '@angular/core';
import { DefaultLoginLayoutComponent } from '../../components/default-login-layout/default-login-layout.component';
import { FormControl, FormGroup, FormRecord, ReactiveFormsModule, Validators } from '@angular/forms';
import { PrimaryInputComponent } from '../../components/primary-input/primary-input.component';
import { Router } from '@angular/router';
import { LoginService } from '../../services/login.service';
import { ToastrService } from 'ngx-toastr';
import {Component} from '@angular/core';
import {DefaultLoginLayoutComponent} from '../../components/default-login-layout/default-login-layout.component';
import {FormControl, FormControlStatus, FormGroup, FormRecord, ReactiveFormsModule, Validators} from '@angular/forms';
import {PrimaryInputComponent} from '../../components/primary-input/primary-input.component';
import {Router} from '@angular/router';
import {LoginService} from '../../services/login.service';
import {ToastrService} from 'ngx-toastr';
import {NgIf} from "@angular/common";

interface LoginForm {
email: FormControl,
password: FormControl
email: FormControl,
password: FormControl
}

@Component({
selector: 'app-login',
standalone: true,
imports: [
DefaultLoginLayoutComponent,
ReactiveFormsModule,
PrimaryInputComponent
],
providers: [
LoginService
],
templateUrl: './login.component.html',
styleUrl: './login.component.scss'
selector: 'app-login',
standalone: true,
imports: [
DefaultLoginLayoutComponent,
ReactiveFormsModule,
PrimaryInputComponent,
NgIf
],
providers: [
LoginService
],
templateUrl: './login.component.html',
styleUrl: './login.component.scss'
})
export class LoginComponent {
loginForm!: FormGroup<LoginForm>;
loginForm!: FormGroup<LoginForm>;
showErroMenssage: boolean = false;

constructor(
private router: Router,
private loginService: LoginService,
private toastService: ToastrService
){
this.loginForm = new FormGroup({
email: new FormControl('', [Validators.required, Validators.email]),
password: new FormControl('', [Validators.required, Validators.minLength(6)])
})
}
constructor(
private router: Router,
private loginService: LoginService,
private toastService: ToastrService
) {
this.loginForm = new FormGroup({
email: new FormControl('', [Validators.required, Validators.email]),
password: new FormControl('', [Validators.required, Validators.minLength(6)])
})
}

submit(){
this.loginService.login(this.loginForm.value.email, this.loginForm.value.password).subscribe({
next: () => this.toastService.success("Login feito com sucesso!"),
error: () => this.toastService.error("Erro inesperado! Tente novamente mais tarde")
})
}
submit() {
if (this.validate()) {
this.loginService.login(this.loginForm.value.email, this.loginForm.value.password).subscribe({
next: () => this.toastService.success("Login feito com sucesso!"),
error: () => this.toastService.error("Erro inesperado! Tente novamente mais tarde")
})
}
}

navigate(){
this.router.navigate(["signup"])
}
validate() {
if (this.loginForm.status == 'VALID') {
this.showErroMenssage = false;
return true;
} else {
this.showErroMenssage = true;
return false;
}
}


navigate() {
this.router.navigate(["signup"])
}
}
63 changes: 33 additions & 30 deletions src/app/pages/signup/signup.component.html
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
<app-default-login-layout
title="Sign in and start today!t"
primaryBtnText="Signup Now"
secondaryBtnText="Login Now"
(submit)="submit()"
(navigate)="navigate()"
[disablePrimaryBtn]="!signupForm.valid"
title="Sign in and start today!t"
primaryBtnText="Signup Now"
secondaryBtnText="Login Now"
(submit)="submit()"
(navigate)="navigate()"
>
<form [formGroup]="signupForm">
<app-primary-input
formControlName="name"
inputName="name"
type="text"
label="Your name"
placeholder="John Doe"
formControlName="name"
inputName="name"
type="text"
label="Your name"
placeholder="John Doe"
>
<img src="/assets/svg/email-icon.svg"/>
</app-primary-input>
<app-primary-input
formControlName="email"
inputName="email"
type="email"
label="Email"
placeholder="[email protected]"
formControlName="email"
inputName="email"
type="email"
label="Email"
placeholder="[email protected]"
>
<img src="/assets/svg/email-icon.svg"/>
</app-primary-input>
<div class="mensage-error-class" *ngIf="showMenssageErrorEmail">* Fill in the email correctly</div>

<app-primary-input
formControlName="password"
inputName="password"
type="password"
label="Your Password"
formControlName="password"
inputName="password"
type="password"
label="Your Password"
>
<img src="/assets/svg/password-icon.svg"/>
</app-primary-input>
<app-primary-input
formControlName="passwordConfirm"
inputName="passwordConfirm"
type="password"
label="Confirm your Password"
>
<img src="/assets/svg/password-icon.svg"/>
</app-primary-input>
<img src="/assets/svg/password-icon.svg"/>
</app-primary-input>
<app-primary-input
formControlName="passwordConfirm"
inputName="passwordConfirm"
type="password"
label="Confirm your Password"
>
<img src="/assets/svg/password-icon.svg"/>
</app-primary-input>
<div class="mensage-error-class" *ngIf="showMenssageErrorPassword">* password correctly</div>

</form>
</app-default-login-layout>
9 changes: 8 additions & 1 deletion src/app/pages/signup/signup.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ form {
color: $secondary-color;
text-align: end;
}
}

.mensage-error-class {
margin-top: -15px;
padding-left: 5px;
font-size: smaller;
color: red;
}
}
Loading