diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 02972627..ccd3a200 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -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)], diff --git a/src/app/app.component.html b/src/app/app.component.html index 17aaa0c6..84d4a91e 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,4 +1,3 @@ +
-
-
diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 8207184c..5e5b5a9b 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -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 { CommonModule } from '@angular/common'; +import { ContactsModule } from './contacts/contacts.module'; @NgModule({ declarations: [AppComponent], - imports: [BrowserModule, AppRoutingModule, LayoutModule], + imports: [BrowserModule, AppRoutingModule, LayoutModule, CommonModule, LayoutModule, ContactsModule], bootstrap: [AppComponent], }) export class AppModule {} diff --git a/src/app/contacts.service.ts b/src/app/contacts.service.ts new file mode 100644 index 00000000..7f6b414a --- /dev/null +++ b/src/app/contacts.service.ts @@ -0,0 +1,9 @@ +import { Injectable } from '@angular/core'; + +@Injectable({ + providedIn: 'root' +}) +export class ContactsService { + + constructor() { } +} diff --git a/src/app/contacts/add/add.component.css b/src/app/contacts/add/add.component.css new file mode 100644 index 00000000..04068da6 --- /dev/null +++ b/src/app/contacts/add/add.component.css @@ -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; +} diff --git a/src/app/contacts/add/add.component.html b/src/app/contacts/add/add.component.html new file mode 100644 index 00000000..9b826e51 --- /dev/null +++ b/src/app/contacts/add/add.component.html @@ -0,0 +1,16 @@ +

Create Contact

+
+ + + + + + + + + +
+
+ +
+
diff --git a/src/app/contacts/add/add.component.ts b/src/app/contacts/add/add.component.ts new file mode 100644 index 00000000..59ed3b98 --- /dev/null +++ b/src/app/contacts/add/add.component.ts @@ -0,0 +1,31 @@ +import { Component, inject } from '@angular/core'; +import { FormBuilder, FormGroup, Validators } from '@angular/forms'; +import { contactService } from 'src/app/services/contact-service'; +import { Router } from '@angular/router'; + +@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({ + name: ['', Validators.required], + email: ['', Validators.required], + description: ['', Validators.required], + }); + } + + addContact() { + this.contactService.addContact(this.contactForm.value) + this.router.navigate(['contacts']) + } +} diff --git a/src/app/contacts/contacts.module.ts b/src/app/contacts/contacts.module.ts new file mode 100644 index 00000000..ede1f6b3 --- /dev/null +++ b/src/app/contacts/contacts.module.ts @@ -0,0 +1,32 @@ +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 { EditComponent } from './edit/edit.component'; +import { RouterModule } from '@angular/router'; +import { ReactiveFormsModule } from '@angular/forms'; + + + +@NgModule({ + declarations: [ + AddComponent, + ViewComponent, + ListComponent, + EditComponent + ], + imports: [ + CommonModule, + RouterModule, + ReactiveFormsModule + ], + exports:[ + AddComponent, + ViewComponent, + ListComponent, + EditComponent + ] + +}) +export class ContactsModule { } diff --git a/src/app/contacts/data/contacts.ts b/src/app/contacts/data/contacts.ts new file mode 100644 index 00000000..c8ebdcbc --- /dev/null +++ b/src/app/contacts/data/contacts.ts @@ -0,0 +1,22 @@ +import { Contact } from "../models/contact"; + +export const CONSTACTS: Contact[] = [ + { + id: 1, + name: 'Porsche', + email: '911', + description: 'Mid-life crisis in car form', + }, + { + id: 2, + name: 'Volks Wagen', + email: 'Beetle', + description: 'Herbie', + }, + { + id: 3, + name: 'Mini', + email: 'Cooper', + description: 'Tiny car', + }, +]; \ No newline at end of file diff --git a/src/app/contacts/edit/edit.component.css b/src/app/contacts/edit/edit.component.css new file mode 100644 index 00000000..e69de29b diff --git a/src/app/contacts/edit/edit.component.html b/src/app/contacts/edit/edit.component.html new file mode 100644 index 00000000..d1393d89 --- /dev/null +++ b/src/app/contacts/edit/edit.component.html @@ -0,0 +1 @@ +

edit works!

diff --git a/src/app/contacts/edit/edit.component.ts b/src/app/contacts/edit/edit.component.ts new file mode 100644 index 00000000..3d890bfc --- /dev/null +++ b/src/app/contacts/edit/edit.component.ts @@ -0,0 +1,10 @@ +import { Component } from '@angular/core'; + +@Component({ + selector: 'app-edit', + templateUrl: './edit.component.html', + styleUrl: './edit.component.css' +}) +export class EditComponent { + +} diff --git a/src/app/contacts/list/list.component.css b/src/app/contacts/list/list.component.css new file mode 100644 index 00000000..a7943571 --- /dev/null +++ b/src/app/contacts/list/list.component.css @@ -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; +} diff --git a/src/app/contacts/list/list.component.html b/src/app/contacts/list/list.component.html new file mode 100644 index 00000000..2e92b54f --- /dev/null +++ b/src/app/contacts/list/list.component.html @@ -0,0 +1,18 @@ +

Contacts

+ + @if (this.contacts.length >0){ +@for (item of contacts; track $index) { +
+
+ {{item.name}} + View +
+
+} + +} +@else { + + +
No contacts yet
+} diff --git a/src/app/contacts/list/list.component.ts b/src/app/contacts/list/list.component.ts new file mode 100644 index 00000000..c7138beb --- /dev/null +++ b/src/app/contacts/list/list.component.ts @@ -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', + templateUrl: './list.component.html', + styleUrl: './list.component.css' +}) +export class ListComponent { + contactService = inject(contactService) + contacts: Contact[] = this.contactService.getAllContacts(); + +} diff --git a/src/app/contacts/models/contact.ts b/src/app/contacts/models/contact.ts new file mode 100644 index 00000000..5a7873a4 --- /dev/null +++ b/src/app/contacts/models/contact.ts @@ -0,0 +1,6 @@ +export interface Contact { + id: number; + name: string; + email: string; + description: string; +} \ No newline at end of file diff --git a/src/app/contacts/view/view.component.css b/src/app/contacts/view/view.component.css new file mode 100644 index 00000000..e69de29b diff --git a/src/app/contacts/view/view.component.html b/src/app/contacts/view/view.component.html new file mode 100644 index 00000000..2fa58963 --- /dev/null +++ b/src/app/contacts/view/view.component.html @@ -0,0 +1,14 @@ + + + @if (contact) { + +

{{ contact.name}}

+

{{ contact.email }}, {{ contact.description }}

+
+ } @else { + + +

Contact not found

+
+ } diff --git a/src/app/contacts/view/view.component.ts b/src/app/contacts/view/view.component.ts new file mode 100644 index 00000000..affb1887 --- /dev/null +++ b/src/app/contacts/view/view.component.ts @@ -0,0 +1,17 @@ +import { Component, inject } from '@angular/core'; +import { Contact } from '../models/contact'; +import { contactService } from 'src/app/services/contact-service'; +import { ActivatedRoute } from '@angular/router'; + +@Component({ + selector: 'app-view', + 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)); + +} diff --git a/src/app/layout/menu/menu.component.html b/src/app/layout/menu/menu.component.html index 7c5ec7a2..b2401662 100644 --- a/src/app/layout/menu/menu.component.html +++ b/src/app/layout/menu/menu.component.html @@ -1,5 +1,6 @@

Menu

diff --git a/src/app/services/contact-service.ts b/src/app/services/contact-service.ts new file mode 100644 index 00000000..9259ae8a --- /dev/null +++ b/src/app/services/contact-service.ts @@ -0,0 +1,30 @@ +import { Injectable } from "@angular/core"; +import { CONSTACTS } from "../contacts/data/contacts"; +import { Contact } from "../contacts/models/contact"; + + + + +@Injectable({ + providedIn: 'root' +}) +export class contactService { + private contacts: Contact[] = CONSTACTS + 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}) + + } +} \ No newline at end of file