diff --git a/src/app/house.component.html b/src/app/house.component.html index 46d5a7f..b88b67f 100644 --- a/src/app/house.component.html +++ b/src/app/house.component.html @@ -16,7 +16,7 @@ - + {{house.order}} @@ -36,8 +36,7 @@ - @@ -54,8 +53,30 @@ + + + + + + + + + + + + + + + + - 집을 추가해 주세요. + 집을 추가해 주세요. diff --git a/src/app/house.component.ts b/src/app/house.component.ts index 6389793..61340cf 100644 --- a/src/app/house.component.ts +++ b/src/app/house.component.ts @@ -2,18 +2,23 @@ import { Component, OnInit } from '@angular/core'; import { TerritoryService } from 'app/services/territory.service'; import { House } from 'app/classes/house'; import { ActivatedRoute, ParamMap } from '@angular/router'; - +import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/switchMap'; + @Component({ templateUrl: './house.component.html' }) export class HouseComponent implements OnInit { - houses: House[] = []; + houses: Observable; territoryKey: string; selectedHouse: House; editingHouse: House; + newHouse: House = { + order: 1, + name: '' + }; constructor( private territoryService: TerritoryService, @@ -21,19 +26,15 @@ export class HouseComponent implements OnInit { ) {} ngOnInit() { - this.route.paramMap.switchMap((params: ParamMap) => { + this.houses = this.route.paramMap.switchMap((params: ParamMap) => { this.territoryKey = params.get('key'); return this.territoryService.getHouses(this.territoryKey); - }).subscribe(houses => this.houses = houses); + }); } addRow() { - this.houses.push({ - name: '', - order: 1 - }); - this.selectedHouse = this.houses[this.houses.length - 1]; - this.editingHouse = Object.assign({}, this.houses[this.houses.length - 1]); + this.selectedHouse = this.newHouse; + this.editingHouse = Object.assign({}, this.newHouse); } save() { @@ -45,7 +46,6 @@ export class HouseComponent implements OnInit { } else { this.territoryService.createHouse(this.territoryKey, this.editingHouse).then(() => { this.selectedHouse = null; - this.houses = this.houses.filter(house => house.$key); }); } } @@ -57,11 +57,10 @@ export class HouseComponent implements OnInit { cancel() { this.selectedHouse = null; - this.houses = this.houses.filter(house => house.$key); } delete(house: House) { - this.territoryService.deleteHouse(this.territoryKey, house); + this.territoryService.deleteHouse(this.territoryKey, house.$key); } } diff --git a/src/app/services/territory.service.ts b/src/app/services/territory.service.ts index a99b462..aca0586 100644 --- a/src/app/services/territory.service.ts +++ b/src/app/services/territory.service.ts @@ -128,9 +128,8 @@ export class TerritoryService { * @returns * @memberof TerritoryService */ - deleteHouse(territoryKey: string, house: House) { + deleteHouse(territoryKey: string, houseKey: string) { const congregation = this.authService.getCongregation(); - const houseKey = house.$key; return this.db.object(`${congregation}/houses/${territoryKey}/${houseKey}`).remove(); } @@ -143,7 +142,9 @@ export class TerritoryService { */ getHouses(territoryKey: string) { const congregation = this.authService.getCongregation(); - return this.db.list(`${congregation}/houses/${territoryKey}`); + return this.db.list(`${congregation}/houses/${territoryKey}`).map(houses => { + return houses.sort((a, b) => +a.order < +b.order ? -1 : 1); + }); } /**