diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 02972627..dacac2c2 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -1,7 +1,16 @@ 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 { UpdateComponent } from './contacts/update/update.component'; -const routes: Routes = []; +const routes: Routes = [ + { path: 'contacts', component: ListComponent }, + { path: 'contacts/add', component: AddComponent }, + { path: 'contacts/:id', component: ViewComponent }, + { path: 'contacts/:id/update', component: UpdateComponent} +]; @NgModule({ imports: [RouterModule.forRoot(routes)], diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 8207184c..1f46866b 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -4,10 +4,18 @@ 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 { ContactsModule } from './contacts/contacts-module'; @NgModule({ declarations: [AppComponent], - imports: [BrowserModule, AppRoutingModule, LayoutModule], + imports: [ + BrowserModule, + AppRoutingModule, + LayoutModule, + CommonModule, + 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..476614c8 --- /dev/null +++ b/src/app/contacts/add/add.component.html @@ -0,0 +1,36 @@ +
+ +

Add Contact

+
+
+ + +
+ +
+ + +
+ +
+ + +
+ + +
+
\ No newline at end of file diff --git a/src/app/contacts/add/add.component.spec.ts b/src/app/contacts/add/add.component.spec.ts new file mode 100644 index 00000000..f4528470 --- /dev/null +++ b/src/app/contacts/add/add.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { AddComponent } from './add.component'; + +describe('AddComponent', () => { + let component: AddComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [AddComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(AddComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/contacts/add/add.component.ts b/src/app/contacts/add/add.component.ts new file mode 100644 index 00000000..f4aa478f --- /dev/null +++ b/src/app/contacts/add/add.component.ts @@ -0,0 +1,38 @@ +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 contactsService : ContactsService, + private readonly router : Router + ) { + this.contactForm = this.formBuilder.group({ + firstname: ['', Validators.required], + lastname: ['', Validators.required], + email: ['', Validators.required], + }); + } + + addContact() : void { + const newContact : Contact = { + id: this.contactsService.getLastId() + 1, + firstname: this.contactForm.value.firstname, + lastname: this.contactForm.value.lastname, + email: this.contactForm.value.email, + }; + this.contactsService.AddContact(newContact); + this.contactForm.reset(); + 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..ab471601 --- /dev/null +++ b/src/app/contacts/contacts-module.ts @@ -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 { UpdateComponent } from './update/update.component'; +import { RouterModule } from '@angular/router'; +import { ReactiveFormsModule } from '@angular/forms'; + + + +@NgModule({ + declarations: [ + AddComponent, + ViewComponent, + ListComponent, + UpdateComponent + ], + imports: [ + CommonModule, + RouterModule, + ReactiveFormsModule + ], + exports: [ + AddComponent, + ViewComponent, + ListComponent, + UpdateComponent + ] +}) +export class ContactsModule { } diff --git a/src/app/contacts/contacts-service.ts b/src/app/contacts/contacts-service.ts new file mode 100644 index 00000000..f2c922f6 --- /dev/null +++ b/src/app/contacts/contacts-service.ts @@ -0,0 +1,28 @@ +import { Injectable } from '@angular/core'; +import { Contact } from './models/contact'; +import { CONTACTS } from '../data/contacts'; + +@Injectable({ + providedIn: 'root' +}) +export class ContactsService { + public contacts: Contact[] = CONTACTS; + + public updateContact(updatedContact : Contact) : void { + this.contacts[this.contacts.findIndex(c => c.id === updatedContact.id)] = updatedContact; + } + + public getLastId() : number { + return Number(this.contacts.at(this.contacts.length - 1)?.id); + } + + public AddContact(contact : Contact) : void { + this.contacts.push(contact); + } + + public getContactById(id: number | null) : Contact | null { + const contact = this.contacts.find(c => c.id === id); + return contact ? contact : null; + } + +} 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..69eb5964 --- /dev/null +++ b/src/app/contacts/list/list.component.html @@ -0,0 +1,13 @@ +

Contacts list page

+ + + + + + + + + + + +
FirstnameLastname
{{contact.firstname}}{{contact.lastname}}Details
\ No newline at end of file diff --git a/src/app/contacts/list/list.component.ts b/src/app/contacts/list/list.component.ts new file mode 100644 index 00000000..aadbfcc3 --- /dev/null +++ b/src/app/contacts/list/list.component.ts @@ -0,0 +1,19 @@ +import { Component } from '@angular/core'; +import { Contact } from '../models/contact'; +import { ContactsService } from '../contacts-service'; +import { RouterLink } from '@angular/router'; +import { RouterModule } from '@angular/router'; + +@Component({ + selector: 'app-list', + standalone: false, + templateUrl: './list.component.html', + styleUrls: ['./list.component.css'] +}) +export class ListComponent { + contacts: Contact[]; + + constructor(private readonly contactsService: ContactsService) { + this.contacts = this.contactsService.contacts; + } +} diff --git a/src/app/contacts/models/contact.ts b/src/app/contacts/models/contact.ts new file mode 100644 index 00000000..58aca8ad --- /dev/null +++ b/src/app/contacts/models/contact.ts @@ -0,0 +1,6 @@ +export interface Contact { + id: number | null; + firstname: string; + lastname: string; + email: string; +} \ No newline at end of file diff --git a/src/app/contacts/update/update.component.css b/src/app/contacts/update/update.component.css new file mode 100644 index 00000000..e69de29b diff --git a/src/app/contacts/update/update.component.html b/src/app/contacts/update/update.component.html new file mode 100644 index 00000000..8da2bfd8 --- /dev/null +++ b/src/app/contacts/update/update.component.html @@ -0,0 +1,36 @@ +
+ +

Update Contact

+
+
+ + +
+ +
+ + +
+ +
+ + +
+ + +
+
\ No newline at end of file diff --git a/src/app/contacts/update/update.component.ts b/src/app/contacts/update/update.component.ts new file mode 100644 index 00000000..2c60e160 --- /dev/null +++ b/src/app/contacts/update/update.component.ts @@ -0,0 +1,60 @@ +import { Component } from '@angular/core'; +import { FormGroup, FormBuilder, Validators } from '@angular/forms'; +import { Contact } from '../models/contact'; +import { ContactsService } from '../contacts-service'; +import { Router } from '@angular/router'; +import { ActivatedRoute } from '@angular/router'; + +@Component({ + selector: 'app-update', + standalone: false, + templateUrl: './update.component.html', + styleUrl: './update.component.css' +}) +export class UpdateComponent { + id: string | null; + contact: Contact | null; + + contactForm : FormGroup; + constructor( + private readonly formBuilder : FormBuilder, + private readonly contactsService : ContactsService, + private readonly router : Router, + private readonly route: ActivatedRoute + ) { + this.id = this.route.snapshot.paramMap.get('id'); + this.contact = this.contactsService.getContactById(Number(this.id)); + + this.contactForm = this.formBuilder.group({ + firstname: [this.contact?.firstname, Validators.required], + lastname: [this.contact?.lastname, Validators.required], + email: [this.contact?.email, Validators.required], + }); + } + + updateContact() : void { + const newContact : Contact = { + id: Number(this.id), + firstname: this.contactForm.value.firstname, + lastname: this.contactForm.value.lastname, + email: this.contactForm.value.email, + }; + this.contactsService.updateContact(newContact); + this.contactForm.reset(); + this.router.navigate(['/contacts']) + } + +} + +/* + +id: string | null; + contact: Contact | null; + + constructor(private readonly contactsService : ContactsService, private readonly route: ActivatedRoute) { + this.id = this.route.snapshot.paramMap.get('id'); + this.contact = this.contactsService.getContactById(Number(this.id)); + } + + +*/ \ 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..431f6a92 --- /dev/null +++ b/src/app/contacts/view/view.component.html @@ -0,0 +1,9 @@ +
+ +

{{contact?.id}}

+

{{contact?.firstname}}

+

{{contact?.lastname}}

+

{{contact?.email}}

+ +Update +
\ No newline at end of file diff --git a/src/app/contacts/view/view.component.ts b/src/app/contacts/view/view.component.ts new file mode 100644 index 00000000..51a57d8b --- /dev/null +++ b/src/app/contacts/view/view.component.ts @@ -0,0 +1,24 @@ +import { Component } from '@angular/core'; +import { ContactsModule } from '../contacts-module'; +import { ActivatedRoute } from '@angular/router'; +import { Contact } from '../models/contact'; +import { ContactsService } from '../contacts-service'; +import { RouterLink } from '@angular/router'; +import { RouterModule } from '@angular/router'; + +@Component({ + selector: 'app-view', + standalone: false, + templateUrl: './view.component.html', + styleUrl: './view.component.css' +}) +export class ViewComponent { + id: string | null; + contact: Contact | null; + + constructor(private readonly contactsService : ContactsService, private readonly route: ActivatedRoute) { + this.id = this.route.snapshot.paramMap.get('id'); + this.contact = this.contactsService.getContactById(Number(this.id)); + } + +} diff --git a/src/app/data/contacts.ts b/src/app/data/contacts.ts new file mode 100644 index 00000000..c7ad97af --- /dev/null +++ b/src/app/data/contacts.ts @@ -0,0 +1,10 @@ +import { Contact } from "../contacts/models/contact"; + +export const CONTACTS: Contact[] = [ + { + id: 1, + firstname: 'Johan', + lastname: 'Reitan', + email: 'johanreitan@email.com', + }, +] \ No newline at end of file diff --git a/src/app/layout/menu/menu.component.html b/src/app/layout/menu/menu.component.html index 7c5ec7a2..62b90d8d 100644 --- a/src/app/layout/menu/menu.component.html +++ b/src/app/layout/menu/menu.component.html @@ -1,5 +1,6 @@ -

Menu

+

MENU

+ +
  • Contacts
  • +
  • Add
  • + \ No newline at end of file