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

Signup #23

Merged
merged 6 commits into from
Oct 10, 2024
Merged
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
3,472 changes: 1,736 additions & 1,736 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/icons/error-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion public/icons/valid-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions src/app/core/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { TestBed } from '@angular/core/testing';

import { AuthService } from './auth.service';
import { provideHttpClientTesting } from '@angular/common/http/testing';
import { provideHttpClient } from '@angular/common/http';

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

beforeEach(() => {
TestBed.configureTestingModule({
providers: [provideHttpClientTesting(), provideHttpClient()],
});
service = TestBed.inject(AuthService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
15 changes: 15 additions & 0 deletions src/app/core/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { SignupPayload } from './models/signup-payload.model';

@Injectable({
providedIn: 'root',
})
export class AuthService {
readonly #http = inject(HttpClient);

public signup(payload: SignupPayload): Observable<void> {
return this.#http.post<void>('/auth/signup', payload);
}
}
5 changes: 5 additions & 0 deletions src/app/core/auth/models/signup-payload.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface SignupPayload {
username: string;
email: string;
password: string;
}
39 changes: 39 additions & 0 deletions src/app/core/auth/signup/signup.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
form {
display: flex;
flex-direction: column;
align-items: stretch;
}

.btn-submit {
font-weight: 700;
margin-block-start: 1rem;
}

.password-strength-container {
display: flex;
gap: 0.5rem;
justify-content: space-between;
align-items: center;
}

.level-list {
display: flex;
justify-content: space-between;
gap: 0.3rem;
width: 100%;
}

.level {
height: 0.3rem;
width: 100%;
}

.level-active {
background-color: var(--success-color);
}

.password-strength-label {
font-size: 0.7rem;
min-width: 4rem;
text-align: end;
}
36 changes: 36 additions & 0 deletions src/app/core/auth/signup/signup.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<form [formGroup]="form" [isOptionalEnforced]="true" (ngSubmit)="submit()">
<app-form-field label="Username">
<input app-field type="text" formControlName="username" />
</app-form-field>
<app-form-field label="Email">
<input app-field type="text" formControlName="email" />
@if (form.controls.email.hasError("email")) {
<app-error-message>Invalid email format</app-error-message>
}
</app-form-field>
<app-form-field label="Password">
<input app-field type="password" formControlName="password" />
<app-hint-message class="password-strength-container">
@let passwordStrength = passwordStrength$ | async;
<div class="level-list">
@for (level of passwordLevels; track level; let index = $index) {
<div
class="level"
[class.level-active]="index <= passwordStrength"
></div>
}
</div>
<div class="password-strength-label">
{{ passwordLevels[passwordStrength] }}
</div>
</app-hint-message>
</app-form-field>
<app-form-field label="Confirm password">
<input app-field type="password" formControlName="confirmPassword" />
@if (form.controls.confirmPassword.hasError("passwordMismatch")) {
<app-error-message>Passwords do not match</app-error-message>
}
</app-form-field>

<button type="submit" class="btn btn-primary btn-submit">Sign up</button>
</form>
22 changes: 22 additions & 0 deletions src/app/core/auth/signup/signup.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { SignupComponent } from './signup.component';

describe('SignupComponent', () => {
let component: SignupComponent;
let fixture: ComponentFixture<SignupComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [SignupComponent],
}).compileComponents();

fixture = TestBed.createComponent(SignupComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
102 changes: 102 additions & 0 deletions src/app/core/auth/signup/signup.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { ChangeDetectionStrategy, Component, output } from '@angular/core';
import {
AbstractControl,
FormControl,
FormGroup,
ReactiveFormsModule,
ValidationErrors,
ValidatorFn,
Validators,
} from '@angular/forms';
import { FormFieldComponent } from '../../../shared/components/form-field/form-field.component';
import { FieldDirective } from '../../../shared/directives/field.directive';
import { ErrorMessageDirective } from '../../../shared/directives/error-message.directive';
import { FormDirective } from '../../../shared/directives/form.directive';
import { map } from 'rxjs';
import { AsyncPipe } from '@angular/common';
import { SignupPayload } from '../models/signup-payload.model';
import { HintMessageDirective } from '../../../shared/directives/hint-message.directive';

interface FormType {
username: FormControl<string>;
email: FormControl<string>;
password: FormControl<string>;
confirmPassword: FormControl<string>;
}

@Component({
selector: 'app-signup',
standalone: true,
imports: [
ReactiveFormsModule,
FormFieldComponent,
FieldDirective,
ErrorMessageDirective,
FormDirective,
AsyncPipe,
HintMessageDirective,
],
templateUrl: './signup.component.html',
styleUrl: './signup.component.css',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SignupComponent {
readonly submitForm = output<SignupPayload>();

form = new FormGroup<FormType>({
username: new FormControl('', {
validators: [Validators.required],
nonNullable: true,
}),
email: new FormControl('', {
validators: [Validators.required, Validators.email], // TODO add custom pattern instead of email
nonNullable: true,
}),
password: new FormControl('', {
validators: [Validators.required],
nonNullable: true,
}),
confirmPassword: new FormControl('', {
validators: [Validators.required, this.validatePasswordMatch()],
nonNullable: true,
}),
});

passwordLevels = ['weak', 'fair', 'good', 'strong', 'very strong'];
passwordStrength$ = this.form.controls.password.valueChanges.pipe(
map((password) => {
let strength = 0;

if (password.length >= 8) strength++;
if (/[A-Z]/.test(password)) strength++;
if (/[a-z]/.test(password)) strength++;
if (/\d/.test(password)) strength++;
if (/[\W_]/.test(password)) strength++;

return strength;
}),
);

submit(): void {
this.form.markAllAsTouched();

if (this.form.valid) {
this.submitForm.emit(this.form.value as SignupPayload);
}
}

validatePasswordMatch(): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
if (!control.parent) {
return null;
}

const form = control.parent as FormGroup<FormType>;

const password = form.value.password;
const confirmPassword = form.value.confirmPassword;

return password === confirmPassword ? null : { passwordMismatch: true };
};
}
}
3 changes: 0 additions & 3 deletions src/app/core/layout/header/header.component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { RouterLink, RouterLinkActive } from '@angular/router';
import { NgOptimizedImage } from '@angular/common';

@Component({
selector: 'app-header',
standalone: true,
imports: [RouterLink, NgOptimizedImage, RouterLinkActive],
templateUrl: './header.component.html',
styleUrl: './header.component.css',
changeDetection: ChangeDetectionStrategy.OnPush,
Expand Down
2 changes: 1 addition & 1 deletion src/app/features/about/pages/about/about.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { AboutComponent } from './about.component';
import AboutComponent from './about.component';

describe('AboutComponent', () => {
let component: AboutComponent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { CommunityListComponent } from './community-list.component';
import CommunityListComponent from './community-list.component';

describe('CommunityListComponent', () => {
let component: CommunityListComponent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { EventListComponent } from './event-list.component';
import EventListComponent from './event-list.component';

describe('EventListComponent', () => {
let component: EventListComponent;
Expand Down
14 changes: 5 additions & 9 deletions src/app/shared/components/form-field/form-field.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
.label-container {
display: flex;
flex-direction: column;
gap: 0.5rem;
gap: 0.3rem;
}

.label-text {
Expand All @@ -18,7 +18,9 @@
.error-container {
display: flex;
flex-direction: column;
min-height: 2rem;
min-height: 1.25rem;
padding: 0.4rem;
line-height: 1rem;
}

.focused {
Expand All @@ -27,7 +29,7 @@
}

.field-input {
height: 2.5rem;
height: 2rem;
border-width: 1px;
border-style: solid;
border-color: var(--neutral-color-300);
Expand All @@ -52,9 +54,3 @@
.required {
color: var(--error-color);
}

.error-message {
padding: 0.5rem;
color: var(--error-color);
line-height: 1rem;
}
31 changes: 17 additions & 14 deletions src/app/shared/components/form-field/form-field.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
<label class="label-container">
<span class="label-text">
{{ label() }}
@if (isRequired()) {
@if (isRequired() && !isOptionalEnforced()) {
<span class="required">*</span>
} @else if (!isRequired() && isOptionalEnforced()) {
<span class="optional">(Optional)</span>
}
</span>
<div
class="field-input"
[class.focused]="field().isFocus()"
[class.invalid]="control().touched && control().invalid"
>
<div class="field-input" [class.focused]="field().isFocus()">
<ng-content select="input, textarea, select"></ng-content>
<ng-content select="button"></ng-content>
<div class="field-validation">
Expand All @@ -34,15 +32,20 @@
</div>
</div>
</label>
<div class="error-container" aria-live="polite">
@if (control().touched && control().invalid) {
<!-- avoid displaying an error related to the control format if the field is empty -->
@if (!control().hasError("required")) {
<ng-content select="app-error-message"></ng-content>
}
@if (control().hasError("required")) {
<app-error-message>Required field</app-error-message>
<div class="error-container" aria-live="assertive">
@if (control().touched) {
@if (control().invalid) {
@if (!control().hasError("required")) {
<ng-content select="app-error-message"></ng-content>
}
@if (control().hasError("required")) {
<app-error-message>Required field</app-error-message>
}
}
}

@if (!control().hasError("required") && !extraErrorMessages().length) {
<ng-content select="app-hint-message"></ng-content>
}
</div>
</fieldset>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { FormFieldComponent } from './form-field.component';
import { FormDirective } from '../../directives/form.directive';

describe('FormFieldComponent', () => {
let component: FormFieldComponent;
Expand All @@ -9,6 +10,7 @@ describe('FormFieldComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [FormFieldComponent],
providers: [FormDirective],
}).compileComponents();

fixture = TestBed.createComponent(FormFieldComponent);
Expand Down
Loading
Loading