Add use of Store for CampaignService (CC-62)

pull/46/head
HardiReady 2018-10-07 23:41:43 +02:00
parent 8e46b6c6f1
commit 1be757bbc1
6 changed files with 55 additions and 40 deletions

View File

@ -2,14 +2,20 @@ import {Injectable} from '@angular/core';
import {Campaign} from '../../models/model-interfaces'; import {Campaign} from '../../models/model-interfaces';
import {AppConfig} from '../../app.config'; import {AppConfig} from '../../app.config';
import {HttpClient} from '../http-client'; import {HttpClient} from '../http-client';
import {ADD, EDIT, LOAD, REMOVE, Store} from '../stores/generic-store';
import {Observable} from 'rxjs';
import {RequestMethod, RequestOptions} from '@angular/http';
@Injectable() @Injectable()
export class CampaignService { export class CampaignService {
campaigns: Campaign[]; campaigns$: Observable<Campaign[]>;
campaignStore = new Store<Campaign>();
constructor(private http: HttpClient, constructor(private http: HttpClient,
private config: AppConfig) { private config: AppConfig) {
this.campaigns$ = this.campaignStore.items$;
} }
getAllCampaigns() { getAllCampaigns() {
@ -18,23 +24,48 @@ export class CampaignService {
} }
getAllCampaignsWithWars() { getAllCampaignsWithWars() {
return this.http.get(this.config.apiWarPath) this.http.get(this.config.apiWarPath)
.map(res => res.json()); .map(res => res.json())
.do((ranks) => {
this.campaignStore.dispatch({type: LOAD, data: ranks});
}).subscribe(_ => {
});
return this.campaigns$;
} }
submitCampaign(campaign: Campaign) { submitCampaign(campaign: Campaign) {
let requestUrl: string;
let requestMethod: RequestMethod
let accessType;
if (campaign._id) { if (campaign._id) {
return this.http.patch(this.config.apiCampaignPath + '/' + campaign._id, campaign) requestUrl = this.config.apiCampaignPath + '/' + campaign._id;
.map(res => res.json()); requestMethod = RequestMethod.Patch;
accessType = EDIT;
} else { } else {
return this.http.post(this.config.apiCampaignPath, campaign) requestUrl = this.config.apiCampaignPath;
.map(res => res.json()); requestMethod = RequestMethod.Post;
} accessType = ADD;
} }
deleteCampaign(id: string) { const options = new RequestOptions({
return this.http.delete(this.config.apiCampaignPath + '/' + id) body: campaign,
.map(res => res.json()); method: requestMethod
});
return this.http.request(requestUrl, options)
.map(res => res.json())
.do(savedCampaign => {
const action = {type: accessType, data: savedCampaign};
this.campaignStore.dispatch(action);
});
}
deleteCampaign(campaign: Campaign) {
return this.http.delete(this.config.apiCampaignPath + '/' + campaign._id)
.do(res => {
this.campaignStore.dispatch({type: REMOVE, data: campaign});
});
} }
getCampaign(id: string) { getCampaign(id: string) {

View File

@ -1,14 +1,4 @@
import { import {Component, ElementRef, EventEmitter, Input, OnChanges, Output, SimpleChanges, ViewChild} from '@angular/core';
AfterViewInit,
Component,
ElementRef,
EventEmitter,
Input,
OnChanges,
Output,
SimpleChanges,
ViewChild
} from '@angular/core';
import {Campaign} from '../../../models/model-interfaces'; import {Campaign} from '../../../models/model-interfaces';
import {LoginService} from '../../../services/app-user-service/login-service'; import {LoginService} from '../../../services/app-user-service/login-service';
@ -47,9 +37,11 @@ export class CampaignNavigationComponent implements OnChanges {
} }
select(campaign) { select(campaign) {
if (campaign && campaign._id) {
this.selectedCampaignId = campaign._id; this.selectedCampaignId = campaign._id;
this.campaignSelect.emit(campaign); this.campaignSelect.emit(campaign);
} }
}
edit(campaign) { edit(campaign) {
this.campaignEdit.emit(campaign); this.campaignEdit.emit(campaign);

View File

@ -40,14 +40,9 @@ export class StatisticHighScoreComponent implements OnInit {
.map(params => params['id']) .map(params => params['id'])
.subscribe((id) => { .subscribe((id) => {
this.id = id; this.id = id;
if (this.campaignService.campaigns) {
this.initData();
} else {
this.campaignService.getAllCampaignsWithWars().subscribe(campaigns => { this.campaignService.getAllCampaignsWithWars().subscribe(campaigns => {
this.campaignService.campaigns = campaigns;
this.initData(); this.initData();
}); });
}
}); });
const searchTermStream = this.searchTerm.valueChanges.debounceTime(400); const searchTermStream = this.searchTerm.valueChanges.debounceTime(400);

View File

@ -46,13 +46,9 @@ export class StatisticOverviewComponent implements OnInit {
.map(params => params['id']) .map(params => params['id'])
.subscribe((id) => { .subscribe((id) => {
this.id = id; this.id = id;
if (this.campaignService.campaigns) {
this.initWars(this.campaignService.campaigns);
} else {
this.campaignService.getAllCampaignsWithWars().subscribe(campaigns => { this.campaignService.getAllCampaignsWithWars().subscribe(campaigns => {
this.initWars(campaigns); this.initWars(campaigns);
}); });
}
}); });
} }

View File

@ -12,7 +12,7 @@ import {SettingsService} from '../services/settings.service';
}) })
export class StatisticComponent implements OnInit { export class StatisticComponent implements OnInit {
selectedCampaign: Campaign = {}; selectedCampaign: Campaign;
campaigns: Campaign[] = []; campaigns: Campaign[] = [];
@ -29,14 +29,16 @@ export class StatisticComponent implements OnInit {
ngOnInit() { ngOnInit() {
this.campaignService.getAllCampaignsWithWars().subscribe((campaigns) => { this.campaignService.getAllCampaignsWithWars().subscribe((campaigns) => {
this.campaigns = campaigns; this.campaigns = campaigns;
// TODO: next line has to die!
this.campaignService.campaigns = campaigns;
this.selectedCampaign = this.resolveCampaignFromUrl(); this.selectedCampaign = this.resolveCampaignFromUrl();
this.switchCampaign(this.selectedCampaign); this.switchCampaign(this.selectedCampaign);
}); });
} }
resolveCampaignFromUrl() { resolveCampaignFromUrl() {
if (this.campaigns.length === 0) {
return {};
}
const url = this.router.url; const url = this.router.url;
const idFetchPattern = /right:.*\/(.*)\)$/; const idFetchPattern = /right:.*\/(.*)\)$/;
@ -80,9 +82,8 @@ export class StatisticComponent implements OnInit {
deleteCampaign(campaign) { deleteCampaign(campaign) {
this.translate.get('stats.campaign.manage.delete.confirm', {title: campaign.title}).subscribe((confirmQuestion: string) => { this.translate.get('stats.campaign.manage.delete.confirm', {title: campaign.title}).subscribe((confirmQuestion: string) => {
if (confirm(confirmQuestion)) { if (confirm(confirmQuestion)) {
this.campaignService.deleteCampaign(campaign._id) this.campaignService.deleteCampaign(campaign)
.subscribe((res) => { .subscribe((res) => {
this.campaigns.splice(this.campaigns.indexOf(campaign), 1);
}); });
} }
}); });

View File

@ -19,7 +19,7 @@
id="campaign" id="campaign"
[(ngModel)]="war.campaign" [(ngModel)]="war.campaign"
required> required>
<option *ngFor="let campaign of campaignService.campaigns" [ngValue]="campaign._id"> <option *ngFor="let campaign of campaignService.campaigns$ | async" [ngValue]="campaign._id">
{{campaign.title}} {{campaign.title}}
</option> </option>
</select> </select>