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
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 './contacts/list/list.component';
import { AddComponent } from './contacts/add/add.component';
import { ViewComponent } from './contacts/view/view.component';

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

@NgModule({
imports: [RouterModule.forRoot(routes)],
Expand Down
1 change: 1 addition & 0 deletions src/app/app.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
display: flex;
justify-content: center;
}

3 changes: 3 additions & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<h1>Contacts</h1>

<app-menu></app-menu>

<div class="page">
<router-outlet></router-outlet>
</div>
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { BrowserModule } from '@angular/platform-browser';

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

@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AppRoutingModule, LayoutModule],
imports: [BrowserModule, AppRoutingModule, CommonModule, LayoutModule, ContactsModule],
bootstrap: [AppComponent],
})
export class AppModule {}
Empty file.
18 changes: 18 additions & 0 deletions src/app/contacts/add/add.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<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>
<button type="submit">Add Contact</button>
</form>
</div>
30 changes: 30 additions & 0 deletions src/app/contacts/add/add.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Component, inject } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { ContactsService } from 'src/app/services/contacts.service';

@Component({
selector: 'app-add',
templateUrl: './add.component.html',
styleUrl: './add.component.css'
})
export class AddComponent {
contactForm: FormGroup;
formBuilder = inject(FormBuilder)
contactsService = inject(ContactsService)
router = inject(Router)

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

addContact() {
this.contactsService.addContact(this.contactForm.value)
this.router.navigate(['contacts'])
}

}
28 changes: 28 additions & 0 deletions src/app/contacts/contacts.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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 { RouterModule } from '@angular/router';
import { ReactiveFormsModule } from '@angular/forms';



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

export const CONTACTS: Contact[] = [
{ id: 1, firstName: 'Roderick', lastName: 'Leito', city: 'The Hague' },
{ id: 2, firstName: 'Arjen', lastName: 'Lubach', city: 'Haarlem' },
{ id: 3, firstName: 'James', lastName: 'Harden', city: 'Amsterdam' },
{ id: 4, firstName: 'Lisa', lastName: 'Gravenberch', city: 'Leiden' },
{ id: 5, firstName: 'Joshua', lastName: 'Bart', city: 'Utrecht' }
]
Empty file.
21 changes: 21 additions & 0 deletions src/app/contacts/list/list.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<h2>Contact List Page</h2>
<table>
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Link</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><a routerLink="/contacts/{{ contact.id }}">Link</a></td>
</tr>
}
</tbody>
</table>
14 changes: 14 additions & 0 deletions src/app/contacts/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 { ContactsService } from '../../services/contacts.service';
import { Contact } from '../models/contact';

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

contacts: Contact[] = this.contactService.getAllContacts();
}
6 changes: 6 additions & 0 deletions src/app/contacts/models/contact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Contact {
id: number;
firstName: string;
lastName: string;
city: string;
}
Empty file.
10 changes: 10 additions & 0 deletions src/app/contacts/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 not available</strong></p>
</div>
} @else {
<div>
<h3>Name: {{contact.firstName}} {{contact.lastName}}</h3>
<p>City: {{contact.city}}</p>
</div>
}
19 changes: 19 additions & 0 deletions src/app/contacts/view/view.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Component } from '@angular/core';
import { inject } from '@angular/core';
import { ContactsService } from '../../services/contacts.service';
import { ActivatedRoute } from '@angular/router';
import { Contact } from '../models/contact';

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

id = this.route.snapshot.paramMap.get('id');

contact: Contact | null = this.contactService.getContactById(Number (this.id))
}
1 change: 1 addition & 0 deletions src/app/layout/menu/menu.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
ul {
padding: 0;
}

li {
list-style: none;
}
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="/contacts">Contacts list</a></li>
<li><a routerLink="/contacts/add">Add new contact</a></li>
</ul>
16 changes: 16 additions & 0 deletions src/app/services/contacts.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { ContactsService } from './contacts.service';

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

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

it('should be created', () => {
expect(service).toBeTruthy();
});
});
28 changes: 28 additions & 0 deletions src/app/services/contacts.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Injectable } from '@angular/core';
import { Contact } from '../contacts/models/contact';
import { CONTACTS } from '../contacts/data/contacts';

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

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

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

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