From 7dbdc1c7b3f92bee7d4bcf7cbf9394e5cd6e6ac7 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 8 Sep 2025 14:40:54 +0200 Subject: [PATCH] core finished --- angular.json | 3 ++ src/app/app-routing.module.ts | 10 ++++- src/app/app.component.css | 24 +++++++++--- src/app/app.component.html | 19 ++++++++-- src/app/app.module.ts | 15 +++++--- src/app/contact.service.spec.ts | 16 ++++++++ src/app/contact.service.ts | 29 +++++++++++++++ .../contacts-list/contacts-list.component.css | 0 .../contacts-list.component.html | 8 ++++ .../contacts-list.component.spec.ts | 23 ++++++++++++ .../contacts-list/contacts-list.component.ts | 18 +++++++++ src/app/new-contact/new-contact.component.css | 0 .../new-contact/new-contact.component.html | 20 ++++++++++ .../new-contact/new-contact.component.spec.ts | 23 ++++++++++++ src/app/new-contact/new-contact.component.ts | 37 +++++++++++++++++++ .../view-contact/view-contact.component.css | 0 .../view-contact/view-contact.component.html | 8 ++++ .../view-contact.component.spec.ts | 23 ++++++++++++ .../view-contact/view-contact.component.ts | 22 +++++++++++ 19 files changed, 283 insertions(+), 15 deletions(-) create mode 100644 src/app/contact.service.spec.ts create mode 100644 src/app/contact.service.ts create mode 100644 src/app/contacts-list/contacts-list.component.css create mode 100644 src/app/contacts-list/contacts-list.component.html create mode 100644 src/app/contacts-list/contacts-list.component.spec.ts create mode 100644 src/app/contacts-list/contacts-list.component.ts create mode 100644 src/app/new-contact/new-contact.component.css create mode 100644 src/app/new-contact/new-contact.component.html create mode 100644 src/app/new-contact/new-contact.component.spec.ts create mode 100644 src/app/new-contact/new-contact.component.ts create mode 100644 src/app/view-contact/view-contact.component.css create mode 100644 src/app/view-contact/view-contact.component.html create mode 100644 src/app/view-contact/view-contact.component.spec.ts create mode 100644 src/app/view-contact/view-contact.component.ts diff --git a/angular.json b/angular.json index 7a4f1ef7..02897fd8 100644 --- a/angular.json +++ b/angular.json @@ -94,5 +94,8 @@ } } } + }, + "cli": { + "analytics": "5af61b5e-4086-4e04-814e-3157b06486a2" } } diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 02972627..bbe1ff2b 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -1,7 +1,15 @@ import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; +import { ContactsListComponent } from './contacts-list/contacts-list.component'; +import { NewContactComponent } from './new-contact/new-contact.component'; +import { ViewContactComponent } from './view-contact/view-contact.component'; -const routes: Routes = []; +const routes: Routes = [ + { path: 'contacts', component: ContactsListComponent }, + { path: 'contacts/new', component: NewContactComponent }, + { path: 'contacts/:id', component: ViewContactComponent }, + { path: '', redirectTo: '/contacts', pathMatch: 'full' } +]; @NgModule({ imports: [RouterModule.forRoot(routes)], diff --git a/src/app/app.component.css b/src/app/app.component.css index 31d9e249..83bc75c7 100644 --- a/src/app/app.component.css +++ b/src/app/app.component.css @@ -1,9 +1,23 @@ -:host { +.container { display: flex; + height: 100vh; } -.page { - flex: 1; - display: flex; - justify-content: center; +.sidebar { + width: 200px; + + padding: 1rem; + box-sizing: border-box; } + +.sidebar ul { + list-style: none; + padding: 0; + margin: 0; +} + +.content { + flex: 1; + padding: 1rem; + box-sizing: border-box; +} \ No newline at end of file diff --git a/src/app/app.component.html b/src/app/app.component.html index 17aaa0c6..4fe3c5a2 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,4 +1,15 @@ - -
- -
+
+ + +
+ +
+
\ No newline at end of file diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 8207184c..eff0ad7c 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -3,11 +3,16 @@ import { BrowserModule } from '@angular/platform-browser'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; -import { LayoutModule } from './layout/layout.module'; +import { ContactsListComponent } from './contacts-list/contacts-list.component'; +import { NewContactComponent } from './new-contact/new-contact.component'; +import { ViewContactComponent } from './view-contact/view-contact.component'; +import { RouterModule } from '@angular/router'; +import { ReactiveFormsModule } from '@angular/forms'; @NgModule({ - declarations: [AppComponent], - imports: [BrowserModule, AppRoutingModule, LayoutModule], - bootstrap: [AppComponent], + declarations: [AppComponent, ContactsListComponent, NewContactComponent, ViewContactComponent], + imports: [BrowserModule, AppRoutingModule, RouterModule, ReactiveFormsModule], + providers: [], + bootstrap: [AppComponent] }) -export class AppModule {} +export class AppModule { } diff --git a/src/app/contact.service.spec.ts b/src/app/contact.service.spec.ts new file mode 100644 index 00000000..10418408 --- /dev/null +++ b/src/app/contact.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { ContactService } from './contact.service'; + +describe('ContactService', () => { + let service: ContactService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(ContactService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/contact.service.ts b/src/app/contact.service.ts new file mode 100644 index 00000000..dbd64794 --- /dev/null +++ b/src/app/contact.service.ts @@ -0,0 +1,29 @@ +import { Injectable } from '@angular/core'; + +export interface Contact { + id: number; + name: string; + email: string; +} + +@Injectable({ + providedIn: 'root' +}) +export class ContactService { + private contacts: Contact[] = [ + { id: 1, name: 'Alice', email: 'alice@mail.com' }, + { id: 2, name: 'Bob', email: 'bob@mail.com' } + ]; + + getContacts(): Contact[] { + return this.contacts; + } + + getContactById(id: number): Contact | undefined { + return this.contacts.find(c => c.id === id); + } + + addContact(contact: Contact): void { + this.contacts.push(contact); + } +} diff --git a/src/app/contacts-list/contacts-list.component.css b/src/app/contacts-list/contacts-list.component.css new file mode 100644 index 00000000..e69de29b diff --git a/src/app/contacts-list/contacts-list.component.html b/src/app/contacts-list/contacts-list.component.html new file mode 100644 index 00000000..d2a228e1 --- /dev/null +++ b/src/app/contacts-list/contacts-list.component.html @@ -0,0 +1,8 @@ +

Contacts

+ \ No newline at end of file diff --git a/src/app/contacts-list/contacts-list.component.spec.ts b/src/app/contacts-list/contacts-list.component.spec.ts new file mode 100644 index 00000000..3ce82cbd --- /dev/null +++ b/src/app/contacts-list/contacts-list.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ContactsListComponent } from './contacts-list.component'; + +describe('ContactsListComponent', () => { + let component: ContactsListComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [ContactsListComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(ContactsListComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/contacts-list/contacts-list.component.ts b/src/app/contacts-list/contacts-list.component.ts new file mode 100644 index 00000000..b6a1aba3 --- /dev/null +++ b/src/app/contacts-list/contacts-list.component.ts @@ -0,0 +1,18 @@ +import { Component, OnInit } from '@angular/core'; +import { ContactService, Contact } from '../contact.service'; + +@Component({ + selector: 'app-contacts-list', + templateUrl: './contacts-list.component.html', + styleUrl: './contacts-list.component.css' +}) +export class ContactsListComponent implements OnInit { + contacts: Contact[] = []; + + constructor(private contactService: ContactService) { } + + ngOnInit(): void { + this.contacts = this.contactService.getContacts(); + } + +} diff --git a/src/app/new-contact/new-contact.component.css b/src/app/new-contact/new-contact.component.css new file mode 100644 index 00000000..e69de29b diff --git a/src/app/new-contact/new-contact.component.html b/src/app/new-contact/new-contact.component.html new file mode 100644 index 00000000..d01b6177 --- /dev/null +++ b/src/app/new-contact/new-contact.component.html @@ -0,0 +1,20 @@ +

New Contact

+
+ +
+ Name is required +
+ + +
+ Enter a valid email +
+ + +
\ No newline at end of file diff --git a/src/app/new-contact/new-contact.component.spec.ts b/src/app/new-contact/new-contact.component.spec.ts new file mode 100644 index 00000000..a6b88212 --- /dev/null +++ b/src/app/new-contact/new-contact.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { NewContactComponent } from './new-contact.component'; + +describe('NewContactComponent', () => { + let component: NewContactComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [NewContactComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(NewContactComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/new-contact/new-contact.component.ts b/src/app/new-contact/new-contact.component.ts new file mode 100644 index 00000000..84abb5da --- /dev/null +++ b/src/app/new-contact/new-contact.component.ts @@ -0,0 +1,37 @@ +import { Component, OnInit } from '@angular/core'; +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; +import { ContactService, Contact } from '../contact.service'; +import { Router } from '@angular/router'; + +@Component({ + selector: 'app-new-contact', + templateUrl: './new-contact.component.html', + styleUrls: ['./new-contact.component.css'] +}) +export class NewContactComponent implements OnInit { + contactForm!: FormGroup; + + constructor( + private fb: FormBuilder, + private contactService: ContactService, + private router: Router + ) { } + + ngOnInit(): void { + this.contactForm = this.fb.group({ + name: ['', Validators.required], + email: ['', [Validators.required, Validators.email]] + }); + } + + onSubmit(): void { + if (this.contactForm.valid) { + const newContact: Contact = { + id: this.contactService.getContacts().length + 1, // simpel id + ...this.contactForm.value + }; + this.contactService.addContact(newContact); + this.router.navigate(['/contacts']); // terug naar lijst + } + } +} diff --git a/src/app/view-contact/view-contact.component.css b/src/app/view-contact/view-contact.component.css new file mode 100644 index 00000000..e69de29b diff --git a/src/app/view-contact/view-contact.component.html b/src/app/view-contact/view-contact.component.html new file mode 100644 index 00000000..67ce4ba6 --- /dev/null +++ b/src/app/view-contact/view-contact.component.html @@ -0,0 +1,8 @@ +
+

{{ contact.name }}

+

Email: {{ contact.email }}

+
+ + +

Contact not found!

+
\ No newline at end of file diff --git a/src/app/view-contact/view-contact.component.spec.ts b/src/app/view-contact/view-contact.component.spec.ts new file mode 100644 index 00000000..fce5d2fc --- /dev/null +++ b/src/app/view-contact/view-contact.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ViewContactComponent } from './view-contact.component'; + +describe('ViewContactComponent', () => { + let component: ViewContactComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [ViewContactComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(ViewContactComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/view-contact/view-contact.component.ts b/src/app/view-contact/view-contact.component.ts new file mode 100644 index 00000000..1aeae57e --- /dev/null +++ b/src/app/view-contact/view-contact.component.ts @@ -0,0 +1,22 @@ +import { Component, OnInit } from '@angular/core'; +import { ActivatedRoute } from '@angular/router'; +import { ContactService, Contact } from '../contact.service'; + +@Component({ + selector: 'app-view-contact', + templateUrl: './view-contact.component.html', + styleUrls: ['./view-contact.component.css'] +}) +export class ViewContactComponent implements OnInit { + contact: Contact | undefined; + + constructor( + private route: ActivatedRoute, + private contactService: ContactService + ) { } + + ngOnInit(): void { + const id = Number(this.route.snapshot.paramMap.get('id')); + this.contact = this.contactService.getContactById(id); + } +}