diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 02972627..59a7162c 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.ts b/src/app/app.component.ts index b82791ab..f74dbbeb 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -6,5 +6,5 @@ import { Component } from '@angular/core'; styleUrls: ['./app.component.css'] }) export class AppComponent { - title = 'angular-address-book'; + title = 'Angular Address Book'; } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 8207184c..6fb323a5 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -2,12 +2,24 @@ import { NgModule } from '@angular/core'; 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'; +import { AppComponent } from './app.component'; @NgModule({ - declarations: [AppComponent], - imports: [BrowserModule, AppRoutingModule, LayoutModule], - bootstrap: [AppComponent], + declarations: [ + AppComponent + ], + imports: [ + BrowserModule, + AppRoutingModule, + CommonModule, + LayoutModule, + ContactsModule + ], + providers: [ + ], + bootstrap: [AppComponent] }) -export class AppModule {} +export class AppModule { } diff --git a/src/app/contacts.service.ts b/src/app/contacts.service.ts new file mode 100644 index 00000000..4bfffd90 --- /dev/null +++ b/src/app/contacts.service.ts @@ -0,0 +1,30 @@ +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; + + 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 }) + } +} 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..f2ac4659 --- /dev/null +++ b/src/app/contacts/add/add.component.html @@ -0,0 +1,22 @@ +
+

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..005c9970 --- /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/contacts.service'; + +@Component({ + selector: 'app-add', + templateUrl: './add.component.html', + styleUrl: './add.component.css' +}) +export class AddComponent { + contactForm: FormGroup; + formBuilder = inject(FormBuilder); + contactService = inject(ContactsService); + 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(['contacts']); + } +} diff --git a/src/app/contacts/contacts.module.ts b/src/app/contacts/contacts.module.ts new file mode 100644 index 00000000..e8154aeb --- /dev/null +++ b/src/app/contacts/contacts.module.ts @@ -0,0 +1,29 @@ +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..e7c67f07 --- /dev/null +++ b/src/app/contacts/data/contacts.ts @@ -0,0 +1,25 @@ +import { Contact } from '../models/contact'; + +export const CONTACTS: Contact[] = [ + { + id: 1, + firstName: "Richard", + lastName: "Persson", + street: "Lakkegata 53", + city: "Oslo" + }, + { + id: 2, + firstName: "Moueed", + lastName: "Ali", + street: "Lakkegata 54", + city: "Oslo" + }, + { + id: 3, + firstName: "Linda", + lastName: "Do", + street: "Lakkegata 55", + city: "Oslo" + }, +] \ 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..fa010c1c --- /dev/null +++ b/src/app/contacts/list/list.component.html @@ -0,0 +1,25 @@ +

Contacts List Page

+ + + + + + + + + + + + + @for (contact of contactService.getAllContacts(); track contact) { + + + + + + + + + } + +
IdFirst NameLast NameStreetCityLink
{{contact.id}}{{contact.firstName}}{{contact.lastName}}{{contact.street}}{{contact.city}}Link
\ 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..2d6540b8 --- /dev/null +++ b/src/app/contacts/list/list.component.ts @@ -0,0 +1,14 @@ +import { Component, inject } from '@angular/core'; +import { ContactsService } from 'src/app/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..61b9bfcb --- /dev/null +++ b/src/app/contacts/models/contact.ts @@ -0,0 +1,10 @@ +export interface Contact { + // any object of type Car needs to have this shape + // kind of like blueprint like Java??? + + id: number; + firstName: string; + lastName: string; + street: 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..deed1250 --- /dev/null +++ b/src/app/contacts/view/view.component.html @@ -0,0 +1,10 @@ +@if (this.contact === null) { +
+

Contact not available

+
+} @else { +
+

{{contact.firstName}} {{contact.lastName}}

+

{{contact.street}} {{contact.city}}

+
+} \ 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..73716eea --- /dev/null +++ b/src/app/contacts/view/view.component.ts @@ -0,0 +1,17 @@ +import { Component, inject } from '@angular/core'; +import { ActivatedRoute } from '@angular/router'; +import { ContactsService } from 'src/app/contacts.service'; +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 + contact: Contact | null = this.contactService.getContactById(Number(this.id)); +} 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/index.html b/src/index.html index b0e9832d..bcdc83e5 100644 --- a/src/index.html +++ b/src/index.html @@ -2,7 +2,7 @@ - AngularAddressBook + Angular Address Book