diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 02972627..31ae7a43 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -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)], diff --git a/src/app/app.component.css b/src/app/app.component.css index 31d9e249..e5a17386 100644 --- a/src/app/app.component.css +++ b/src/app/app.component.css @@ -7,3 +7,4 @@ display: flex; justify-content: center; } + diff --git a/src/app/app.component.html b/src/app/app.component.html index 17aaa0c6..1a53890f 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,4 +1,7 @@ +

Contacts

+ +
diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 8207184c..22b3dc08 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -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 {} diff --git a/src/app/contacts/add/add.component.css b/src/app/contacts/add/add.component.css new file mode 100644 index 00000000..e69de29b diff --git a/src/app/contacts/add/add.component.html b/src/app/contacts/add/add.component.html new file mode 100644 index 00000000..0e0c527c --- /dev/null +++ b/src/app/contacts/add/add.component.html @@ -0,0 +1,18 @@ +
+

Add a contact

+
+
+ + +
+
+ + +
+
+ + +
+ +
+
\ No newline at end of file diff --git a/src/app/contacts/add/add.component.ts b/src/app/contacts/add/add.component.ts new file mode 100644 index 00000000..d0db5724 --- /dev/null +++ b/src/app/contacts/add/add.component.ts @@ -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']) + } + +} diff --git a/src/app/contacts/contacts.module.ts b/src/app/contacts/contacts.module.ts new file mode 100644 index 00000000..af4d42dc --- /dev/null +++ b/src/app/contacts/contacts.module.ts @@ -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 { } diff --git a/src/app/contacts/data/contacts.ts b/src/app/contacts/data/contacts.ts new file mode 100644 index 00000000..c5b70bf7 --- /dev/null +++ b/src/app/contacts/data/contacts.ts @@ -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' } +] \ No newline at end of file diff --git a/src/app/contacts/list/list.component.css b/src/app/contacts/list/list.component.css new file mode 100644 index 00000000..e69de29b diff --git a/src/app/contacts/list/list.component.html b/src/app/contacts/list/list.component.html new file mode 100644 index 00000000..45950d2f --- /dev/null +++ b/src/app/contacts/list/list.component.html @@ -0,0 +1,21 @@ +

Contact List Page

+ + + + + + + + + + + @for (contact of this.contacts; track contact) { + + + + + + + } + +
IdFirst NameLast NameLink
{{ contact.id }}{{ contact.firstName }}{{ contact.lastName }}Link
diff --git a/src/app/contacts/list/list.component.ts b/src/app/contacts/list/list.component.ts new file mode 100644 index 00000000..809ca42d --- /dev/null +++ b/src/app/contacts/list/list.component.ts @@ -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(); +} diff --git a/src/app/contacts/models/contact.ts b/src/app/contacts/models/contact.ts new file mode 100644 index 00000000..eb710a78 --- /dev/null +++ b/src/app/contacts/models/contact.ts @@ -0,0 +1,6 @@ +export interface Contact { + id: number; + firstName: string; + lastName: string; + city: string; +} \ No newline at end of file diff --git a/src/app/contacts/view/view.component.css b/src/app/contacts/view/view.component.css new file mode 100644 index 00000000..e69de29b diff --git a/src/app/contacts/view/view.component.html b/src/app/contacts/view/view.component.html new file mode 100644 index 00000000..25ed6c3b --- /dev/null +++ b/src/app/contacts/view/view.component.html @@ -0,0 +1,10 @@ +@if (this.contact===null) { +
+

Contact not available

+
+} @else { +
+

Name: {{contact.firstName}} {{contact.lastName}}

+

City: {{contact.city}}

+
+} diff --git a/src/app/contacts/view/view.component.ts b/src/app/contacts/view/view.component.ts new file mode 100644 index 00000000..fd7119b6 --- /dev/null +++ b/src/app/contacts/view/view.component.ts @@ -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)) +} diff --git a/src/app/layout/menu/menu.component.css b/src/app/layout/menu/menu.component.css index 75955b3c..2b813100 100644 --- a/src/app/layout/menu/menu.component.css +++ b/src/app/layout/menu/menu.component.css @@ -5,6 +5,7 @@ ul { padding: 0; } + li { list-style: none; } diff --git a/src/app/layout/menu/menu.component.html b/src/app/layout/menu/menu.component.html index 7c5ec7a2..39c6f2f5 100644 --- a/src/app/layout/menu/menu.component.html +++ b/src/app/layout/menu/menu.component.html @@ -1,5 +1,6 @@

Menu

diff --git a/src/app/services/contacts.service.spec.ts b/src/app/services/contacts.service.spec.ts new file mode 100644 index 00000000..b64b5962 --- /dev/null +++ b/src/app/services/contacts.service.spec.ts @@ -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(); + }); +}); diff --git a/src/app/services/contacts.service.ts b/src/app/services/contacts.service.ts new file mode 100644 index 00000000..9d4c196a --- /dev/null +++ b/src/app/services/contacts.service.ts @@ -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 }) + } +}