Skip to content

Commit 55bf997

Browse files
WarrenLynesjrista
authored andcommitted
feat(+products): Added +products
1 parent 4dfdc33 commit 55bf997

28 files changed

+473
-12
lines changed

data/db.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,14 @@
11031103
{
11041104
"name": "Jon's Customer",
11051105
"id": 101
1106+
},
1107+
{
1108+
"name": "bil",
1109+
"id": 102
1110+
},
1111+
{
1112+
"name": "asdfasdf",
1113+
"id": 103
11061114
}
11071115
],
11081116
"accounts": [
@@ -3893,6 +3901,10 @@
38933901
"price": "197.00",
38943902
"color": "black",
38953903
"details": "Awesome"
3904+
},
3905+
{
3906+
"name": "NEW PRODUCT",
3907+
"id": 201
38963908
}
38973909
],
38983910
"orders": [

projects/ngrx-auto-entity/src/lib/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export const buildState = <TState extends IEntityState<TModel>, TParentState, TM
291291
}
292292
};
293293

294-
const reducer = function(state = initialState): IEntityState<TModel> {
294+
const reducer = (state = initialState): IEntityState<TModel> => {
295295
// tslint:disable-line
296296
return state;
297297
};
@@ -482,7 +482,7 @@ export const buildFeatureState = <TState extends IEntityState<TModel>, TParentSt
482482
}
483483
};
484484

485-
const reducer = function(state = initialState): IEntityState<TModel> {
485+
const reducer = (state = initialState): IEntityState<TModel> => {
486486
// tslint:disable-line
487487
return state;
488488
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<mat-card>
2+
<mat-card-content>
3+
<form [formGroup]="formGroup">
4+
<mat-form-field class="product-input">
5+
<input matInput type="text" placeholder="Product Name" formControlName="name">
6+
</mat-form-field>
7+
8+
<mat-form-field class="product-input">
9+
<input matInput type="number" placeholder="Product Price" formControlName="price">
10+
</mat-form-field>
11+
12+
<mat-form-field class="product-input">
13+
<input matInput type="text" placeholder="Product Details" formControlName="details">
14+
</mat-form-field>
15+
16+
<mat-error *ngIf="formGroup.get('name').hasError('required')">
17+
Name is required
18+
</mat-error>
19+
</form>
20+
</mat-card-content>
21+
</mat-card>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.customer-input {
2+
width: 100%;
3+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Component, EventEmitter, Input, OnChanges, OnDestroy, Output, SimpleChanges } from '@angular/core';
2+
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
3+
import { Product } from 'models/product.model';
4+
import { Subject } from 'rxjs';
5+
import { takeUntil } from 'rxjs/operators';
6+
7+
@Component({
8+
selector: 'app-product-form',
9+
templateUrl: './product-form.component.html',
10+
styleUrls: ['./product-form.component.scss']
11+
})
12+
export class ProductFormComponent implements OnChanges, OnDestroy {
13+
/** The product */
14+
@Input() product: Product;
15+
16+
@Output() productChange = new EventEmitter<{ product: Product; valid: boolean }>();
17+
/** Event emitter for a product change */
18+
19+
/** The product form */
20+
formGroup: FormGroup;
21+
22+
/** Subject to trigger unsubscribe */
23+
private unsubscribe = new Subject<void>();
24+
25+
constructor(private formBuilder: FormBuilder) {
26+
this.buildForm();
27+
}
28+
29+
ngOnChanges(changes: SimpleChanges) {
30+
if (changes.product && changes.product.currentValue) {
31+
this.formGroup.patchValue(this.product);
32+
}
33+
}
34+
35+
ngOnDestroy() {
36+
this.unsubscribe.next();
37+
this.unsubscribe.complete();
38+
}
39+
40+
private buildForm() {
41+
this.formGroup = this.formBuilder.group({
42+
name: ['', Validators.required],
43+
details: ['', Validators.required],
44+
price: [0, Validators.required]
45+
});
46+
47+
this.formGroup.valueChanges.pipe(takeUntil(this.unsubscribe)).subscribe(value => {
48+
this.productChange.emit({
49+
product: {
50+
...this.product,
51+
...value
52+
},
53+
valid: this.formGroup.valid
54+
});
55+
});
56+
}
57+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<mat-form-field class="filter-input">
2+
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
3+
</mat-form-field>
4+
5+
<div class="mat-elevation-z8">
6+
<mat-table [dataSource]="dataSource">
7+
<ng-container matColumnDef="name">
8+
<mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
9+
<mat-cell *matCellDef="let product"> {{product.name}} </mat-cell>
10+
</ng-container>
11+
<ng-container matColumnDef="details">
12+
<mat-header-cell *matHeaderCellDef> Details </mat-header-cell>
13+
<mat-cell *matCellDef="let product"> {{product.details}} </mat-cell>
14+
</ng-container>
15+
<ng-container matColumnDef="price">
16+
<mat-header-cell *matHeaderCellDef> Price </mat-header-cell>
17+
<mat-cell *matCellDef="let product"> {{product.price}} </mat-cell>
18+
</ng-container>
19+
<ng-container matColumnDef="actions">
20+
<mat-header-cell *matHeaderCellDef></mat-header-cell>
21+
<mat-cell *matCellDef="let product">
22+
<button mat-icon-button [matMenuTriggerFor]="menu">
23+
<mat-icon>more_vert</mat-icon>
24+
</button>
25+
<mat-menu #menu="matMenu">
26+
<button mat-menu-item (click)="edit.emit(product)">Edit</button>
27+
<button mat-menu-item (click)="delete.emit(product)">Delete</button>
28+
</mat-menu>
29+
</mat-cell>
30+
</ng-container>
31+
<mat-header-row *matHeaderRowDef="columnsToDisplay"></mat-header-row>
32+
<mat-row *matRowDef="let product; columns: columnsToDisplay"></mat-row>
33+
</mat-table>
34+
<mat-paginator [pageSizeOptions]="[5, 10, 25, 100]" showFirstLastButtons></mat-paginator>
35+
</div>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@import '~@angular/material/theming';
2+
3+
.filter-input {
4+
width: 100%;
5+
}
6+
7+
mat-row:hover {
8+
background-color: mat-color($mat-grey, 200);
9+
cursor: pointer;
10+
}
11+
12+
.mat-column-actions {
13+
flex: 0 0 40px;
14+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core';
2+
import { MatPaginator, MatTableDataSource } from '@angular/material';
3+
import { Product } from 'models/product.model';
4+
5+
@Component({
6+
selector: 'app-products-table',
7+
templateUrl: './products-table.component.html',
8+
styleUrls: ['./products-table.component.scss']
9+
})
10+
export class ProductsTableComponent implements OnChanges, OnInit {
11+
@Input() products: Product[];
12+
@Output() delete = new EventEmitter<Product>();
13+
@Output() edit = new EventEmitter<Product>();
14+
15+
@ViewChild(MatPaginator) paginator: MatPaginator;
16+
17+
columnsToDisplay = ['name', 'details', 'price', 'actions'];
18+
dataSource = new MatTableDataSource();
19+
20+
constructor() {}
21+
22+
applyFilter(filter: string) {
23+
this.dataSource.filter = filter.trim().toLowerCase();
24+
}
25+
26+
ngOnChanges(simpleChanges: SimpleChanges) {
27+
if (simpleChanges.products && simpleChanges.products.currentValue) {
28+
this.dataSource.data = this.products;
29+
}
30+
}
31+
32+
ngOnInit() {
33+
this.dataSource.paginator = this.paginator;
34+
}
35+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div fxLayout="row" fxLayoutAlign="center stretch" class="products">
2+
<div fxFlex.xs="90%" fxFlex.sm="80%" fxFlex.md="70%" fxFlex.lg="65%" fxFlex.xl="60%">
3+
<h2>Product</h2>
4+
<app-product-form [product]="product | async" (productChange)="onProductChange($event)"></app-product-form>
5+
<div class="actions" fxLayout="row" fxLayoutAlign="end center">
6+
<button mat-button color="secondary" routerLink="/products">Cancel</button>
7+
<button mat-raised-button color="primary" (click)="onSave()" [disabled]="!valid">Save</button>
8+
</div>
9+
</div>
10+
</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.products {
2+
padding: 15px;
3+
4+
.actions {
5+
margin: 15px 0;
6+
7+
button + button {
8+
margin-left: 8px;
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)