Changeset View
Changeset View
Standalone View
Standalone View
src/app/_services/area.service.ts
Show All 11 Lines | export class AreaService { | ||||
public isAreaChecked = false; | public isAreaChecked = false; | ||||
private reputation: { [area: string]: Reputation; } = { }; | private reputation: { [area: string]: Reputation; } = { }; | ||||
public constructor( | public constructor( | ||||
private httpService: HttpService | private httpService: HttpService | ||||
) { } | ) { } | ||||
getAreaRep(area: string): Observable<Reputation> { | getAreaRep(area: string): Observable<Reputation> { | ||||
// Get area rep from api, cache response | |||||
if (this.reputation[area]) { | if (this.reputation[area]) { | ||||
return Observable.of(this.reputation[area]); | return Observable.of(this.reputation[area]); | ||||
} else { | } else { | ||||
return this.httpService.GET('/areas/' + area + '/rep/') | return this.httpService.GET('/areas/' + area + '/rep/') | ||||
.map((response: Response) => { | .map((response: Response) => { | ||||
this.reputation[area] = Reputation.parse(response); | this.reputation[area] = Reputation.parse(response); | ||||
return this.reputation[area]; | return this.reputation[area]; | ||||
}); | }); | ||||
} | } | ||||
} | } | ||||
getAreas(): Observable<Area[]> { | getAreas(): Observable<Area[]> { | ||||
// get areas from api | // Get areas from api, cache response | ||||
if (!this.areas) { | if (!this.areas) { | ||||
return this.httpService.GET('/areas/') | return this.httpService.GET('/areas/') | ||||
.map(response => { | .map(response => { | ||||
const areas: Area[] = []; | const areas: Area[] = []; | ||||
for (let i = 0; i < response.length; i++) { | for (let i = 0; i < response.length; i++) { | ||||
areas.push(Area.parse(response[i])) | areas.push(Area.parse(response[i])); | ||||
} | } | ||||
this.areas = areas; | this.areas = areas; | ||||
this.currentAreaName = this.areas[0].name; | |||||
return areas; | return areas; | ||||
}); | }); | ||||
} else { | } else { | ||||
return Observable.of(this.areas); | return Observable.of(this.areas); | ||||
} | } | ||||
} | } | ||||
getArea(s: string): Observable<Area> { | |||||
if (s === '_') { | |||||
return Observable.of(new Area('_', 'All Posts', 0, 0)); | |||||
} | |||||
if (!this.areas) { | |||||
return this.getAreas() | |||||
.map(areas => { | |||||
if (areas) { | |||||
for (let i = 0; i < areas.length; i++) { | |||||
if (areas[i].name === s) { | |||||
return areas[i]; | |||||
} | |||||
} | |||||
} | |||||
}); | |||||
} else { | |||||
for (let i = 0; i < this.areas.length; i++) { | |||||
if (this.areas[i].name === s) { | |||||
return Observable.of(this.areas[i]); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} | } |