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
23 changes: 22 additions & 1 deletion src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ListComponent } from './contacts/list/list.component';
import { ViewComponent } from './contacts/view/view.component';
import { AddComponent } from './contacts/add/add.component';
import { EditComponent } from './contacts/edit/edit.component';

const routes: Routes = [];
const routes: Routes = [
{
path: "contacts",
component: ListComponent
},
{
path: "contacts/add",
component: AddComponent
},
{
path: "contacts/update/:id",
component: EditComponent
},
{
path: "contacts/:id",
component: ViewComponent
},
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
Expand Down
5 changes: 5 additions & 0 deletions src/app/app.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
.page {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
}

.contact-list {
display: grid;
}
15 changes: 11 additions & 4 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<app-menu></app-menu>
<div class="page">
<router-outlet></router-outlet>
</div>

<main class="main">
<div class="content">
<div class="page">
<h1>{{title}}</h1>
<app-menu />
</div>
</div>
</main>

<router-outlet />
3 changes: 2 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
standalone: false,
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'angular-address-book';
title = 'Address-book';
}
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { LayoutModule } from './layout/layout.module';
import { ContactsModule } from './contacts/contacts.module';
import { CommonModule } from '@angular/common';

@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AppRoutingModule, LayoutModule],
imports: [BrowserModule, AppRoutingModule, LayoutModule, ContactsModule, CommonModule],
bootstrap: [AppComponent],
})
export class AppModule {}
Empty file.
23 changes: 23 additions & 0 deletions src/app/contacts/add/add.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

<div class="side-page">
<h2 style="text-align: center;">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>
39 changes: 39 additions & 0 deletions src/app/contacts/add/add.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Component, inject } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { ContactService } from 'src/app/services/contact.service';
import { Contact } from '../models/contact';

@Component({
selector: 'app-add',
standalone: false,
templateUrl: './add.component.html',
styleUrl: './add.component.css'
})
export class AddComponent {
contactForm: FormGroup;
formBuilder = inject(FormBuilder);
contactService = inject(ContactService)
router = inject(Router)

constructor() {
this.contactForm = this.formBuilder.group({
firstName: ["", Validators.required],
lastName: ["", Validators.required],
street: ["", Validators.required],
city: ["", Validators.required]
});
}
addContact() {
const newContact: Contact = {
id: 0,
firstName: this.contactForm.value.firstName,
lastName: this.contactForm.value.lastName,
street: this.contactForm.value.street,
city: this.contactForm.value.city,
}
this.contactService.addContact(newContact)
this.contactForm.reset()
this.router.navigate(['contacts'])
}
}
26 changes: 26 additions & 0 deletions src/app/contacts/contacts.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ListComponent } from './list/list.component';
import { RouterModule } from '@angular/router';
import { AddComponent } from './add/add.component';
import { ViewComponent } from './view/view.component';
import { ReactiveFormsModule } from '@angular/forms';
import { EditComponent } from './edit/edit.component';



@NgModule({
declarations: [
ListComponent,
AddComponent,
ViewComponent,
EditComponent
],
imports: [
CommonModule,
RouterModule,
ReactiveFormsModule
],
exports: [ListComponent, ViewComponent, AddComponent, EditComponent]
})
export class ContactsModule { }
33 changes: 33 additions & 0 deletions src/app/contacts/data/contacts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import { Contact } from "../models/contact";

export const CONTACTS: Contact[] = [
{
id: 1,
firstName: "Magnus",
lastName: "Hissingby",
street: "someStreet",
city: "someCity"
},
{
id: 2,
firstName: "Birk",
lastName: "Sala",
street: "someStreet",
city: "someCity"
},
{
id: 3,
firstName: "Cru",
lastName: "Lavender",
street: "someStreet",
city: "someCity"
},
{
id: 4,
firstName: "Unc",
lastName: "Rivand",
street: "someStreet",
city: "someCity"
}
]
Empty file.
27 changes: 27 additions & 0 deletions src/app/contacts/edit/edit.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

@if (this.contact === null) {
<p>Contact does not exist!!!</p>
} @else {
<div class="side-page">
<h2>Edit contact</h2>
<form [formGroup]="contactForm" (ngSubmit)="editContact()">
<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">Update contact</button>
</form>
</div>
}
58 changes: 58 additions & 0 deletions src/app/contacts/edit/edit.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Component, inject } from '@angular/core';
import { Contact } from '../models/contact';
import { Form, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ContactService } from 'src/app/services/contact.service';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
selector: 'app-edit',
templateUrl: './edit.component.html',
styleUrl: './edit.component.css'
})
export class EditComponent {
contact: Contact | null = null;
contactId: number | null = null;
contactForm: FormGroup;
formBuilder: FormBuilder = inject(FormBuilder)
contactService: ContactService = inject(ContactService)
router: Router = inject(Router)
route: ActivatedRoute = inject(ActivatedRoute)

constructor() {
this.contactForm = this.formBuilder.group({
firstName: ['', Validators.required],
lastName: ['', Validators.required],
street: ['', Validators.required],
city: ['', Validators.required]
});
}

ngOnInit() {
this.contactId = Number(this.route.snapshot.paramMap.get('id'));
this.contact = this.contactService.getContactById(this.contactId)
if (this.contact !== null && this.contact !== undefined) {
this.contactForm.patchValue({
firstName: this.contact.firstName,
lastName: this.contact.lastName,
street: this.contact.street,
city: this.contact.city
})
}
}

editContact() {
if (!this.contactId) return;

const updatedContact: Contact = {
id: this.contactId,
firstName: this.contactForm.value.firstName,
lastName: this.contactForm.value.lastName,
street: this.contactForm.value.street,
city: this.contactForm.value.city
}
this.contactService.editContact(updatedContact);
this.contactForm.reset();
this.router.navigate(['contacts'])
}

}
7 changes: 7 additions & 0 deletions src/app/contacts/list/list.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

.contact-list {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: .9rem;
border: 1px solid black;
}
21 changes: 21 additions & 0 deletions src/app/contacts/list/list.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

<h1 style="text-align: center;">Contacts</h1>

@if (this.contacts.length===0){
<div>No contacts yet</div>
} @else {
<div class="side-page">
<div class="contact">
<ul class="contact-list" style="list-style-type: none;">
@for (contact of this.contacts; track contact){
<li>{{contact.firstName}} {{contact.lastName}}</li>
<li>{{contact.street}}</li>
<li>{{contact.city}}</li>
<li><a routerLink="/contacts/{{ contact.id }}">View Contact</a></li>
<li><a routerLink="/contacts/update/{{ contact.id }}">Update Contact</a></li>
}
</ul>
</div>
</div>
}

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 { ContactService } from 'src/app/services/contact.service';
import { Contact } from '../models/contact';

@Component({
selector: 'app-list',
standalone: false,
templateUrl: './list.component.html',
styleUrl: './list.component.css'
})
export class ListComponent {
contactService = inject(ContactService)
contacts: Contact[] = this.contactService.getAllContacts();
}
9 changes: 9 additions & 0 deletions src/app/contacts/models/contact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


export interface Contact {
id: number;
firstName: string;
lastName: string;
street: string;
city: string
}
Empty file.
12 changes: 12 additions & 0 deletions src/app/contacts/view/view.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

@if (this.contact === null) {
<div class="side-page">
<p><strong>Contact is null</strong></p>
</div>
} @else {
<div class="side-page">
<h1>{{this.contact.firstName}} {{this.contact.lastName}}</h1>
<h2>Street: {{this.contact.street}}</h2>
<h2>City: {{this.contact.city}}</h2>
</div>
}
18 changes: 18 additions & 0 deletions src/app/contacts/view/view.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Component, inject } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { ContactService } from 'src/app/services/contact.service';
import { Contact } from '../models/contact';

@Component({
selector: 'app-view',
standalone: false,
templateUrl: './view.component.html',
styleUrl: './view.component.css'
})
export class ViewComponent {
contactService = inject(ContactService)
route = inject(ActivatedRoute)

id = this.route.snapshot.paramMap.get('id')
contact: Contact | null = this.contactService.getContactById(Number(this.id))
}
2 changes: 1 addition & 1 deletion src/app/layout/layout.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';
import { MenuComponent } from './menu/menu.component';
import { RouterModule } from '@angular/router';

@NgModule({
declarations: [MenuComponent],
Expand Down
Loading