Skip to content

Commit b944267

Browse files
committed
/product-detail view, reactive form, new bs-select component: done
1 parent 4e05745 commit b944267

File tree

17 files changed

+270
-55
lines changed

17 files changed

+270
-55
lines changed

section_19-project/project/db/seeds/bread.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,36 @@ let collection = [
33
title: 'Pizza Slice',
44
imageUrl: 'https://www.dropbox.com/s/0dymh0nkdhccz6j/119627_01.jpg?raw=1',
55
price: 0.69,
6-
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
76
category: 'Bread',
87
},
98
{
109
title: 'Italian Style Panini Roll',
1110
imageUrl: 'https://www.dropbox.com/s/b03fg3pjd8m9e0z/1950_01.jpg?raw=1',
1211
price: 0.25,
13-
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
1412
category: 'Bread',
1513
},
1614
{
1715
title: 'French Baguette',
1816
imageUrl: 'https://www.dropbox.com/s/kgpaae6jahstelz/6742_01.jpg?raw=1',
1917
price: 0.69,
20-
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
2118
category: 'Bread',
2219
},
2320
{
2421
title: 'Maple and Pecan',
2522
imageUrl: 'https://www.dropbox.com/s/8wqgzxumpz9e99w/6817_01.jpg?raw=1',
2623
price: 0.49,
27-
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
2824
category: 'Bread',
2925
},
3026
{
3127
title: 'Double Chocolate Cookie',
3228
imageUrl: 'https://www.dropbox.com/s/lqjddrhhehdwiow/5207811_01.jpg?raw=1',
3329
price: 0.39,
34-
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
3530
category: 'Bread',
3631
},
3732
{
3833
title: 'All Butter Croisssant',
3934
imageUrl: 'https://www.dropbox.com/s/ojwtiirrdvo1mhs/121278_01.jpg?raw=1',
4035
price: 0.37,
41-
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
4236
category: 'Bread',
4337
},
4438
]
@@ -47,7 +41,29 @@ let collection = [
4741
collection = collection.map((item, index) => {
4842
return {
4943
...item,
50-
seqN: index + 1
44+
seqN: index + 1,
45+
categories: [
46+
{
47+
name: 'Bread',
48+
id: 1
49+
},
50+
{
51+
name: 'Dairy',
52+
id: 2
53+
},
54+
{
55+
name: 'Fruits',
56+
id: 3
57+
},
58+
{
59+
name: 'Seasoning and Spice',
60+
id: 4
61+
},
62+
{
63+
name: 'Vegetables',
64+
id: 5
65+
}
66+
]
5167
}
5268
})
5369

section_19-project/project/db/seeds/vegetables.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,30 @@ let collection = [
33
title: 'Oaklands Selection Pointed Red Peppers ',
44
imageUrl: 'https://www.dropbox.com/s/3uvy83gkftwtmln/82632_01.jpg?raw=1',
55
price: 1.05,
6-
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
76
category: 'Vegetables',
87
},
98
{
109
title: 'Oaklands Organic Vine Tomatoes',
1110
imageUrl: 'https://www.dropbox.com/s/7qe68yj8wyh0jd0/84570_01.jpg?raw=1',
1211
price: 1.60,
13-
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
1412
category: 'Vegetables',
1513
},
1614
{
1715
title: 'Oaklands Organic Carrots',
1816
imageUrl: 'https://www.dropbox.com/s/qf1597z38c1p2sy/84610_01.jpg?raw=1',
1917
price: 0.95,
20-
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
2118
category: 'Vegetables',
2219
},
2320
{
2421
title: 'Oaklands Baby Corn',
2522
imageUrl: 'https://www.dropbox.com/s/0cd780450mo01ho/82190_65.jpg?raw=1',
2623
price: 1.09,
27-
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
2824
category: 'Vegetables',
2925
},
3026
{
3127
title: 'Oaklands Selection British Piccolo Tomatoes',
3228
imageUrl: 'https://www.dropbox.com/s/daio1kdyrfw4jkp/82394_01.jpg?raw=1',
3329
price: 1.45,
34-
categories: ['Bread', 'Dairy', 'Fruits', 'Seasoning and Spice', 'Vegetables'],
3530
category: 'Vegetables',
3631
}
3732
]
@@ -40,7 +35,29 @@ let collection = [
4035
collection = collection.map((item, index) => {
4136
return {
4237
...item,
43-
seqN: index + 1
38+
seqN: index + 1,
39+
categories: [
40+
{
41+
name: 'Bread',
42+
id: 1
43+
},
44+
{
45+
name: 'Dairy',
46+
id: 2
47+
},
48+
{
49+
name: 'Fruits',
50+
id: 3
51+
},
52+
{
53+
name: 'Seasoning and Spice',
54+
id: 4
55+
},
56+
{
57+
name: 'Vegetables',
58+
id: 5
59+
}
60+
]
4461
}
4562
})
4663

section_19-project/project/src/app/app-routing.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { NgModule } from '@angular/core';
22
import { Routes, RouterModule } from '@angular/router';
33
import { ProductsIndexComponent } from './components/products/products-index/products-index.component';
4+
import { ProductsShowComponent } from './components/products/products-show/products-show.component';
45

56

67
const routes: Routes = [
8+
{ path: 'products/:id', component: ProductsShowComponent },
79
{ path: 'products', component: ProductsIndexComponent }
810
// { path: '**', component: PathNotFoundComponent }
911
];

section_19-project/project/src/app/app.module.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
// modules
12
import { BrowserModule } from '@angular/platform-browser';
23
import { NgModule } from '@angular/core';
3-
44
import { AppRoutingModule } from './app-routing.module';
5-
import { AppComponent } from './app.component';
6-
75
import { AngularFireModule } from '@angular/fire';
86
import { AngularFireAnalyticsModule } from '@angular/fire/analytics';
97
import { AngularFirestoreModule } from '@angular/fire/firestore';
8+
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
9+
10+
// extra
1011
import { environment } from '../environments/environment';
1112
import { DataService } from './services/data.service';
13+
// components
14+
import { AppComponent } from './app.component';
1215
import { ProductsIndexComponent } from './components/products/products-index/products-index.component';
1316
import { ProductsShowComponent } from './components/products/products-show/products-show.component';
1417
import { BootstrapFormComponent } from './reusable-components/bootstrap-form/bootstrap-form.component';
@@ -35,7 +38,8 @@ import { BootstrapCardGroupComponent } from './reusable-components/bootstrap-car
3538
AppRoutingModule,
3639
AngularFireModule.initializeApp(environment.firebase),
3740
AngularFirestoreModule,
38-
AngularFireAnalyticsModule
41+
AngularFireAnalyticsModule,
42+
ReactiveFormsModule
3943
],
4044
providers: [DataService],
4145
bootstrap: [AppComponent]

section_19-project/project/src/app/components/products/products-index/products-index.component.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<div class="row m-5">
22
<div class="col-4">
3-
<bootstrap-list (handleSelection)='handleSelection($event)' [props]='listGroup'></bootstrap-list>
3+
<bootstrap-list (handleSelectedLi)='handleSelectedLi($event)' [props]='listGroup'></bootstrap-list>
44
</div>
55
<div class="col-8">
66

7-
<div class="row row-cols-1 row-cols-md-3">
7+
<div class="row row-cols-md-2 row-cols-xl-3">
88
<bootstrap-cards-group *ngFor="let product of products">
9-
<img class="selector-header card-img-top" src={{product.imageUrl}} alt="{{product.title}}">
9+
<img (click)="navigateTo(product)" class="selector-header card-img-top" src={{product.imageUrl}} alt="{{product.title}}">
1010
<ng-container class="selector-body">
1111
<div class="card-body">
1212
<h5 class="card-title">{{product.title}}</h5>

section_19-project/project/src/app/components/products/products-index/products-index.component.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Component, OnInit, OnDestroy } from '@angular/core';
22
import { DataService } from '../../../services/data.service';
33
import { IProduct, ICategory, IListGroup } from '../interfaces';
44
import { Subscription } from 'rxjs';
5+
import { Router } from '@angular/router';
56

67
@Component({
78
selector: 'products-index',
@@ -18,7 +19,7 @@ export class ProductsIndexComponent implements OnInit, OnDestroy {
1819
// default apiCalled onload
1920
apiEndpoint: string = 'vegetables';
2021

21-
constructor(private service: DataService) { }
22+
constructor(private service: DataService, private router: Router) { }
2223

2324
getCategoriesMenu() {
2425
this.service.getAll('categories')
@@ -33,14 +34,19 @@ export class ProductsIndexComponent implements OnInit, OnDestroy {
3334
})
3435
}
3536

36-
handleSelection(obj: ICategory) {
37+
handleSelectedLi(obj: ICategory) {
3738
this.products = [];
3839
this.lastPageloaded = 0;
3940
const currentCategory = obj.categoryName.toLowerCase();
4041
this.apiEndpoint = currentCategory;
4142
this.getCollection();
4243
}
4344

45+
navigateTo(currentProduct: IProduct) {
46+
const endPoint = currentProduct.category.toLowerCase();
47+
this.router.navigate([`/products/${currentProduct.id}`], { state: { data: currentProduct } });
48+
}
49+
4450
getCollection() {
4551
this.subscription = this.service
4652
.getCollectionPaginated(this.apiEndpoint, 'seqN', "asc", this.lastPageloaded, 3)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const formTemplate = [
2+
{
3+
name: "title",
4+
type: "text",
5+
label: "Title",
6+
isRequired: false,
7+
minLenght: 0
8+
},
9+
{
10+
name: "price",
11+
type: "text",
12+
label: "Price",
13+
isRequired: false,
14+
minLenght: 0
15+
},
16+
{
17+
name: "categories",
18+
type: "select",
19+
label: "Categories",
20+
isRequired: false,
21+
minLenght: 0
22+
},
23+
{
24+
name: "imageUrl",
25+
type: "text",
26+
label: "Image Url",
27+
isRequired: false,
28+
minLenght: 0
29+
},
30+
];
31+
32+
export default formTemplate
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
<p>products-show works!</p>
1+
<div class="row mt-5">
2+
<div class="col">
3+
<bootstrap-form (handleSubmit)="handleSubmit($event)" [formGroup]='formGroup'>
4+
<bootstrap-input
5+
*ngFor="let item of inputs"
6+
[formGroup]='formGroup'
7+
[name]='item.name'
8+
[type]='item.type'
9+
[label]='item.label'>
10+
</bootstrap-input>
11+
12+
<bootstrap-select *ngFor="let item of selects"
13+
[formGroup]='formGroup'
14+
[options]='product.categories'
15+
[optionKey]="'name'"
16+
[name]='item.name'
17+
[label]='item.label'>
18+
</bootstrap-select>
19+
</bootstrap-form>
20+
</div>
21+
</div>
Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,57 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { IProduct } from '../interfaces';
3+
import { BootstrapFormComponent } from '../../../reusable-components/bootstrap-form/bootstrap-form.component';
4+
import formTemplate from './form-template';
25

36
@Component({
4-
selector: 'app-products-show',
5-
templateUrl: './products-show.component.html',
6-
styleUrls: ['./products-show.component.scss']
7+
selector: 'products-show',
8+
templateUrl: './products-show.component.html',
9+
styleUrls: ['./products-show.component.scss']
710
})
8-
export class ProductsShowComponent implements OnInit {
11+
export class ProductsShowComponent extends BootstrapFormComponent implements OnInit {
912

10-
constructor() { }
13+
constructor() {
14+
super()
15+
}
16+
17+
product: IProduct;
18+
19+
productDetail() {
20+
const { data } = history.state;
21+
if (data) {
22+
localStorage.setItem('product', JSON.stringify(data));
23+
this.product = data;
24+
} else {
25+
// this will prevent a blank page on refresh
26+
const fetchDataFromLocalStorage = JSON.parse(localStorage.getItem('product'));
27+
this.product = fetchDataFromLocalStorage;
28+
}
29+
console.log(this.product);
30+
// this.productEdit(this.product)
31+
}
32+
33+
productEdit(product: IProduct) {
34+
console.log(product);
35+
this.formGroup.setValue({
36+
title: product.title,
37+
imageUrl: product.imageUrl,
38+
price: product.price,
39+
categories: product.categories,
40+
// category: product.category
41+
})
42+
43+
}
44+
45+
handleSubmit(isSubmitted: boolean) {
46+
const { value } = this.formGroup;
47+
console.log(value)
48+
49+
}
50+
51+
ngOnInit(): void {
52+
this.formMaker(formTemplate);
53+
this.productDetail();
54+
}
1155

12-
ngOnInit(): void {
13-
}
1456

1557
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
<p>bootstrap-form works!</p>
1+
<form (ngSubmit)="submit()" [formGroup]='formGroup'>
2+
<ng-content></ng-content>
3+
<button type="submit" class="btn btn-primary">Submit</button>
4+
<!-- <button [disabled]="!formGroup.valid" type="submit" class="btn btn-primary">Submit</button> -->
5+
</form>

0 commit comments

Comments
 (0)