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": "5e68f19b-492b-43bf-a083-d2e199b34a6d"
}
}
11 changes: 10 additions & 1 deletion src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
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';
import { EditComponent } from './contacts/edit/edit.component';

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

@NgModule({
imports: [RouterModule.forRoot(routes)],
Expand Down
3 changes: 2 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ 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';

@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AppRoutingModule, LayoutModule],
imports: [BrowserModule, AppRoutingModule, LayoutModule, ContactsModule],
bootstrap: [AppComponent],
})
export class AppModule {}
25 changes: 25 additions & 0 deletions src/app/contacts/add/add.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/add/add.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" type="text" formControlName="firstName"/>

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

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

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

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

@Component({
selector: 'app-add',
standalone: false,
templateUrl: './add.component.html',
styleUrl: './add.component.css',
})

export class AddComponent {
contactForm: FormGroup;
cservice: ContactsService;
constructor(
private formBuilder: FormBuilder,
private readonly contactService: ContactsService,
private readonly router: Router
) {
this.contactForm = this.formBuilder.group({
firstName: ['', Validators.required],
lastName: ['', Validators.required],
city: ['', Validators.required],
street: ['', Validators.required],
});
this.cservice = this.contactService;
}

addContact(): void {
const newContact: Contact = {
id: 0,
firstName: this.contactForm.value.firstName,
lastName: this.contactForm.value.lastName,
city: this.contactForm.value.city,
street: this.contactForm.value.street,
};
console.log(newContact)
this.cservice.AddContact(newContact);
this.contactForm.reset();
this.router.navigate(['/contacts']);
}
}
15 changes: 15 additions & 0 deletions src/app/contacts/contacts.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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 { ReactiveFormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';
import { EditComponent } from './edit/edit.component';

@NgModule({
declarations: [AddComponent, ViewComponent, ListComponent, EditComponent],
imports: [CommonModule, ReactiveFormsModule, RouterModule],
exports: [AddComponent, ViewComponent, ListComponent, EditComponent],
})
export class ContactsModule {}
16 changes: 16 additions & 0 deletions src/app/contacts/contacts.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { ContactsService } from './contacts.service';

describe('ContactsService', () => {
let service: ContactsService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ContactsService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
25 changes: 25 additions & 0 deletions src/app/contacts/contacts.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Injectable } from '@angular/core';
import { CONTACTS } from '../data/contacts';
import { HttpClient } from '@angular/common/http';
import { Contact } from '../models/contacts';
import { Observable, of } from 'rxjs';

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

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

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

EditContact(newContact: Contact) {
}

}
25 changes: 25 additions & 0 deletions src/app/contacts/edit/edit.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;
}
20 changes: 20 additions & 0 deletions src/app/contacts/edit/edit.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h1>Edit Contact</h1>
<form [formGroup]="contactForm" (ngSubmit)="editContact()">
<!-- <form> -->
<label for="first-name">First name:</label>
<input id="first-name" type="text" formControlName="firstName"/>

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

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

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

<div class="actions">
<div class="spacer"></div>
<button type="submit">Create</button>
</div>
</form>
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();
});
});
46 changes: 46 additions & 0 deletions src/app/contacts/edit/edit.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ContactsService } from '../contacts.service';
import { Contact } from 'src/app/models/contacts';
import { Router } from '@angular/router';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ReactiveFormsModule } from '@angular/forms';


@Component({
selector: 'app-edit',
standalone: false,
templateUrl: './edit.component.html',
styleUrl: './edit.component.css'
})
export class EditComponent {
contactForm: FormGroup;
cservice: ContactsService;
constructor(
private formBuilder: FormBuilder,
private readonly contactService: ContactsService,
private readonly router: Router
) {
this.contactForm = this.formBuilder.group({
firstName: ['', Validators.required],
lastName: ['', Validators.required],
city: ['', Validators.required],
street: ['', Validators.required],
});
this.cservice = this.contactService;
}

editContact(): void {
const newContact: Contact = {
id: 0,
firstName: this.contactForm.value.firstName,
lastName: this.contactForm.value.lastName,
city: this.contactForm.value.city,
street: this.contactForm.value.street,
};
this.cservice.AddContact(newContact);
this.contactForm.reset();
this.router.navigate(['/contacts']);
}
}
Empty file.
11 changes: 11 additions & 0 deletions src/app/contacts/list/list.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>Contacts</h1>
<!-- if we have contacts -->
<div class="contact-container">
<div class="contact">
<p *ngFor="let contact of contacts" >
<a routerLink="/contacts/{{ contact.id }}">{{contact.firstName}} {{contact.lastName}}</a>
<a routerLink="/contacts/edit/{{ contact.id }}"><button>edit</button></a>
</p>
</div>
</div>
<!-- if we don't have contacts -->
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();
});
});
20 changes: 20 additions & 0 deletions src/app/contacts/list/list.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Injectable } from '@angular/core';
import { Component } from '@angular/core';
import { Contact } from 'src/app/models/contacts';
import { ContactsService } from '../contacts.service';

@Component({
selector: 'app-list',
standalone: false,
// imports: [],
templateUrl: './list.component.html',
styleUrl: './list.component.css'
})
export class ListComponent {
contacts: Contact[] = [];

constructor(private readonly contactService: ContactsService) {
this.contacts = this.contactService.contacts;
console.log(this.contacts)
}
}
Empty file.
Loading