Skip to content
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
15 changes: 12 additions & 3 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ListComponent } from './contacts/list/list.component';
import { AddComponent } from './contacts/add/add.component';
import { ViewComponent } from './contacts/view/view.component';
import { EditComponent } from './contacts/edit/edit.component';

const routes: Routes = [];
const routes: Routes = [
{ path: 'contacts', component: ListComponent },
{ path: 'contacts/add', component: AddComponent },
{ path: 'contacts/:id', component: ViewComponent },
{ path: 'contacts/edit/:id', component: EditComponent },
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
exports: [RouterModule],
})
export class AppRoutingModule { }
export class AppRoutingModule {}
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LayoutModule } from './layout/layout.module';

import { ContactsModule } from './contacts/contacts.module';

@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AppRoutingModule, LayoutModule],
imports: [BrowserModule, AppRoutingModule, LayoutModule, ContactsModule],
bootstrap: [AppComponent],
})
export class AppModule {}
25 changes: 25 additions & 0 deletions src/app/contacts/add/add.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
:host {
display: flex;
box-sizing: border-box;
width: 40vw;
flex-direction: column;
}

form {
display: flex;
flex-direction: column;
flex: 1;
gap: 0.5em;
}

label {
font-weight: bold;
}

.actions {
display: flex;
}

.spacer {
flex: 1;
}
52 changes: 52 additions & 0 deletions src/app/contacts/add/add.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<div>
<h2>Add Beer</h2>
<form [formGroup]="contactForm" (ngSubmit)="addContact()" class="form-group">
<div>
<label for="firstName" class="form-control">First Name:</label>
<input
type="text"
id="firstName"
formControlName="firstName"
class="form-control"
/>
</div>

<div>
<label for="lastName" class="form-control">last Name:</label>
<input
type="text"
id="lastName"
formControlName="lastName"
class="form-control"
/>
</div>

<div>
<label for="street" class="form-control">Street:</label>
<input
type="text"
id="street"
formControlName="street"
class="form-control"
/>
</div>

<div>
<label for="city" class="form-control">City:</label>
<input
type="text"
id="city"
formControlName="city"
class="form-control"
/>
</div>

<button
type="submit"
[disabled]="contactForm.invalid"
class="btn btn-success"
>
Add Contact
</button>
</form>
</div>
23 changes: 23 additions & 0 deletions src/app/contacts/add/add.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { AddComponent } from './add.component';

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

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

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

it('should create', () => {
expect(component).toBeTruthy();
});
});
39 changes: 39 additions & 0 deletions src/app/contacts/add/add.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Component } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { ContactsService } from '../contacts.service';
import { Contact } from '../../models/contact';
import { Router } from '@angular/router';

@Component({
selector: 'app-add',
standalone: false,
templateUrl: './add.component.html',
styleUrl: './add.component.css',
})
export class AddComponent {
contactForm: FormGroup;
constructor(
private readonly formBuilder: FormBuilder,
private readonly contactService: ContactsService,
private readonly router: Router
) {
this.contactForm = this.formBuilder.group({
firstName: ['', Validators.required],
lastName: ['', Validators.required],
street: ['', Validators.required],
city: ['', Validators.required],
});
}
addContact(): void {
const newContact: Contact = {
id: 0,
firstName: this.contactForm.value.firstName,
lastName: this.contactForm.value.lastName,
street: this.contactForm.value.street,
city: this.contactForm.value.city,
};
this.contactService.AddContact(newContact);
this.contactForm.reset();
this.router.navigate(['/contacts']);
}
}
16 changes: 16 additions & 0 deletions src/app/contacts/contacts.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ListComponent } from './list/list.component';
import { AddComponent } from './add/add.component';
import { ViewComponent } from './view/view.component';
import { RouterModule } from '@angular/router';
import { ReactiveFormsModule } from '@angular/forms';

import { EditComponent } from './edit/edit.component';

@NgModule({
declarations: [ListComponent, AddComponent, ViewComponent, EditComponent],
imports: [CommonModule, ReactiveFormsModule, RouterModule],
exports: [AddComponent, ListComponent, ViewComponent, EditComponent],
})
export class ContactsModule {}
31 changes: 31 additions & 0 deletions src/app/contacts/contacts.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Injectable } from '@angular/core';
import { Contact } from '../models/contact';
import { CONTACTS } from '../data/contacts';
import { Observable, of } from 'rxjs';

@Injectable({
providedIn: 'root',
})
export class ContactsService {
public contacts: Contact[] = CONTACTS;

public AddContact(contact: Contact): void {
const maxId =
this.contacts.length > 0
? Math.max(...this.contacts.map((c) => c.id))
: 0;

const newContact = { ...contact, id: maxId + 1 };
this.contacts.push(newContact);
}
GetContactById(id: number): Observable<Contact | undefined> {
const contact = this.contacts.find((c) => c.id === id);
return of(contact);
}
public UpdateContact(updatedContact: Contact): void {
const index = this.contacts.findIndex((c) => c.id === updatedContact.id);
if (index !== -1) {
this.contacts[index] = updatedContact;
}
}
}
25 changes: 25 additions & 0 deletions src/app/contacts/edit/edit.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
:host {
display: flex;
box-sizing: border-box;
width: 40vw;
flex-direction: column;
}

form {
display: flex;
flex-direction: column;
flex: 1;
gap: 0.5em;
}

label {
font-weight: bold;
}

.actions {
display: flex;
}

.spacer {
flex: 1;
}
62 changes: 62 additions & 0 deletions src/app/contacts/edit/edit.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<div *ngIf="contact; else loading">
<h2>Edit Contact</h2>
<form
[formGroup]="contactForm"
(ngSubmit)="updateContact()"
class="form-group"
>
<div>
<label for="firstName" class="form-control">First Name:</label>
<input
type="text"
id="firstName"
formControlName="firstName"
class="form-control"
/>
</div>

<div>
<label for="lastName" class="form-control">Last Name:</label>
<input
type="text"
id="lastName"
formControlName="lastName"
class="form-control"
/>
</div>

<div>
<label for="street" class="form-control">Street:</label>
<input
type="text"
id="street"
formControlName="street"
class="form-control"
/>
</div>

<div>
<label for="city" class="form-control">City:</label>
<input
type="text"
id="city"
formControlName="city"
class="form-control"
/>
</div>

<button
type="submit"
[disabled]="contactForm.invalid"
class="btn btn-primary"
>
Save Changes
</button>
</form>
</div>

<ng-template #loading>
<ng-container>
<h1>Contact not found</h1>
</ng-container>
</ng-template>
23 changes: 23 additions & 0 deletions src/app/contacts/edit/edit.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { EditComponent } from './edit.component';

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

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

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

it('should create', () => {
expect(component).toBeTruthy();
});
});
57 changes: 57 additions & 0 deletions src/app/contacts/edit/edit.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { ContactsService } from '../contacts.service';
import { Contact } from '../../models/contact';

@Component({
selector: 'app-edit',
standalone: false,
templateUrl: './edit.component.html',
styleUrls: ['./edit.component.css'],
})
export class EditComponent implements OnInit {
contactForm: FormGroup;
contactId!: number;
contact: Contact | null = null;

constructor(
private readonly fb: FormBuilder,
private readonly route: ActivatedRoute,
private readonly router: Router,
private readonly contactService: ContactsService
) {
this.contactForm = this.fb.group({
firstName: ['', Validators.required],
lastName: ['', Validators.required],
street: ['', Validators.required],
city: ['', Validators.required],
});
}

ngOnInit(): void {
this.contactId = Number(this.route.snapshot.paramMap.get('id'));
this.contactService.GetContactById(this.contactId).subscribe((data) => {
this.contact = data!;
});

if (this.contact) {
this.contactForm.patchValue({
firstName: this.contact.firstName,
lastName: this.contact.lastName,
street: this.contact.street,
city: this.contact.city,
});
}
}

updateContact(): void {
const updatedContact: Contact = {
id: this.contactId,
...this.contactForm.value,
};

this.contactService.UpdateContact(updatedContact);
this.router.navigate(['/contacts']);
}
}
Loading