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
Empty file.
22 changes: 22 additions & 0 deletions src/app/addressbook/add/add.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div>
<h2>Add a contact</h2>
<form [formGroup]="contactForm" (ngSubmit)="addContact()">
<div>
<label for="firstName">First Name</label>
<input type="text" id="firstName" formControlName="firstName" />
</div>
<div>
<label for="lastName">Last Name</label>
<input type="text" id="lastName" formControlName="lastName" />
</div>
<div>
<label for="city">City </label>
<input type="text" id="city" formControlName="city" />
</div>
<div>
<label for="street">Street</label>
<input type="text" id="street" formControlName="street" />
</div>
<button type="submit">Add Contact</button>
</form>
</div>
31 changes: 31 additions & 0 deletions src/app/addressbook/add/add.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Component, inject } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { AddressbookService } from 'src/app/services/addressbook.service';

@Component({
selector: 'app-add',
templateUrl: './add.component.html',
styleUrl: './add.component.css'
})

export class AddComponent {
contactForm: FormGroup;
formBuilder = inject(FormBuilder);
contactService = inject(AddressbookService)
router = inject(Router)

constructor() {
this.contactForm = this.formBuilder.group({
firstName: ["", Validators.required],
lastName: ["", Validators.required],
street: ["", Validators.required],
city: ["", Validators.required]
});
}

addContact() {
this.contactService.addContact(this.contactForm.value)
this.router.navigate(['addressbook'])
}
}
31 changes: 31 additions & 0 deletions src/app/addressbook/addressbook.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AddComponent } from './add/add.component';
import { ViewComponent } from './view/view.component';
import { ListComponent } from './list/list.component';
import { EditComponent } from './edit/edit.component';
import { RouterModule } from '@angular/router';
import { ReactiveFormsModule } from '@angular/forms';



@NgModule({
declarations: [
AddComponent,
ViewComponent,
ListComponent,
EditComponent
],
imports: [
CommonModule,
RouterModule,
ReactiveFormsModule
],
exports: [
AddComponent,
ViewComponent,
ListComponent,
EditComponent
]
})
export class AddressbookModule { }
18 changes: 18 additions & 0 deletions src/app/addressbook/data/contact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Contact } from "../model/addressbook";

export const CONTACTS: Contact[] = [
{
id: 1,
firstName: "Dave",
lastName: "Ames",
street: "Bacup road 12",
city: "Bacup",
},
{
id: 2,
firstName: "Nigel",
lastName: "Sibbert",
street: "Bournemouth road 12",
city: "Bournemouth",
},
]
Empty file.
1 change: 1 addition & 0 deletions src/app/addressbook/edit/edit.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>edit works!</p>
10 changes: 10 additions & 0 deletions src/app/addressbook/edit/edit.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Component } from '@angular/core';

@Component({
selector: 'app-edit',
templateUrl: './edit.component.html',
styleUrl: './edit.component.css'
})
export class EditComponent {

}
Empty file.
25 changes: 25 additions & 0 deletions src/app/addressbook/list/list.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h2>Contact List Page</h2>
<table>
<thead>
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>Street</th>
<th>City</th>
</tr>
</thead>

<tbody>
@for (contact of this.contacts; track contact) {
<tr>
<td>{{ contact.id }}</td>
<td>{{ contact.firstName }}</td>
<td>{{ contact.lastName }}</td>
<td>{{ contact.street }}</td>
<td>{{ contact.city }}</td>
<td><a routerLink="/addressbook/{{ contact.id }}">Link</a></td>
</tr>
}
</tbody>
</table>
14 changes: 14 additions & 0 deletions src/app/addressbook/list/list.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Component, inject } from '@angular/core';
import { AddressbookService } from 'src/app/services/addressbook.service';
import { Contact } from '../model/addressbook';

@Component({
selector: 'app-list',
templateUrl: './list.component.html',
styleUrl: './list.component.css'
})
export class ListComponent {
contactService = inject(AddressbookService)

contacts: Contact[] = this.contactService.getAllContacts();
}
7 changes: 7 additions & 0 deletions src/app/addressbook/model/addressbook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface Contact {
id: number;
firstName: string;
lastName: string;
street: string;
city: string;
}
Empty file.
10 changes: 10 additions & 0 deletions src/app/addressbook/view/view.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@if (this.contact === null) {
<div>
<p><strong>Contact is not available</strong></p>
</div>
} @else {
<div>
<h3>{{contact.firstName}} {{contact.lastName}}</h3>
<p>{{contact.street}} {{contact.city}}</p>
</div>
}
17 changes: 17 additions & 0 deletions src/app/addressbook/view/view.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Component, inject } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AddressbookService } from 'src/app/services/addressbook.service';
import { Contact } from '../model/addressbook';

@Component({
selector: 'app-view',
templateUrl: './view.component.html',
styleUrl: './view.component.css'
})
export class ViewComponent {
addressbookService = inject(AddressbookService)
route = inject(ActivatedRoute)

id = this.route.snapshot.paramMap.get('id')
contact: Contact | null = this.addressbookService.getContactById(Number(this.id))
}
18 changes: 17 additions & 1 deletion src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ListComponent } from './addressbook/list/list.component';
import { AddComponent } from './addressbook/add/add.component';
import { ViewComponent } from './addressbook/view/view.component';

const routes: Routes = [];
const routes: Routes = [
{
path: "addressbook",
component: ListComponent,
},
{
path: "addressbook/add",
component: AddComponent,
},
{
path: "addressbook/:id",
component: ViewComponent,
},
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
Expand Down
8 changes: 5 additions & 3 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { AppComponent } from './app.component';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LayoutModule } from './layout/layout.module';
import { CommonModule } from '@angular/common';
import { AddressbookModule } from './addressbook/addressbook.module';


@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AppRoutingModule, LayoutModule],
imports: [BrowserModule, AppRoutingModule, LayoutModule, CommonModule, AddressbookModule],
bootstrap: [AppComponent],
})
export class AppModule {}
5 changes: 3 additions & 2 deletions src/app/layout/menu/menu.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<h2>Menu</h2>
<ul>
<li><a>Contacts list</a></li>
<li><a>Add new contact</a></li>
<li><a routerLink="/">Home</a></li>
<li><a routerLink="/addressbook">Contacts list</a></li>
<li><a routerLink="/addressbook/add">Add new contact</a></li>
</ul>
26 changes: 26 additions & 0 deletions src/app/services/addressbook.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Injectable } from '@angular/core';
import { Contact } from '../addressbook/model/addressbook';
import { CONTACTS } from '../addressbook/data/contact';

@Injectable({
providedIn: 'root'
})
export class AddressbookService {
private contacts: Contact[] = CONTACTS
private currentId: number = this.contacts.length

public getContactById(id: number | null): Contact | null {
const contact = this.contacts.find((contact) => contact.id === id)
return !contact ? null : contact
}

public getAllContacts(): Contact[] {
return this.contacts
}

public addContact(contact: Contact) {
this.currentId++;
this.contacts.push({...contact, id: this.currentId});
}
constructor() { }
}