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
3 changes: 3 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,8 @@
}
}
}
},
"cli": {
"analytics": false
}
}
9 changes: 8 additions & 1 deletion src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ContactlistComponent } from './contacts/contactlist/contactlist.component';
import { ContactFormComponent } from './contacts/contact-form/contact-form.component';
import { ContactViewComponent } from './contacts/contact-view/contact-view.component';

const routes: Routes = [];
const routes: Routes = [
{path: 'contacts', component: ContactlistComponent},
{path: 'contact-form', component: ContactFormComponent},
{path: 'contact/:id', component: ContactViewComponent}
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
Expand Down
4 changes: 2 additions & 2 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<app-menu></app-menu>
<app-menu />
<div class="page">
<router-outlet></router-outlet>
<router-outlet/>
</div>
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, CommonModule, AppRoutingModule, LayoutModule, ContactsModule],
bootstrap: [AppComponent],
})
export class AppModule {}
25 changes: 25 additions & 0 deletions src/app/contacts/contact-form/contact-form.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
:host {
display: flex;
box-sizing: border-box;
width: 40vw;
flex-direction: column;
}

form {
display: flex;
flex-direction: column;
flex: 1;
gap: 0.5em;
}

label {
font-weight: bold;
}

.actions {
display: flex;
}

.spacer {
flex: 1;
}
19 changes: 19 additions & 0 deletions src/app/contacts/contact-form/contact-form.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h1>Create Contact</h1>
<form [formGroup]="contactForm" (ngSubmit)="addContact()">
<label for="first-name">First name:</label>
<input id="first-name" formControlName="firstname" type="text" />

<label for="last-name">Last name:</label>
<input id="last-name" formControlName="lastname" type="text" />

<label for="street">Street:</label>
<input id="street" formControlName="street" type="text" />

<label for="city">City:</label>
<input id="city" formControlName="city" type="text" />

<div class="actions">
<div class="spacer"></div>
<button type="submit">Create</button>
</div>
</form>
39 changes: 39 additions & 0 deletions src/app/contacts/contact-form/contact-form.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ContactsService } from '../contacts.service';
import { Router } from '@angular/router';
import { Contact } from '../models/contact';

@Component({
selector: 'app-contact-form',
standalone: false,
templateUrl: './contact-form.component.html',
styleUrl: './contact-form.component.css'
})
export class ContactFormComponent {
contactForm: FormGroup;
constructor(
private readonly formBuilder: FormBuilder,
private readonly contactService: ContactsService,
private readonly router: Router
) {
this.contactForm = this.formBuilder.group({
firstname: ['', Validators.required],
lastname: ['', Validators.required],
street: ['', Validators.required],
city: ['', Validators.required],
});
}
addContact(): void {
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/contact-view/contact-view.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
:host {
display: flex;
box-sizing: border-box;
width: 40vw;
flex-direction: column;
}


form {
display: flex;
flex-direction: column;
flex: 1;
gap: 0.5em;
}

label {
font-weight: bold;
}

.actions {
display: flex;
}

.spacer {
flex: 1;
}
33 changes: 33 additions & 0 deletions src/app/contacts/contact-view/contact-view.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<ng-container *ngIf="contact?.id == null">
<p>Contact does not exist!</p>
</ng-container>

<ng-container *ngIf="contact?.id != null">
<ng-container *ngIf="!isEditing">
<h2>{{ contact?.firstname }} {{ contact?.lastname }}</h2>
<p>{{ contact?.street }}, {{ contact?.city }}</p>
<button (click)="toggleEdit()">Edit</button>
</ng-container >

<ng-container *ngIf="isEditing">
<form *ngIf="isEditing" [formGroup]="contactForm" (ngSubmit)="saveChanges()">
<label for="first-name">First name:</label>
<input id="first-name" formControlName="firstname" type="text" />

<label for="last-name">Last name:</label>
<input id="last-name" formControlName="lastname" type="text" />

<label for="street">Street:</label>
<input id="street" formControlName="street" type="text" />

<label for="city">City:</label>
<input id="city" formControlName="city" type="text" />

<div class="actions">
<div class="spacer"></div>
<button type="button" (click)="toggleEdit()">Cancel</button>
<button type="submit" [disabled]="contactForm.invalid">Edit</button>
</div>
</form>
</ng-container>
</ng-container>
58 changes: 58 additions & 0 deletions src/app/contacts/contact-view/contact-view.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Component } from '@angular/core';
import { Contact } from '../models/contact';
import { ActivatedRoute } from '@angular/router';
import { ContactsService } from '../contacts.service';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
selector: 'app-contact-view',
standalone: false,
templateUrl: './contact-view.component.html',
styleUrl: './contact-view.component.css'
})
export class ContactViewComponent {
contact: Contact | null = null;
contactId: number | null = null;
isEditing: boolean = false;
contactForm!: FormGroup;

constructor(
private route: ActivatedRoute,
private contactService: ContactsService,
private formBuilder: FormBuilder
) {}

ngOnInit(): void {
this.contactId = Number(this.route.snapshot.paramMap.get('id'));

this.contactService.GetContactById(this.contactId).subscribe((data) => {
this.contact = data!;
});

if (this.contact) {
this.contactForm = this.formBuilder.group({
firstname: [this.contact.firstname, Validators.required],
lastname: [this.contact.lastname, Validators.required],
street: [this.contact.street, Validators.required],
city: [this.contact.city, Validators.required]
})
}
}

toggleEdit(): void {
this.isEditing = !this.isEditing;
}

saveChanges(): void{
if (this.contact && this.contactForm.valid) {
const updatedContact = {id: this.contact.id, ...this.contactForm.value}

const index = this.contactService.contacts.findIndex(c => c.id === this.contact?.id)
if(index !== -1) {
this.contactService.contacts[index] = updatedContact;
this.contact = updatedContact;
}
this.isEditing = false;
}
}
}
11 changes: 11 additions & 0 deletions src/app/contacts/contactlist/contactlist.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
:host {
padding: 0.5em;
width: 400px;
}

.contact {
display: flex;
justify-content: space-between;
border-bottom: 1px solid #ddd;
padding: 0.5em 0;
}
15 changes: 15 additions & 0 deletions src/app/contacts/contactlist/contactlist.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h1>Contacts</h1>
<!-- if we have contacts -->
<ng-container *ngIf="contacts.length > 0;" >
<div class="contact-container">
<div class="contact" *ngFor="let contact of contacts">
<span>{{contact.firstname}} {{contact.lastname}}</span>
<!-- <a [routerLink]="['/contact-view', contact.id]">View</a> -->
<a routerLink="/contact/{{ contact.id }}">View</a>
</div>
</div>
</ng-container>
<!-- if we don't have contacts -->
<ng-container *ngIf="contacts.length == 0;">
<div>No contacts yet</div>
</ng-container>
17 changes: 17 additions & 0 deletions src/app/contacts/contactlist/contactlist.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Component } from '@angular/core';
import { Contact } from '../models/contact';
import { ContactsService } from '../contacts.service';

@Component({
selector: 'app-contactlist',
standalone: false,
templateUrl: './contactlist.component.html',
styleUrl: './contactlist.component.css'
})
export class ContactlistComponent {
contacts: Contact[]= [];
constructor(private readonly contactService: ContactsService){
this.contacts = this.contactService.contacts;
console.log("Contacts", this.contacts)
}
}
14 changes: 14 additions & 0 deletions src/app/contacts/contacts.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ContactlistComponent } from './contactlist/contactlist.component';
import { ContactViewComponent } from './contact-view/contact-view.component';
import { ContactFormComponent } from './contact-form/contact-form.component';
import { RouterModule } from '@angular/router';
import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
declarations: [ContactViewComponent, ContactlistComponent, ContactFormComponent],
imports: [CommonModule, ReactiveFormsModule, RouterModule],
exports: [ContactViewComponent, ContactlistComponent, ContactFormComponent],
})
export class ContactsModule {}
31 changes: 31 additions & 0 deletions src/app/contacts/contacts.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';

import { Contact } from './models/contact';
import { CONTACTS } from './data/contacts';

@Injectable({
providedIn: 'root',
})
export class ContactsService {
public contacts: Contact[] = CONTACTS;
private lastId:number;

constructor(){
this.lastId = this.contacts.length > 0 ? this.contacts[this.contacts.length-1].id ?? 0: 0;
}

public AddContact(contact: Contact): void {
this.lastId++;
contact.id = this.lastId;
console.log(contact)
this.contacts.push(contact);
}
GetContactById(id: number): Observable<Contact | undefined> {
const contact = this.contacts.find((b) => b.id === id);
return of(contact);
}


}
Loading