From 6542d34948601431cd94553eaf0465981e7ea7d1 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 4 Oct 2017 14:10:27 +0300 Subject: [PATCH] added possibility for admins to edit product from main page --- .../new-orders/order/order.component.html | 2 +- src/app/guards/admin.guard.ts | 4 +- src/app/homepage/homepage.component.html | 1 + src/app/homepage/homepage.component.scss | 27 ++++++- src/app/homepage/homepage.component.ts | 78 +++++++++++++++---- src/app/services/user.service.ts | 6 +- src/assets/icons/ic_mode_edit_black_24px.svg | 4 + 7 files changed, 98 insertions(+), 24 deletions(-) create mode 100644 src/assets/icons/ic_mode_edit_black_24px.svg diff --git a/src/app/admin-page/new-orders/order/order.component.html b/src/app/admin-page/new-orders/order/order.component.html index ba731b7..32f444a 100644 --- a/src/app/admin-page/new-orders/order/order.component.html +++ b/src/app/admin-page/new-orders/order/order.component.html @@ -21,6 +21,6 @@ - + \ No newline at end of file diff --git a/src/app/guards/admin.guard.ts b/src/app/guards/admin.guard.ts index 86fde28..ba0d250 100644 --- a/src/app/guards/admin.guard.ts +++ b/src/app/guards/admin.guard.ts @@ -11,9 +11,7 @@ import { AngularFireAuth } from 'angularfire2/auth'; @Injectable() export class AdminGuard implements CanActivate { - adminsArr = [ - "PAUi0aWuH5T062teCexxtByBHTB3" - ]; + constructor( private _router: Router, diff --git a/src/app/homepage/homepage.component.html b/src/app/homepage/homepage.component.html index f1bdb64..e2db8b4 100644 --- a/src/app/homepage/homepage.component.html +++ b/src/app/homepage/homepage.component.html @@ -59,6 +59,7 @@

Sorting by category

+
diff --git a/src/app/homepage/homepage.component.scss b/src/app/homepage/homepage.component.scss index 31252fc..112bef9 100644 --- a/src/app/homepage/homepage.component.scss +++ b/src/app/homepage/homepage.component.scss @@ -42,9 +42,9 @@ box-sizing: border-box; border: 1px solid #eee; - > * { + /* > * { width: 100%; - } + } */ ul, li { list-style-type: none; @@ -101,6 +101,24 @@ margin-top: 100px; color: #7b0041; } + +/deep/ .edit { + cursor: pointer; + position: absolute; + right: 10px; + top: 10px; + transition: ease all .2s; + + &:hover{ + svg { + path:first-child { + fill: $darkRead; + } + } + } + + + } } .leftcol { @@ -157,9 +175,9 @@ md-divider { align-content: flex-start; width: 79%; - * { +/* * { width: 100%; - } + } */ .productImg { text-align: center; @@ -223,6 +241,7 @@ md-divider { .oneObjet { + position: relative; display: flex; flex-direction: column; align-items: space-between; diff --git a/src/app/homepage/homepage.component.ts b/src/app/homepage/homepage.component.ts index 81b9f60..9d3d107 100644 --- a/src/app/homepage/homepage.component.ts +++ b/src/app/homepage/homepage.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit, Input, Output, EventEmitter, OnDestroy} from '@angular/core'; +import { Component, OnInit, Input, Output, EventEmitter, OnDestroy } from '@angular/core'; import { ProductsListService } from '../services/products-list.service'; import { PosterComponent } from './poster/poster.component'; import { SmoothScrollToDirective, SmoothScrollDirective } from "ng2-smooth-scroll"; @@ -16,6 +16,11 @@ import { CurrencyPipe } from '@angular/common'; import { Subject } from 'rxjs/Subject'; import { SafeHtml } from '@angular/platform-browser'; import { MdMenuModule } from '@angular/material'; +import { MdIconRegistry } from '@angular/material'; +import { DomSanitizer } from '@angular/platform-browser'; +import { MdDialog } from '@angular/material'; +import { EditProductComponent } from '../admin-page/remove-menu/remove-product/edit-product/edit-product.component'; +import { AdminService } from '../services/admin.service'; @Component({ moduleId: module.id, @@ -31,20 +36,45 @@ export class HomepageComponent implements OnInit { @Input() filtered: Product[]; @Output() click = new EventEmitter(); productsCategory: any[] = PRODUCT_CATEGORY_FILTER; - procuctsType: any[] = PRODUCT_TYPE_FILTER; + procuctsType: any[] = PRODUCT_TYPE_FILTER; public selected: string = ''; categories: FirebaseListObservable; templateTypes: FirebaseListObservable; @Input() product; deleteButton: boolean; startAt = new Subject() - endAt = new Subject() + endAt = new Subject(); + isAdmin: boolean; + subscriptionToUserService: Subscription + subscriptionToAdminService: Subscription - constructor(private productListService: ProductsListService, private db: AngularFireDatabase, private designService: DesignService, private orderService: OrderService, public snackBar: MdSnackBar, private router: Router, private userService: UserService) { + constructor( + private productListService: ProductsListService, + private db: AngularFireDatabase, + private designService: DesignService, + private orderService: OrderService, + public snackBar: MdSnackBar, + private router: Router, + private userService: UserService, + private iconRegistry: MdIconRegistry, + private sanitizer: DomSanitizer, + public dialog: MdDialog, + private adminService: AdminService + ) { + iconRegistry + .addSvgIcon('mode_edit', sanitizer.bypassSecurityTrustResourceUrl('../../assets/icons/ic_mode_edit_black_24px.svg')) }; - ngOnInit():void { - this.designService.getDesignCategory().subscribe(res => {this.categories = res}); + ngOnInit(): void { + let that = this; + this.subscriptionToUserService = this.userService.getUserIdAsync().subscribe(user => { + this.subscriptionToAdminService = this.adminService.getAdmin(user.uid).subscribe(admin => { + if(admin.length > 0){ + this.isAdmin = true; + } + }) + }) + this.designService.getDesignCategory().subscribe(res => { this.categories = res }); this.productListService.getTemplateTypes().subscribe(res => { this.templateTypes = res; }); @@ -53,24 +83,29 @@ export class HomepageComponent implements OnInit { this.arrOfProds = this.prods; }); //this.productListService.getProducts2(this.startAt, this.endAt).subscribe(items => this.prods = items); - }; + }; + + ngOnDestroy(){ + this.subscriptionToUserService.unsubscribe(); + this.subscriptionToAdminService.unsubscribe() + } - sorting(propValue) { - this.productListService.selectProducts(propValue).subscribe(res=> { + sorting(propValue) { + this.productListService.selectProducts(propValue).subscribe(res => { this.prods = res; }); } - navChanged (child: string){ + navChanged(child: string) { this.selected = child - } + } - search(search) { + search(search) { this.prods = this.arrOfProds; this.prods = this.productListService.search(search, this.prods); } - - addToCart(product):void { + + addToCart(product): void { if (!product.size) { let config = new MdSnackBarConfig(); config.extraClasses = ['success-snackbar']; @@ -85,7 +120,20 @@ export class HomepageComponent implements OnInit { } } - setSize(size, product):void { + setSize(size, product): void { product.size = size; } + + onEdit({ $key, name, category, owner, price, type }) { + let dialogRef = this.dialog.open(EditProductComponent, { + data: { + $key: $key, + name: name, + category: category, + owner: owner, + price: price, + type: type + } + }); + } } diff --git a/src/app/services/user.service.ts b/src/app/services/user.service.ts index cc4b354..8ba0c2a 100644 --- a/src/app/services/user.service.ts +++ b/src/app/services/user.service.ts @@ -42,10 +42,14 @@ export class UserService { // return this.db.object('/users/' + userId + '/gallery'); // } - getUserId(){ + getUserId(): string{ return this.afAuth.auth.currentUser.uid; } + getUserIdAsync(){ + return this.afAuth.authState + } + isUserLogIn() { return this.afAuth.auth.currentUser; } diff --git a/src/assets/icons/ic_mode_edit_black_24px.svg b/src/assets/icons/ic_mode_edit_black_24px.svg new file mode 100644 index 0000000..4488460 --- /dev/null +++ b/src/assets/icons/ic_mode_edit_black_24px.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file