Add use of Store for CampaignService (CC-62)
parent
8e46b6c6f1
commit
1be757bbc1
|
@ -2,14 +2,20 @@ import {Injectable} from '@angular/core';
|
|||
import {Campaign} from '../../models/model-interfaces';
|
||||
import {AppConfig} from '../../app.config';
|
||||
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()
|
||||
export class CampaignService {
|
||||
|
||||
campaigns: Campaign[];
|
||||
campaigns$: Observable<Campaign[]>;
|
||||
|
||||
campaignStore = new Store<Campaign>();
|
||||
|
||||
constructor(private http: HttpClient,
|
||||
private config: AppConfig) {
|
||||
this.campaigns$ = this.campaignStore.items$;
|
||||
}
|
||||
|
||||
getAllCampaigns() {
|
||||
|
@ -18,23 +24,48 @@ export class CampaignService {
|
|||
}
|
||||
|
||||
getAllCampaignsWithWars() {
|
||||
return this.http.get(this.config.apiWarPath)
|
||||
.map(res => res.json());
|
||||
this.http.get(this.config.apiWarPath)
|
||||
.map(res => res.json())
|
||||
.do((ranks) => {
|
||||
this.campaignStore.dispatch({type: LOAD, data: ranks});
|
||||
}).subscribe(_ => {
|
||||
});
|
||||
return this.campaigns$;
|
||||
}
|
||||
|
||||
submitCampaign(campaign: Campaign) {
|
||||
let requestUrl: string;
|
||||
let requestMethod: RequestMethod
|
||||
let accessType;
|
||||
|
||||
if (campaign._id) {
|
||||
return this.http.patch(this.config.apiCampaignPath + '/' + campaign._id, campaign)
|
||||
.map(res => res.json());
|
||||
requestUrl = this.config.apiCampaignPath + '/' + campaign._id;
|
||||
requestMethod = RequestMethod.Patch;
|
||||
accessType = EDIT;
|
||||
} else {
|
||||
return this.http.post(this.config.apiCampaignPath, campaign)
|
||||
.map(res => res.json());
|
||||
}
|
||||
requestUrl = this.config.apiCampaignPath;
|
||||
requestMethod = RequestMethod.Post;
|
||||
accessType = ADD;
|
||||
}
|
||||
|
||||
deleteCampaign(id: string) {
|
||||
return this.http.delete(this.config.apiCampaignPath + '/' + id)
|
||||
.map(res => res.json());
|
||||
const options = new RequestOptions({
|
||||
body: campaign,
|
||||
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) {
|
||||
|
|
|
@ -1,14 +1,4 @@
|
|||
import {
|
||||
AfterViewInit,
|
||||
Component,
|
||||
ElementRef,
|
||||
EventEmitter,
|
||||
Input,
|
||||
OnChanges,
|
||||
Output,
|
||||
SimpleChanges,
|
||||
ViewChild
|
||||
} from '@angular/core';
|
||||
import {Component, ElementRef, EventEmitter, Input, OnChanges, Output, SimpleChanges, ViewChild} from '@angular/core';
|
||||
import {Campaign} from '../../../models/model-interfaces';
|
||||
import {LoginService} from '../../../services/app-user-service/login-service';
|
||||
|
||||
|
@ -47,9 +37,11 @@ export class CampaignNavigationComponent implements OnChanges {
|
|||
}
|
||||
|
||||
select(campaign) {
|
||||
if (campaign && campaign._id) {
|
||||
this.selectedCampaignId = campaign._id;
|
||||
this.campaignSelect.emit(campaign);
|
||||
}
|
||||
}
|
||||
|
||||
edit(campaign) {
|
||||
this.campaignEdit.emit(campaign);
|
||||
|
|
|
@ -40,14 +40,9 @@ export class StatisticHighScoreComponent implements OnInit {
|
|||
.map(params => params['id'])
|
||||
.subscribe((id) => {
|
||||
this.id = id;
|
||||
if (this.campaignService.campaigns) {
|
||||
this.initData();
|
||||
} else {
|
||||
this.campaignService.getAllCampaignsWithWars().subscribe(campaigns => {
|
||||
this.campaignService.campaigns = campaigns;
|
||||
this.initData();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const searchTermStream = this.searchTerm.valueChanges.debounceTime(400);
|
||||
|
|
|
@ -46,13 +46,9 @@ export class StatisticOverviewComponent implements OnInit {
|
|||
.map(params => params['id'])
|
||||
.subscribe((id) => {
|
||||
this.id = id;
|
||||
if (this.campaignService.campaigns) {
|
||||
this.initWars(this.campaignService.campaigns);
|
||||
} else {
|
||||
this.campaignService.getAllCampaignsWithWars().subscribe(campaigns => {
|
||||
this.initWars(campaigns);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ import {SettingsService} from '../services/settings.service';
|
|||
})
|
||||
export class StatisticComponent implements OnInit {
|
||||
|
||||
selectedCampaign: Campaign = {};
|
||||
selectedCampaign: Campaign;
|
||||
|
||||
campaigns: Campaign[] = [];
|
||||
|
||||
|
@ -29,14 +29,16 @@ export class StatisticComponent implements OnInit {
|
|||
ngOnInit() {
|
||||
this.campaignService.getAllCampaignsWithWars().subscribe((campaigns) => {
|
||||
this.campaigns = campaigns;
|
||||
// TODO: next line has to die!
|
||||
this.campaignService.campaigns = campaigns;
|
||||
this.selectedCampaign = this.resolveCampaignFromUrl();
|
||||
this.switchCampaign(this.selectedCampaign);
|
||||
});
|
||||
}
|
||||
|
||||
resolveCampaignFromUrl() {
|
||||
if (this.campaigns.length === 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const url = this.router.url;
|
||||
|
||||
const idFetchPattern = /right:.*\/(.*)\)$/;
|
||||
|
@ -80,9 +82,8 @@ export class StatisticComponent implements OnInit {
|
|||
deleteCampaign(campaign) {
|
||||
this.translate.get('stats.campaign.manage.delete.confirm', {title: campaign.title}).subscribe((confirmQuestion: string) => {
|
||||
if (confirm(confirmQuestion)) {
|
||||
this.campaignService.deleteCampaign(campaign._id)
|
||||
this.campaignService.deleteCampaign(campaign)
|
||||
.subscribe((res) => {
|
||||
this.campaigns.splice(this.campaigns.indexOf(campaign), 1);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
id="campaign"
|
||||
[(ngModel)]="war.campaign"
|
||||
required>
|
||||
<option *ngFor="let campaign of campaignService.campaigns" [ngValue]="campaign._id">
|
||||
<option *ngFor="let campaign of campaignService.campaigns$ | async" [ngValue]="campaign._id">
|
||||
{{campaign.title}}
|
||||
</option>
|
||||
</select>
|
||||
|
|
Loading…
Reference in New Issue