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
Empty file.
49 changes: 49 additions & 0 deletions src/app/contacts/add/add.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<div>
<h2>Add Contact</h2>
<form [formGroup]="contactForm" (ngSubmit)="addContact()" class="form-group">
<div>
<label for="name" class="form-control">Name:</label>
<input
type="text"
id="name"
formControlName="name"
class="form-control"
/>
</div>

<div>
<label for="phone" class="form-control">Phone:</label>
<input
type="text"
id="phone"
formControlName="phone"
class="form-control"
/>
</div>

<div>
<label for="email" class="form-control">Email:</label>
<input
type="text"
id="email"
formControlName="email"
class="form-control"
/>
</div>
<div>
<label for="address" class="form-control"
>Address:</label
>
<input
type="text"
id="address"
formControlName="address"
class="form-control"
/>
</div>

<button type="submit" [disabled]="contactForm.invalid" class="btn btn-success">
Add Contact
</button>
</form>
</div>
23 changes: 23 additions & 0 deletions src/app/contacts/add/add.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { AddComponent } from './add.component';

describe('AddComponent', () => {
let component: AddComponent;
let fixture: ComponentFixture<AddComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [AddComponent]
})
.compileComponents();

fixture = TestBed.createComponent(AddComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
40 changes: 40 additions & 0 deletions src/app/contacts/add/add.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Component } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { ContactsService } from '../contacts.service';
import { Contact } from 'src/app/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 contactService: ContactsService,
private readonly router: Router
){
this.contactForm = this.formBuilder.group({
name: ['', Validators.required],
phone: ['', Validators.required],
email: ['', Validators.required],
address: ['', Validators.required],
});
}
addContact(): void {
const contactsMax = Math.max(...this.contactService.contacts.map(c => c.id).filter((id): id is number => id !== null));
const newContact: Contact = {
id: contactsMax + 1,
name: this.contactForm.value.name,
phone: this.contactForm.value.phone,
email: this.contactForm.value.email,
address: this.contactForm.value.address
}
this.contactService.AddContact(newContact);
this.contactForm.reset();
this.router.navigate(['/contacts'])
}
}
16 changes: 16 additions & 0 deletions src/app/contacts/contacts.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ListComponent } from './list/list.component';
import { AddComponent } from './add/add.component';
import { ViewComponent } from './view/view.component';
import { RouterModule } from '@angular/router';
import { ReactiveFormsModule } from '@angular/forms';
import { EditComponent } from './edit/edit.component';


@NgModule({
declarations: [ListComponent, AddComponent, ViewComponent, EditComponent],
imports: [CommonModule, ReactiveFormsModule, RouterModule],
exports: [AddComponent, ListComponent, ViewComponent, EditComponent]
})
export class ContactsModule { }
37 changes: 37 additions & 0 deletions src/app/contacts/contacts.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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;

public AddContact(contact: Contact): void {
this.contacts.push(contact)
}

public GetContactById(id: number | null){
const contact = this.contacts.find((c) => c.id === id);
if(contact == null){
return null;
}
return contact;
}

public EditContact(contact:Contact): void {
const thisContact = this.GetContactById(contact.id)
if (thisContact != null){
thisContact.name = contact.name,
thisContact.phone = contact.phone,
thisContact.email = contact.email,
thisContact.address = contact.address

}
}

//constructor() { }
}
Empty file.
49 changes: 49 additions & 0 deletions src/app/contacts/edit/edit.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<div>
<h2>Edit Contact</h2>
<form [formGroup]="contactForm" (ngSubmit)="editContact()" class="form-group">
<div>
<label for="name" class="form-control">Name:</label>
<input
type="text"
id="name"
formControlName="name"
class="form-control"
/>
</div>

<div>
<label for="phone" class="form-control">Phone:</label>
<input
type="text"
id="phone"
formControlName="phone"
class="form-control"
/>
</div>

<div>
<label for="email" class="form-control">Email:</label>
<input
type="text"
id="email"
formControlName="email"
class="form-control"
/>
</div>
<div>
<label for="address" class="form-control"
>Address:</label
>
<input
type="text"
id="address"
formControlName="address"
class="form-control"
/>
</div>

<button type="submit" [disabled]="contactForm.invalid" class="btn btn-success">
Edit Contact
</button>
</form>
</div>
23 changes: 23 additions & 0 deletions src/app/contacts/edit/edit.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { EditComponent } from './edit.component';

describe('EditComponent', () => {
let component: EditComponent;
let fixture: ComponentFixture<EditComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [EditComponent]
})
.compileComponents();

fixture = TestBed.createComponent(EditComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
52 changes: 52 additions & 0 deletions src/app/contacts/edit/edit.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Component, OnInit } from '@angular/core';
import { Contact } from 'src/app/models/contact';
import { ContactsService } from '../contacts.service';
import { ActivatedRoute, Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
selector: 'app-edit',
standalone: false,
templateUrl: './edit.component.html',
styleUrl: './edit.component.css'
})
export class EditComponent implements OnInit {
contact: Contact | null = null;
contactId: number | null = null;
contactForm: FormGroup;

constructor(
private route: ActivatedRoute,
private readonly formBuilder: FormBuilder,
private readonly contactService: ContactsService,
private readonly router: Router
) {
this.contactId = Number(this.route.snapshot.paramMap.get('id'));
this.contact = this.contactService.GetContactById(this.contactId);
this.contactForm = this.formBuilder.group({
name: [this.contact?.name, Validators.required],
phone: [this.contact?.phone, Validators.required],
email: [this.contact?.email, Validators.required],
address: [this.contact?.address, Validators.required],
});
}

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


editContact(): void {
const editContact: Contact = {
id: this.contactId,
name: this.contactForm.value.name,
phone: this.contactForm.value.phone,
email: this.contactForm.value.email,
address: this.contactForm.value.address
}
this.contactService.EditContact(editContact);
this.contactForm.reset();
this.router.navigate(['/contacts'])
}
}
Empty file.
22 changes: 22 additions & 0 deletions src/app/contacts/list/list.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<table class="table table-striped">
<thead>
<td>Id</td>
<td>Name</td>
<td>Phone</td>
<td>Email</td>
<td>Address</td>
</thead>
<tbody>
<tr *ngFor="let contact of contacts">
<td>
<a routerLink="/contacts/{{ contact.id }}" class="btn btn-primary">
<span class=""badge>{{ contact.id }}</span>
</a>
</td>
<td> {{ contact.name }} </td>
<td> {{ contact.phone }} </td>
<td> {{ contact.email }} </td>
<td> {{ contact.address }} </td>
</tr>
</tbody>
</table>
23 changes: 23 additions & 0 deletions src/app/contacts/list/list.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { ListComponent } from './list.component';

describe('ListComponent', () => {
let component: ListComponent;
let fixture: ComponentFixture<ListComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [ListComponent]
})
.compileComponents();

fixture = TestBed.createComponent(ListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
17 changes: 17 additions & 0 deletions src/app/contacts/list/list.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Component } from '@angular/core';
import { Injectable } from '@angular/core';
import { Contact } from 'src/app/models/contact';
import { ContactsService } from '../contacts.service';

@Component({
selector: 'app-list',
standalone: false,
templateUrl: './list.component.html',
styleUrl: './list.component.css'
})
export class ListComponent {
contacts: Contact[] = [];
constructor(private readonly contactService: ContactsService){
this.contacts = this.contactService.contacts;
}
}
Empty file.
13 changes: 13 additions & 0 deletions src/app/contacts/view/view.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div *ngIf="contact; else loading">
<h2> {{ contact.name }} </h2>
<p>Phone: {{ contact.phone}} </p>
<p>Email: {{ contact.email}} </p>
<p>Address: {{ contact.address}} </p>

<button routerLink="/contacts"> Back to List </button>
<button [routerLink]="['/contacts', contact.id, 'edit']">Edit Contact</button>
</div>

<ng-template #loading>
<p>Loading contact details...</p>
</ng-template>
Loading