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
2 changes: 1 addition & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import { Component } from '@angular/core';
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'angular-address-book';
title = 'Angular Address Book';
}
22 changes: 17 additions & 5 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
30 changes: 30 additions & 0 deletions src/app/contacts.service.ts
Original file line number Diff line number Diff line change
@@ -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 })
}
}
Empty file.
22 changes: 22 additions & 0 deletions src/app/contacts/add/add.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<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="street">Street</label>
<input type="text" id="street" formControlName="street" />
</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/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']);
}
}
29 changes: 29 additions & 0 deletions src/app/contacts/contacts.module.ts
Original file line number Diff line number Diff line change
@@ -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 { }
25 changes: 25 additions & 0 deletions src/app/contacts/data/contacts.ts
Original file line number Diff line number Diff line change
@@ -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"
},
]
Empty file.
25 changes: 25 additions & 0 deletions src/app/contacts/list/list.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h2>Contacts List Page</h2>
<table>
<thead>
<tr>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Street</th>
<th>City</th>
<th>Link</th>
</tr>
</thead>
<tbody>
@for (contact of contactService.getAllContacts(); track contact) {
<tr>
<td>{{contact.id}}</td>
<td>{{contact.firstName}}</td>
<td>{{contact.lastName}}</td>
<td>{{contact.street}}</td>
<td>{{contact.city}}</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 '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();
}
10 changes: 10 additions & 0 deletions src/app/contacts/models/contact.ts
Original file line number Diff line number Diff line change
@@ -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;
}
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>
<h1>{{contact.firstName}} {{contact.lastName}}</h1>
<p>{{contact.street}} {{contact.city}}</p>
</div>
}
17 changes: 17 additions & 0 deletions src/app/contacts/view/view.component.ts
Original file line number Diff line number Diff line change
@@ -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));
}
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>
2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title>AngularAddressBook</title>
<title>Angular Address Book</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
Expand Down