|
1 | | -import { Component } from '@angular/core'; |
| 1 | +import { Component, OnInit } from '@angular/core'; |
| 2 | +import { Observable } from 'rxjs/Observable'; |
| 3 | +import { |
| 4 | + AngularFirestore, |
| 5 | + AngularFirestoreCollection |
| 6 | +} from 'angularfire2/firestore'; |
| 7 | +import * as firebase from 'firebase'; |
| 8 | +import { MatDialog } from '@angular/material'; |
| 9 | + |
| 10 | +import { CompanyDialogComponent } from '../company-dialog/company-dialog.component'; |
| 11 | +import { CompanyService } from './company.service'; |
2 | 12 |
|
3 | 13 | @Component({ |
4 | 14 | selector: 'app-companies', |
5 | 15 | templateUrl: './companies.component.html', |
6 | 16 | styleUrls: ['./companies.component.scss'] |
7 | 17 | }) |
8 | 18 | export class CompaniesComponent { |
9 | | - constructor() {} |
| 19 | + companies: Observable<any[]>; |
| 20 | + private uploadTask: firebase.storage.UploadTask; |
| 21 | + private companiesCollection: AngularFirestoreCollection<any>; |
| 22 | + constructor( |
| 23 | + db: AngularFirestore, |
| 24 | + private dialog: MatDialog, |
| 25 | + private companyService: CompanyService |
| 26 | + ) { |
| 27 | + this.companiesCollection = db.collection('companies'); |
| 28 | + this.companies = this.companiesCollection.valueChanges(); |
| 29 | + } |
| 30 | + |
| 31 | + uploadSingle(file: any) { |
| 32 | + return this.companyService.pushUpload(file); |
| 33 | + } |
| 34 | + |
| 35 | + addCompany() { |
| 36 | + const dialogRef = this.dialog.open(CompanyDialogComponent, {}); |
| 37 | + dialogRef.afterClosed().subscribe(company => { |
| 38 | + if (company) { |
| 39 | + const file = this.uploadSingle(company.File).then((fileResult: any) => { |
| 40 | + company.File = fileResult.downloadURL; |
| 41 | + this.companiesCollection.add(company).then(result => { |
| 42 | + console.log(result); |
| 43 | + }); |
| 44 | + }); |
| 45 | + } |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + openWindow(url: string) { |
| 50 | + window.open(url, '_blank'); |
| 51 | + } |
10 | 52 | } |
0 commit comments