Added editing categories (Fixes #6)

This commit is contained in:
Zakary Timson 2018-07-15 13:17:00 -04:00
parent dfa54c9f50
commit 4d6bfc5f4d
4 changed files with 44 additions and 16 deletions

View File

@ -1,11 +1,11 @@
<div class="container-fluid" style="background-color: #53709f;"> <div class="container-fluid" style="background-color: #53709f;">
<div class="container"> <div class="container">
<div class="row py-5"> <div class="row py-5">
<div class="ml-auto mr-3 mb-3"> <div *ngIf="app.user" class="ml-auto mr-3 mb-3">
<button *ngIf="app.user" mat-raised-button (click)="create()"> <button mat-raised-button (click)="create()">
<mat-icon>playlist_add</mat-icon> Category <mat-icon>playlist_add</mat-icon> Category
</button> </button>
<button *ngIf="app.user" mat-raised-button class="ml-3"> <button mat-raised-button class="ml-3">
<mat-icon>note_add</mat-icon> Item <mat-icon>note_add</mat-icon> Item
</button> </button>
</div> </div>
@ -17,12 +17,22 @@
</ol> </ol>
</nav> </nav>
<ng-container *ngFor="let c of categories | async"> <ng-container *ngFor="let c of categories | async">
<mat-card class="m-3" (click)="navigate(c.name)"> <mat-card class="m-3">
<div (click)="navigate(c.name)">
<img *ngIf="c.image" mat-card-image [src]="c.image" [alt]="c.name" style="width: 200px; height: 200px;"> <img *ngIf="c.image" mat-card-image [src]="c.image" [alt]="c.name" style="width: 200px; height: 200px;">
<mat-divider *ngIf="c.image" class="custom-line"></mat-divider> <mat-divider *ngIf="c.image" class="custom-line"></mat-divider>
<mat-card-content> <mat-card-content>
<h5>{{c.name}}</h5> <h5>{{c.name}}</h5>
</mat-card-content> </mat-card-content>
</div>
<mat-card-actions *ngIf="app.user">
<button mat-raised-button (click)="create(c)">
<mat-icon>edit</mat-icon>
</button>
<button mat-raised-button class="ml-3">
<mat-icon>delete</mat-icon>
</button>
</mat-card-actions>
</mat-card> </mat-card>
</ng-container> </ng-container>
</div> </div>

View File

@ -32,17 +32,20 @@ export class CategoriesComponent {
this.category = params['category']; this.category = params['category'];
if (!this.category) this.breadcrumb.clear(); if (!this.category) this.breadcrumb.clear();
if (this.category && this.breadcrumb.breadcrumb.length == 0) this.breadcrumb.add(this.category);
this.categories = this.db this.categories = this.db
.collection('categories', ref => ref.orderBy('name')) .collection('categories', ref => ref.orderBy('name'))
.valueChanges() .snapshotChanges()
.pipe( .pipe(
map(rows => map(rows =>
rows rows
.map((row: any) =>
Object.assign({id: row.payload.doc.id, ref: row.payload.doc.ref}, row.payload.doc.data())
)
.filter((row: any) => (!this.category && !row.parent) || (this.category && row.parent == this.category)) .filter((row: any) => (!this.category && !row.parent) || (this.category && row.parent == this.category))
.map((row: any) => { .map((row: any) => {
row.image = this.domSanitizer.bypassSecurityTrustUrl(row.image); row.image = this.domSanitizer.bypassSecurityTrustUrl(row.image);
console.log(row);
return row; return row;
}) })
) )
@ -55,7 +58,7 @@ export class CategoriesComponent {
this.router.navigate(['/store', category]); this.router.navigate(['/store', category]);
} }
create() { create(category) {
this.dialog.open(NewCategoryComponent); this.dialog.open(NewCategoryComponent, {data: {category: category, currentCategory: this.category}});
} }
} }

View File

@ -9,6 +9,7 @@
<mat-form-field class="w-100"> <mat-form-field class="w-100">
<mat-select placeholder="Parent" [(value)]="parent"> <mat-select placeholder="Parent" [(value)]="parent">
<mat-option value="root">root</mat-option> <mat-option value="root">root</mat-option>
<mat-option *ngFor="let c of categories | async" [value]="c.name">{{c.name}}</mat-option>
</mat-select> </mat-select>
</mat-form-field> </mat-form-field>
<input #fileInput type="file" (change)="imageChanged()" hidden> <input #fileInput type="file" (change)="imageChanged()" hidden>

View File

@ -9,6 +9,7 @@ import {MAT_DIALOG_DATA, MatDialogRef} from '../../../../node_modules/@angular/m
export class NewCategoryComponent { export class NewCategoryComponent {
@ViewChild('fileInput') fileInput; @ViewChild('fileInput') fileInput;
categories;
parent: string = 'root'; parent: string = 'root';
name: string; name: string;
image: string; image: string;
@ -17,7 +18,15 @@ export class NewCategoryComponent {
private dialogRef: MatDialogRef<NewCategoryComponent>, private dialogRef: MatDialogRef<NewCategoryComponent>,
private db: AngularFirestore, private db: AngularFirestore,
@Inject(MAT_DIALOG_DATA) public data @Inject(MAT_DIALOG_DATA) public data
) {} ) {
this.categories = this.db.collection('categories').valueChanges();
if (data.currentCategory) this.parent = data.currentCategory;
if (data.category) {
this.name = data.category.name;
this.parent = data.category.parent == null ? 'root' : data.category.parent;
}
}
imageChanged() { imageChanged() {
let reader = new FileReader(); let reader = new FileReader();
@ -26,11 +35,16 @@ export class NewCategoryComponent {
} }
submit() { submit() {
if (!this.data) { let newCategory = {name: this.name, parent: this.parent == 'root' ? null : this.parent};
if (this.image) newCategory['image'] = this.image;
if (!this.data.category) {
this.db this.db
.collection('categories') .collection('categories')
.add({name: this.name, image: this.image, parent: this.parent == 'root' ? null : this.parent}) .add(newCategory)
.then(data => this.dialogRef.close()); .then(data => this.dialogRef.close());
} else {
this.data.category.ref.update(newCategory).then(data => this.dialogRef.close());
} }
} }
} }