Skip to content

Instantly share code, notes, and snippets.

@adnanmc
Created April 28, 2017 20:23
Show Gist options
  • Save adnanmc/c20a80b7b43318e866b79cd69d3df804 to your computer and use it in GitHub Desktop.
Save adnanmc/c20a80b7b43318e866b79cd69d3df804 to your computer and use it in GitHub Desktop.
import { Component, OnInit } from '@angular/core';
import { FirebaseService } from './services/firebase.service';
import 'rxjs/add/operator/filter';
import { Business } from './Business';
import { Category } from './Category';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [FirebaseService]
})
export class AppComponent implements OnInit {
businesses: Business[];
filteredBusinesses: Business[];
categories: Category[];
appState: string;
activeKey: string;
activeCompany:string;
activeCategory:string;
activeYearsInBusiness:string;
activeDescription:string;
activePhone:string;
activeEmail:string;
activeStreetAddress:string;
activeCity:string;
activeState:string;
activeZipcode:string;
constructor(private _firebaseService: FirebaseService){
}
ngOnInit(){
this._firebaseService.getBusinesses().subscribe(businesses => {
this.businesses = businesses;
});
this._firebaseService.getCategories().subscribe(categories => {
this.categories = categories;
});
}
changeState(state, key){
if(key){
this.activeKey = key;
}
this.appState = state;
console.log(state + " " + key);
}
addBusiness(
company:string,
category:string,
years_in_business:number,
description:string,
phone:string,
email:string,
street_address:string,
city:string,
state:string,
zipcode:string){
var created_at = new Date().toString();
var newBusiness = {
company:company,
category:category,
years_in_business:years_in_business,
description:description,
phone:phone,
email:email,
street_address:street_address,
city:city,
state:state,
zipcode:zipcode,
created_at:created_at
}
// console.log(newBusiness);
this._firebaseService.addBusiness(newBusiness);
this._firebaseService.getBusinesses().subscribe(businesses => {
this.businesses = businesses;
});
// this.changeState('default');
}
showEdit(business){
this.changeState('edit', business.$key);
this.activeCompany = business.company;
this.activeCategory = business.category;
this.activeYearsInBusiness = business.years_in_business;
this.activeDescription = business.description;
this.activePhone = business.phone;
this.activeEmail = business.email;
this.activeStreetAddress = business.street_address;
this.activeCity = business.city;
this.activeState = business.state;
this.activeZipcode = business.zipcode;
}
updateBusiness(){
var updBusiness = {
company : this.activeCompany,
category : this.activeCategory,
years_in_business : this.activeYearsInBusiness,
description : this.activeDescription,
phone : this.activePhone,
email : this.activeEmail,
street_address : this.activeStreetAddress,
city : this.activeCity,
state : this.activeState
}
this._firebaseService.updateBusiness(this.activeKey, updBusiness);
this._firebaseService.getBusinesses().subscribe(businesses => {
this.businesses = businesses;
});
}
deleteBusiness(key){
this._firebaseService.deleteBusiness(key);
}
filterCategory(category){
if (category !== "all") {
this._firebaseService.getBusinesses(category).subscribe(businesses => {
this.businesses = businesses;
});
} else {
this._firebaseService.getBusinesses().subscribe(businesses => {
this.businesses = businesses;
});
}
}
searchBusiness(searchStr:string){
searchStr = searchStr.toLowerCase();
if (searchStr !== "") {
this.businesses = this.businesses.filter(business =>
(business.company.toLowerCase().includes(searchStr)) ||
(business.phone.toLowerCase().includes(searchStr))
);
} else {
this._firebaseService.getBusinesses().subscribe(businesses => {
this.businesses = businesses;
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment