Skip to content

Commit ff946b0

Browse files
committed
/product-detail: Update pruduct + batched writes: done
1 parent b944267 commit ff946b0

File tree

6 files changed

+139
-26
lines changed

6 files changed

+139
-26
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function create(db, path, id, obj) {
2+
const batch = db.firestore.batch();
3+
const ref = db.doc(`/${path}/${id}`).ref;
4+
5+
batch.set(ref, obj);
6+
batch.commit();
7+
}
8+
9+
function remove(db, path, id) {
10+
const batch = db.firestore.batch();
11+
const ref = db.doc(`/${path}/${id}`).ref;
12+
13+
batch.delete(ref);
14+
batch.commit();
15+
}
16+
17+
function update(db, path, id, obj) {
18+
const batch = db.firestore.batch();
19+
const ref = db.doc(`/${path}/${id}`).ref;
20+
21+
batch.update(ref, obj);
22+
batch.commit();
23+
}
24+
25+
export default {
26+
create,
27+
remove,
28+
update
29+
}

section_19-project/project/src/app/components/products/interfaces.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ export interface IProduct {
22
title: string;
33
imageUrl: string;
44
price: number;
5-
categories: string[];
5+
categories: ICategory[];
66
category: string;
77
id: string;
88
count: number;
99
isOpen: boolean;
10+
seqN: number;
1011
}
1112

12-
export interface ICategory {
13+
export interface ICategoryMenu {
1314
categoryName: string;
1415
isActive: boolean;
1516
id: string;
@@ -19,4 +20,9 @@ export interface ICategory {
1920
export interface IListGroup {
2021
list: any[];
2122
key: string;
23+
}
24+
25+
export interface ICategory {
26+
name: string;
27+
id: number;
2228
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, OnInit, OnDestroy } from '@angular/core';
22
import { DataService } from '../../../services/data.service';
3-
import { IProduct, ICategory, IListGroup } from '../interfaces';
3+
import { IProduct, ICategoryMenu, IListGroup } from '../interfaces';
44
import { Subscription } from 'rxjs';
55
import { Router } from '@angular/router';
66

@@ -14,7 +14,7 @@ export class ProductsIndexComponent implements OnInit, OnDestroy {
1414
subscription: Subscription;
1515
listGroup: IListGroup;
1616
products: IProduct[] = [];
17-
categories: ICategory[] = [];
17+
categories: ICategoryMenu[] = [];
1818
lastPageloaded: number = 0;
1919
// default apiCalled onload
2020
apiEndpoint: string = 'vegetables';
@@ -23,7 +23,7 @@ export class ProductsIndexComponent implements OnInit, OnDestroy {
2323

2424
getCategoriesMenu() {
2525
this.service.getAll('categories')
26-
.subscribe((response: ICategory[]) => {
26+
.subscribe((response: ICategoryMenu[]) => {
2727
const addCssClass = response.map((item: any) => {
2828
return {
2929
...item,
@@ -34,7 +34,7 @@ export class ProductsIndexComponent implements OnInit, OnDestroy {
3434
})
3535
}
3636

37-
handleSelectedLi(obj: ICategory) {
37+
handleSelectedLi(obj: ICategoryMenu) {
3838
this.products = [];
3939
this.lastPageloaded = 0;
4040
const currentCategory = obj.categoryName.toLowerCase();
@@ -43,7 +43,6 @@ export class ProductsIndexComponent implements OnInit, OnDestroy {
4343
}
4444

4545
navigateTo(currentProduct: IProduct) {
46-
const endPoint = currentProduct.category.toLowerCase();
4746
this.router.navigate([`/products/${currentProduct.id}`], { state: { data: currentProduct } });
4847
}
4948

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,114 @@
1-
import { Component, OnInit } from '@angular/core';
2-
import { IProduct } from '../interfaces';
1+
import { Component, OnInit, OnDestroy } from '@angular/core';
2+
import { Subject } from 'rxjs';
33
import { BootstrapFormComponent } from '../../../reusable-components/bootstrap-form/bootstrap-form.component';
4+
import { DataService } from '../../../services/data.service';
5+
import { AngularFirestore } from '@angular/fire/firestore';
6+
import { IProduct, ICategory } from '../interfaces';
47
import formTemplate from './form-template';
8+
import * as fsBatchedWrites from '../batched-writes';
9+
import { takeUntil } from 'rxjs/operators';
510

611
@Component({
712
selector: 'products-show',
813
templateUrl: './products-show.component.html',
914
styleUrls: ['./products-show.component.scss']
1015
})
11-
export class ProductsShowComponent extends BootstrapFormComponent implements OnInit {
16+
export class ProductsShowComponent extends BootstrapFormComponent implements OnInit, OnDestroy {
1217

13-
constructor() {
18+
constructor(private service: DataService, private db: AngularFirestore) {
1419
super()
1520
}
1621

17-
product: IProduct;
22+
public id: string;
23+
public apiEndPoint: string;
24+
public product: IProduct;
25+
private destroyed$: Subject<boolean> = new Subject();
1826

19-
productDetail() {
27+
28+
productDetail(): void {
2029
const { data } = history.state;
2130
if (data) {
2231
localStorage.setItem('product', JSON.stringify(data));
2332
this.product = data;
2433
} else {
25-
// this will prevent a blank page on refresh
2634
const fetchDataFromLocalStorage = JSON.parse(localStorage.getItem('product'));
2735
this.product = fetchDataFromLocalStorage;
2836
}
29-
console.log(this.product);
30-
// this.productEdit(this.product)
37+
this.setFormValue(this.product);
38+
this.apiEndPoint = this.product.category.toLowerCase();
39+
this.id = this.product.id;
3140
}
3241

33-
productEdit(product: IProduct) {
34-
console.log(product);
42+
setFormValue(product: IProduct): void {
3543
this.formGroup.setValue({
3644
title: product.title,
3745
imageUrl: product.imageUrl,
3846
price: product.price,
39-
categories: product.categories,
40-
// category: product.category
41-
})
47+
categories: this.preSelectCategory()
48+
});
49+
}
4250

51+
preSelectCategory(): ICategory {
52+
const { categories, category } = this.product;
53+
const current: string = category.toLowerCase();
54+
return categories.find(item => item.name.toLowerCase() === current);
4355
}
4456

45-
handleSubmit(isSubmitted: boolean) {
46-
const { value } = this.formGroup;
47-
console.log(value)
48-
57+
productEdit(resource: IProduct) {
58+
this.service.update(this.apiEndPoint, this.id, resource);
59+
}
60+
61+
handleSubmit(isSubmitted: boolean): void {
62+
63+
if (isSubmitted) {
64+
const { value } = this.formGroup;
65+
const payload = { ...this.product };
66+
67+
payload.imageUrl = value.imageUrl;
68+
payload.price = value.price;
69+
payload.title = value.title;
70+
71+
if (payload.category === value.categories.name) {
72+
this.productEdit(payload);
73+
this.refreshStorage();
74+
} else {
75+
payload.category = value.categories.name;
76+
77+
let newApiEndPoint: string = value.categories.name;
78+
newApiEndPoint = newApiEndPoint.replace(/ /g, '').toLowerCase();
79+
80+
this.service.getCollectionOrderBy(newApiEndPoint, 'seqN', "desc")
81+
.pipe(takeUntil(this.destroyed$))
82+
.subscribe((res: any) => {
83+
const seqNHighest = res[0].seqN;
84+
payload.seqN = seqNHighest + 1;
85+
fsBatchedWrites.default.remove(this.db, this.apiEndPoint, this.id);
86+
fsBatchedWrites.default.create(this.db, newApiEndPoint, this.id, payload);
87+
});
88+
}
89+
90+
}
91+
this.formGroup.reset();
92+
}
93+
94+
refreshStorage(): void {
95+
this.service.getItem(this.apiEndPoint, this.id)
96+
.pipe(takeUntil(this.destroyed$))
97+
.subscribe((response: any) => {
98+
this.product = response;
99+
localStorage.setItem('product', JSON.stringify(this.product));
100+
});
49101
}
50102

51103
ngOnInit(): void {
52104
this.formMaker(formTemplate);
53105
this.productDetail();
54106
}
55107

108+
public ngOnDestroy(): void {
109+
this.destroyed$.next(true);
110+
this.destroyed$.complete();
111+
}
112+
56113

57114
}

section_19-project/project/src/app/reusable-components/bootstrap-select/bootstrap-select.component.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<div class="form-group">
33
<label [for]="label">{{label}}</label>
44
<select [formControlName]="name" class="form-control">
5-
<option></option>
65
<option *ngFor="let option of options" [ngValue]="option">
76
{{ option[optionKey] }}
87
</option>

section_19-project/project/src/app/services/data.service.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,32 @@ export class DataService {
4747
)
4848
}
4949

50+
getCollectionOrderBy(
51+
collectioName: string,
52+
key: string,
53+
sortOrder: OrderByDirection,
54+
55+
) {
56+
const response: AngularFirestoreCollection<ID> = this.db.collection(
57+
collectioName,
58+
ref => ref
59+
.orderBy(key, sortOrder)
60+
);
61+
62+
return response.snapshotChanges()
63+
.pipe(
64+
map(snaps => snapsCoverter(snaps)),
65+
first()
66+
)
67+
}
68+
5069
getItem(collectioName: string, id: string) {
5170
return this.db.collection(collectioName).doc(id).valueChanges();
5271
}
5372

73+
update(collectioName: string, id: string, resource) {
74+
return this.db.collection(collectioName).doc(id).set(resource);
75+
}
76+
5477

5578
}

0 commit comments

Comments
 (0)