From f6317d7fbc7fd9fcd83d4fae87335c4149e57c1c Mon Sep 17 00:00:00 2001 From: HardiReady Date: Sun, 14 Oct 2018 15:56:52 +0200 Subject: [PATCH] Migrate main gateway from Http to HttpClient - 1st iteration (CC-63) --- .../users/edit-user/edit-user.component.ts | 4 +- .../users/user-list/user-list.component.ts | 15 ++--- .../app-user-service/app-user.service.ts | 6 +- .../army-management/awarding.service.ts | 6 +- .../army-management/decoration.service.ts | 19 +++--- .../army-management/promotion.service.ts | 23 +++---- .../services/army-management/rank.service.ts | 23 +++---- .../services/army-management/squad.service.ts | 19 +++--- .../services/army-management/user.service.ts | 55 ++++++++-------- static/src/app/services/http-gateway.ts | 62 +++++++++---------- .../src/app/services/logs/campaign.service.ts | 12 ++-- static/src/app/services/logs/logs.service.ts | 40 +++++------- .../src/app/services/logs/player.service.ts | 6 +- static/src/app/services/logs/war.service.ts | 16 ++--- .../src/app/services/stores/generic-store.ts | 3 + .../war/war-header/war-header.component.ts | 3 +- 16 files changed, 138 insertions(+), 174 deletions(-) diff --git a/static/src/app/manage/users/edit-user/edit-user.component.ts b/static/src/app/manage/users/edit-user/edit-user.component.ts index 9705f95..61ec598 100644 --- a/static/src/app/manage/users/edit-user/edit-user.component.ts +++ b/static/src/app/manage/users/edit-user/edit-user.component.ts @@ -95,8 +95,8 @@ export class EditUserComponent implements OnInit { if (this.user._id) { this.userService.updateUser(updateObject) .subscribe(user => { - if (!user.squad) { - user.squad = '0'; + if (!user.squadId) { + user.squadId = '0'; } this.user = user; this.snackBarService.showSuccess('generic.save.success'); diff --git a/static/src/app/manage/users/user-list/user-list.component.ts b/static/src/app/manage/users/user-list/user-list.component.ts index 0ed4ea4..7d5aa35 100644 --- a/static/src/app/manage/users/user-list/user-list.component.ts +++ b/static/src/app/manage/users/user-list/user-list.component.ts @@ -1,11 +1,11 @@ -import {Component, OnInit} from '@angular/core'; +import {Component} from '@angular/core'; import {FormControl} from '@angular/forms'; import {ActivatedRoute, Router} from '@angular/router'; import {Observable} from 'rxjs/Observable'; import {UserService} from '../../../services/army-management/user.service'; import {User} from '../../../models/model-interfaces'; -import {ADD, LOAD} from '../../../services/stores/generic-store'; +import {ADD_ARRAY, LOAD} from '../../../services/stores/generic-store'; import {Fraction} from '../../../utils/fraction.enum'; import {MatButtonToggleGroup} from '@angular/material'; import {UIHelpers} from '../../../utils/global.helpers'; @@ -16,7 +16,7 @@ import {TranslateService} from '@ngx-translate/core'; templateUrl: './user-list.component.html', styleUrls: ['./user-list.component.css', '../../../style/select-list.css'] }) -export class UserListComponent implements OnInit { +export class UserListComponent { selectedUserId: string | number = null; @@ -26,9 +26,9 @@ export class UserListComponent implements OnInit { radioModel = ''; - throttle = 300; + readonly throttle = 300; - scrollDistance = 1; + readonly scrollDistance = 1; offset = 0; @@ -40,9 +40,6 @@ export class UserListComponent implements OnInit { private router: Router, private route: ActivatedRoute, private translate: TranslateService) { - } - - ngOnInit() { this.users$ = this.userService.users$; } @@ -96,7 +93,7 @@ export class UserListComponent implements OnInit { } if (this.limit !== 0) { this.offset += this.limit; - this.filterUsers(ADD); + this.filterUsers(ADD_ARRAY); } } } diff --git a/static/src/app/services/app-user-service/app-user.service.ts b/static/src/app/services/app-user-service/app-user.service.ts index df173e2..c3d7d09 100644 --- a/static/src/app/services/app-user-service/app-user.service.ts +++ b/static/src/app/services/app-user-service/app-user.service.ts @@ -18,8 +18,7 @@ export class AppUserService { } getUsers() { - this.httpGateway.get(this.config.apiAppUserPath) - .map(res => res.json()) + this.httpGateway.get(this.config.apiAppUserPath) .do((users) => { this.appUserStore.dispatch({type: LOAD, data: users}); }).subscribe(_ => { @@ -29,8 +28,7 @@ export class AppUserService { } updateUser(user: AppUser) { - return this.httpGateway.patch(this.config.apiAppUserPath + user._id, user) - .map(res => res.json()) + return this.httpGateway.patch(this.config.apiAppUserPath + user._id, user) .do(savedUser => { const action = {type: EDIT, data: savedUser}; this.appUserStore.dispatch(action); diff --git a/static/src/app/services/army-management/awarding.service.ts b/static/src/app/services/army-management/awarding.service.ts index ece3615..99bfffd 100644 --- a/static/src/app/services/army-management/awarding.service.ts +++ b/static/src/app/services/army-management/awarding.service.ts @@ -18,7 +18,7 @@ export class AwardingService { .concat('&fractFilter=').concat(fraction) .concat('&userId=').concat(userId) .concat('&squadId=').concat(squadId); - return this.httpGateway.get(getUrl).map(res => res.json()); + return this.httpGateway.get(getUrl); } addAwarding(award: Award) { @@ -26,8 +26,7 @@ export class AwardingService { } updateAward(award) { - return this.httpGateway.patch(this.config.apiAwardPath + '/' + award._id, award) - .map(res => res.json()); + return this.httpGateway.patch(this.config.apiAwardPath + '/' + award._id, award); } requestAwarding(award: Award) { @@ -61,4 +60,3 @@ export class AwardingService { return this.getAwardings('', userId); } } - diff --git a/static/src/app/services/army-management/decoration.service.ts b/static/src/app/services/army-management/decoration.service.ts index 39ee0bf..e4f1905 100644 --- a/static/src/app/services/army-management/decoration.service.ts +++ b/static/src/app/services/army-management/decoration.service.ts @@ -1,10 +1,11 @@ import {Injectable} from '@angular/core'; import {Decoration} from '../../models/model-interfaces'; -import {RequestMethod, RequestOptions, URLSearchParams} from '@angular/http'; +import {RequestMethod, RequestOptions} from '@angular/http'; import {Observable} from 'rxjs/Observable'; import {ADD, EDIT, LOAD, REMOVE, Store} from '../stores/generic-store'; import {AppConfig} from '../../app.config'; import {HttpGateway} from '../http-gateway'; +import {HttpParams} from '@angular/common/http'; @Injectable() export class DecorationService { @@ -18,15 +19,13 @@ export class DecorationService { this.decorations$ = this.decorationStore.items$; } - findDecorations(query = '', fractionFilter?) { - const searchParams = new URLSearchParams(); - searchParams.append('q', query); + findDecorations(query = '', fractionFilter?): Observable { + let searchParams = new HttpParams().append('q', query); if (fractionFilter) { - searchParams.append('fractFilter', fractionFilter); + searchParams = searchParams.append('fractFilter', fractionFilter); } - this.httpGateway.get(this.config.apiDecorationPath, searchParams) - .map(res => res.json()) + this.httpGateway.get(this.config.apiDecorationPath, searchParams) .do((squads) => { this.decorationStore.dispatch({type: LOAD, data: squads}); }).subscribe(_ => { @@ -36,8 +35,7 @@ export class DecorationService { } getDecoration(id: number | string): Observable { - return this.httpGateway.get(this.config.apiDecorationPath + id) - .map(res => res.json()); + return this.httpGateway.get(this.config.apiDecorationPath + id); } /** @@ -76,8 +74,7 @@ export class DecorationService { method: requestMethod, }); - return this.httpGateway.request(requestUrl, options) - .map(res => res.json()) + return this.httpGateway.request(requestUrl, options) .do(savedDecoration => { const action = {type: accessType, data: savedDecoration}; this.decorationStore.dispatch(action); diff --git a/static/src/app/services/army-management/promotion.service.ts b/static/src/app/services/army-management/promotion.service.ts index aebbf14..2557237 100644 --- a/static/src/app/services/army-management/promotion.service.ts +++ b/static/src/app/services/army-management/promotion.service.ts @@ -2,6 +2,7 @@ import {Injectable} from '@angular/core'; import {AppConfig} from '../../app.config'; import {Promotion} from '../../models/model-interfaces'; import {HttpGateway} from '../http-gateway'; +import {Observable} from 'rxjs'; @Injectable() export class PromotionService { @@ -12,9 +13,9 @@ export class PromotionService { private config: AppConfig) { } - getUnconfirmedPromotions(fraction?: string) { - return this.httpGateway.get(this.config.apiPromotionPath + '?inProgress=true&fractFilter=' + fraction) - .map(res => res.json()); + getUnconfirmedPromotions(fraction?: string): Observable { + return this.httpGateway.get(this.config.apiPromotionPath + + '?inProgress=true&fractFilter=' + fraction); } checkUnconfirmedPromotions(fraction?: string) { @@ -25,23 +26,19 @@ export class PromotionService { }); } - getSquadPromotions(squadId: string) { - return this.httpGateway.get(this.config.apiPromotionPath + '?squadId=' + squadId) - .map(res => res.json()); + getSquadPromotions(squadId: string): Observable { + return this.httpGateway.get(this.config.apiPromotionPath + '?squadId=' + squadId); } - requestPromotion(promotion: Promotion) { - return this.httpGateway.post(this.config.apiPromotionPath, promotion); + requestPromotion(promotion: Promotion): Observable { + return this.httpGateway.post(this.config.apiPromotionPath, promotion); } - updatePromotion(promotion) { - return this.httpGateway.patch(this.config.apiPromotionPath + '/' + promotion._id, promotion) - .map(res => res.json()); + updatePromotion(promotion): Observable { + return this.httpGateway.patch(this.config.apiPromotionPath + '/' + promotion._id, promotion); } deletePromotion(promotionId) { return this.httpGateway.delete(this.config.apiPromotionPath + promotionId); } - } - diff --git a/static/src/app/services/army-management/rank.service.ts b/static/src/app/services/army-management/rank.service.ts index 8ff6b7d..d6d2cf7 100644 --- a/static/src/app/services/army-management/rank.service.ts +++ b/static/src/app/services/army-management/rank.service.ts @@ -1,10 +1,11 @@ import {Injectable} from '@angular/core'; import {Rank} from '../../models/model-interfaces'; -import {RequestMethod, RequestOptions, URLSearchParams} from '@angular/http'; +import {RequestMethod, RequestOptions} from '@angular/http'; import {Observable} from 'rxjs/Observable'; import {ADD, EDIT, LOAD, REMOVE, Store} from '../stores/generic-store'; import {AppConfig} from '../../app.config'; import {HttpGateway} from '../http-gateway'; +import {HttpParams} from '@angular/common/http'; @Injectable() export class RankService { @@ -18,15 +19,13 @@ export class RankService { this.ranks$ = this.rankStore.items$; } - findRanks(query = '', fractionFilter?) { - const searchParams = new URLSearchParams(); - searchParams.append('q', query); + findRanks(query = '', fractionFilter?): Observable { + let searchParams = new HttpParams().append('q', query); if (fractionFilter) { - searchParams.append('fractFilter', fractionFilter); + searchParams = searchParams.append('fractFilter', fractionFilter); } - this.httpGateway.get(this.config.apiRankPath, searchParams) - .map(res => res.json()) + this.httpGateway.get(this.config.apiRankPath, searchParams) .do((ranks) => { this.rankStore.dispatch({type: LOAD, data: ranks}); }).subscribe(_ => { @@ -36,15 +35,14 @@ export class RankService { } getRank(id: number | string): Observable { - return this.httpGateway.get(this.config.apiRankPath + id) - .map(res => res.json()); + return this.httpGateway.get(this.config.apiRankPath + id); } /** * For creating new data with POST or * update existing with patch PATCH */ - submitRank(rank: Rank, imageFile?) { + submitRank(rank: Rank, imageFile?): Observable { let requestUrl = this.config.apiRankPath; let requestMethod: RequestMethod; let accessType; @@ -77,11 +75,10 @@ export class RankService { method: requestMethod }); - return this.httpGateway.request(requestUrl, options) - .map(res => res.json()) + return this.httpGateway.request(requestUrl, options) .do(savedRank => { const action = {type: accessType, data: savedRank}; - // leave some time to save image file before accessing it through listview + // leave some time to save image file before accessing it through list view setTimeout(() => { this.rankStore.dispatch(action); }, 300); diff --git a/static/src/app/services/army-management/squad.service.ts b/static/src/app/services/army-management/squad.service.ts index b801295..18c7d7a 100644 --- a/static/src/app/services/army-management/squad.service.ts +++ b/static/src/app/services/army-management/squad.service.ts @@ -1,10 +1,11 @@ import {Injectable} from '@angular/core'; import {Squad} from '../../models/model-interfaces'; -import {RequestMethod, RequestOptions, URLSearchParams} from '@angular/http'; +import {RequestMethod, RequestOptions} from '@angular/http'; import {Observable} from 'rxjs/Observable'; import {ADD, EDIT, LOAD, REMOVE, Store} from '../stores/generic-store'; import {AppConfig} from '../../app.config'; import {HttpGateway} from '../http-gateway'; +import {HttpParams} from '@angular/common/http'; @Injectable() export class SquadService { @@ -18,13 +19,11 @@ export class SquadService { this.squads$ = this.squadStore.items$; } - findSquads(query = '', fractionFilter = '') { - const searchParams = new URLSearchParams(); - searchParams.append('q', query); - searchParams.append('fractFilter', fractionFilter); + findSquads(query = '', fractionFilter = ''): Observable { + const searchParams = new HttpParams().append('q', query) + .append('fractFilter', fractionFilter); this.httpGateway.get(this.config.apiSquadPath, searchParams) - .map(res => res.json()) .do((squads) => { this.squadStore.dispatch({type: LOAD, data: squads}); }).subscribe(_ => { @@ -34,15 +33,14 @@ export class SquadService { } getSquad(id: number | string): Observable { - return this.httpGateway.get(this.config.apiSquadPath + id) - .map(res => res.json()); + return this.httpGateway.get(this.config.apiSquadPath + id); } /** * For creating new data with POST or * update existing with patch PATCH */ - submitSquad(squad: Squad, imageFile?) { + submitSquad(squad: Squad, imageFile?): Observable { let requestUrl = this.config.apiSquadPath; let requestMethod: RequestMethod; let accessType; @@ -74,8 +72,7 @@ export class SquadService { method: requestMethod }); - return this.httpGateway.request(requestUrl, options) - .map(res => res.json()) + return this.httpGateway.request(requestUrl, options) .do(savedSquad => { const action = {type: accessType, data: savedSquad}; this.squadStore.dispatch(action); diff --git a/static/src/app/services/army-management/user.service.ts b/static/src/app/services/army-management/user.service.ts index 34e88fb..71f5c53 100644 --- a/static/src/app/services/army-management/user.service.ts +++ b/static/src/app/services/army-management/user.service.ts @@ -1,10 +1,10 @@ import {Injectable} from '@angular/core'; import {User} from '../../models/model-interfaces'; -import {URLSearchParams} from '@angular/http'; import {Observable} from 'rxjs/Observable'; import {ADD, EDIT, LOAD, REMOVE, Store} from '../stores/generic-store'; import {AppConfig} from '../../app.config'; import {HttpGateway} from '../http-gateway'; +import {HttpParams} from '@angular/common/http'; @Injectable() export class UserService { @@ -20,56 +20,55 @@ export class UserService { this.users$ = this.userStore.items$; } - findUsers(filter, limit?, offset?, action = LOAD) { - const searchParams = new URLSearchParams(); + findUsers(filter, limit?, offset?, action = LOAD): Observable { + let searchParams = new HttpParams().append('q', (filter && filter.query) ? filter.query : '') + .append('limit', limit) + .append('offset', offset); - searchParams.append('q', (filter && filter.query) ? filter.query : ''); - - if (filter && filter.fraction) { - searchParams.append('fractFilter', filter.fraction); + if (filter) { + if (filter.fraction) { + searchParams = searchParams.append('fractFilter', filter.fraction); + } + if (filter.squadId) { + searchParams = searchParams.append('squadId', filter.squadId); + } + if (filter.decorationId) { + searchParams = searchParams.append('decorationId', filter.decorationId); + } } - if (filter && filter.squadId) { - searchParams.append('squadId', filter.squadId); - } - if (filter && filter.decorationId) { - searchParams.append('decorationId', filter.decorationId); - } - - searchParams.append('limit', limit); - searchParams.append('offset', offset); - this.httpGateway.get(this.config.apiUserPath, searchParams) + this.httpGateway.get(this.config.apiUserPath, searchParams, true) .do((res) => { const headerCount = parseInt(res.headers.get('x-total-count'), 10); if (headerCount) { this.totalCount = headerCount; } - }).map(res => res.json()).do((users) => { - this.userStore.dispatch({type: action, data: users}); - }).subscribe(_ => { + }) + .map(res => res.body) + .do((users) => { + console.log(users) + this.userStore.dispatch({type: action, data: users}); + }).subscribe(_ => { }); return this.users$; } getUser(_id: number | string): Observable { - return this.httpGateway.get(this.config.apiUserPath + _id) - .map(res => res.json()); + return this.httpGateway.get(this.config.apiUserPath + _id); } - submitUser(user) { - return this.httpGateway.post(this.config.apiUserPath, user) - .map(res => res.json()) + submitUser(user): Observable { + return this.httpGateway.post(this.config.apiUserPath, user) .do(savedUser => { const action = {type: ADD, data: savedUser}; this.userStore.dispatch(action); }); } - updateUser(user) { - return this.httpGateway.put(this.config.apiUserPath + user._id, user) - .map(res => res.json()) + updateUser(user): Observable { + return this.httpGateway.put(this.config.apiUserPath + user._id, user) .do(savedUser => { const action = {type: EDIT, data: savedUser}; this.userStore.dispatch(action); diff --git a/static/src/app/services/http-gateway.ts b/static/src/app/services/http-gateway.ts index 7aee67a..ee3779e 100644 --- a/static/src/app/services/http-gateway.ts +++ b/static/src/app/services/http-gateway.ts @@ -1,13 +1,15 @@ import {Injectable} from '@angular/core'; -import {Headers, Http, RequestMethod} from '@angular/http'; +import {RequestMethod} from '@angular/http'; import {Router} from '@angular/router'; import {CookieService} from 'ngx-cookie-service'; +import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http'; +import {Observable} from 'rxjs'; @Injectable() export class HttpGateway { constructor(private router: Router, - private http: Http, + private http: HttpClient, private cookieService: CookieService) { } @@ -16,60 +18,56 @@ export class HttpGateway { if (cookieField) { const currentUser = JSON.parse(cookieField); if (new Date().getTime() <= Date.parse(currentUser.tokenExpireDate)) { - const headers = new Headers(); - headers.append('x-access-token', currentUser.token); - return headers; + return { + headers: new HttpHeaders({ + 'x-access-token': currentUser.token + }) + }; } else { // logout localStorage.removeItem('currentUser'); this.router.navigate(['/login']); } + } else { + return {}; } } - get(url, searchParams?) { - const headers = this.createAuthorizationHeader(); - const options: any = {headers: headers}; + get(url: string, searchParams?: HttpParams, getFullResponse = false): Observable { + const options = this.createAuthorizationHeader(); + if (searchParams) { - options.search = searchParams; + options['params'] = searchParams; } - return this.http.get(url, options); + if (getFullResponse) { + options['observe'] = 'response'; + } + + return this.http.get(url, options); } - post(url, data) { - const headers = this.createAuthorizationHeader(); - return this.http.post(url, data, { - headers: headers - }); + post(url, data): Observable { + return this.http.post(url, data, this.createAuthorizationHeader()); } - put(url, data) { - const headers = this.createAuthorizationHeader(); - return this.http.put(url, data, { - headers: headers - }); + put(url, data): Observable { + return this.http.put(url, data, this.createAuthorizationHeader()); } - patch(url, data) { - const headers = this.createAuthorizationHeader(); - return this.http.patch(url, data, { - headers: headers - }); + patch(url, data): Observable { + return this.http.patch(url, data, this.createAuthorizationHeader()); } delete(url) { - const headers = this.createAuthorizationHeader(); - return this.http.delete(url, { - headers: headers - }); + return this.http.delete(url, this.createAuthorizationHeader()); } - request(requestUrl, options) { + request(requestUrl, options): Observable { if (options.method === RequestMethod.Post) { - return this.post(requestUrl, options.body); + return this.post(requestUrl, options.body); } if (options.method === RequestMethod.Patch) { - return this.patch(requestUrl, options.body); + return this.patch(requestUrl, options.body); } } } diff --git a/static/src/app/services/logs/campaign.service.ts b/static/src/app/services/logs/campaign.service.ts index b7c1ea2..46c10a4 100644 --- a/static/src/app/services/logs/campaign.service.ts +++ b/static/src/app/services/logs/campaign.service.ts @@ -19,19 +19,16 @@ export class CampaignService { } getAllCampaigns() { - return this.httpGateway.get(this.config.apiCampaignPath) - .map(res => res.json()) + return this.httpGateway.get(this.config.apiCampaignPath) .do((ranks) => this.campaignStore.dispatch({type: LOAD, data: ranks})); } getCampaign(id: string) { - return this.httpGateway.get(`${this.config.apiCampaignPath}/${id}`) - .map(res => res.json()); + return this.httpGateway.get(`${this.config.apiCampaignPath}/${id}`); } getCampaignByWarId(warId) { - return this.httpGateway.get(`${this.config.apiCampaignPath}/with/war/${warId}`) - .map((res) => res.json()); + return this.httpGateway.get(`${this.config.apiCampaignPath}/with/war/${warId}`); } submitCampaign(campaign: Campaign) { @@ -54,8 +51,7 @@ export class CampaignService { method: requestMethod }); - return this.httpGateway.request(requestUrl, options) - .map(res => res.json()) + return this.httpGateway.request(requestUrl, options) .do(savedCampaign => { const action = {type: accessType, data: savedCampaign}; this.campaignStore.dispatch(action); diff --git a/static/src/app/services/logs/logs.service.ts b/static/src/app/services/logs/logs.service.ts index de2e356..317779b 100644 --- a/static/src/app/services/logs/logs.service.ts +++ b/static/src/app/services/logs/logs.service.ts @@ -1,7 +1,7 @@ import {Injectable} from '@angular/core'; import {AppConfig} from '../../app.config'; -import {URLSearchParams} from '@angular/http'; import {HttpGateway} from '../http-gateway'; +import {HttpParams} from '@angular/common/http'; @Injectable() export class LogsService { @@ -11,69 +11,61 @@ export class LogsService { } getFullLog(warId: string) { - return this.httpGateway.get(this.config.apiLogsPath + '/' + warId) - .map(res => res.json()); + return this.httpGateway.get(this.config.apiLogsPath + '/' + warId); } getBudgetLogs(warId: string, fraction = '') { - const params = new URLSearchParams(); + const params = new HttpParams(); params.append('fraction', fraction); - return this.httpGateway.get(this.config.apiLogsPath + '/' + warId + '/budget', params) - .map(res => res.json()); + return this.httpGateway.get(this.config.apiLogsPath + '/' + warId + '/budget', params); } getRespawnLogs(warId: string, playerName = '') { - const params = new URLSearchParams(); + const params = new HttpParams(); params.append('player', playerName); - return this.httpGateway.get(this.config.apiLogsPath + '/' + warId + '/respawn', params) - .map(res => res.json()); + return this.httpGateway.get(this.config.apiLogsPath + '/' + warId + '/respawn', params); } getPointsLogs(warId: string, fraction = '') { - const params = new URLSearchParams(); + const params = new HttpParams(); params.append('fraction', fraction); - return this.httpGateway.get(this.config.apiLogsPath + '/' + warId + '/points', params) - .map(res => res.json()); + return this.httpGateway.get(this.config.apiLogsPath + '/' + warId + '/points', params); } getReviveLogs(warId: string, medicName = '', patientName = '', fraction = '', stabilizedOnly = false, reviveOnly = false) { - const params = new URLSearchParams(); + const params = new HttpParams(); params.append('medic', medicName); params.append('patient', patientName); params.append('fraction', fraction); params.append('stabilized', stabilizedOnly ? 'true' : ''); params.append('revive', reviveOnly ? 'true' : ''); - return this.httpGateway.get(this.config.apiLogsPath + '/' + warId + '/revive', params) - .map(res => res.json()); + return this.httpGateway.get(this.config .apiLogsPath + '/' + warId + '/revive', params); } getKillLogs(warId: string, shooterName = '', targetName = '', fraction = '', friendlyFireOnly = false, notFriendlyFireOnly = false) { - const params = new URLSearchParams(); + const params = new HttpParams(); params.append('shooter', shooterName); params.append('target', targetName); params.append('fraction', fraction); params.append('friendlyFire', friendlyFireOnly ? 'true' : ''); params.append('noFriendlyFire', notFriendlyFireOnly ? 'true' : ''); - return this.httpGateway.get(this.config.apiLogsPath + '/' + warId + '/kills', params) - .map(res => res.json()); + return this.httpGateway.get(this.config.apiLogsPath + '/' + warId + '/kills', params); } getTransportLogs(warId: string, driverName = '', passengerName = '', fraction = '') { - const params = new URLSearchParams(); + const params = new HttpParams(); params.append('driver', driverName); params.append('passenger', passengerName); params.append('fraction', fraction); - return this.httpGateway.get(this.config.apiLogsPath + '/' + warId + '/transport', params) - .map(res => res.json()); + return this.httpGateway.get(this.config.apiLogsPath + '/' + warId + '/transport', params); } getFlagLogs(warId: string, playerName = '', fraction = '', captureOnly = false, defendOnly = false) { - const params = new URLSearchParams(); + const params = new HttpParams(); params.append('player', playerName); params.append('fraction', fraction); params.append('capture', captureOnly ? 'true' : ''); params.append('defend', defendOnly ? 'true' : ''); - return this.httpGateway.get(this.config.apiLogsPath + '/' + warId + '/flag', params) - .map(res => res.json()); + return this.httpGateway.get(this.config.apiLogsPath + '/' + warId + '/flag', params); } } diff --git a/static/src/app/services/logs/player.service.ts b/static/src/app/services/logs/player.service.ts index 8a232fc..e9099ae 100644 --- a/static/src/app/services/logs/player.service.ts +++ b/static/src/app/services/logs/player.service.ts @@ -10,12 +10,10 @@ export class PlayerService { } getCampaignPlayer(campaignId: string, playerName: string) { - return this.httpGateway.get(this.config.apiPlayersPath + '/single/' + campaignId + '/' + playerName) - .map(res => res.json()); + return this.httpGateway.get(this.config.apiPlayersPath + '/single/' + campaignId + '/' + playerName); } getCampaignHighscore(campaignId: string) { - return this.httpGateway.get(this.config.apiPlayersPath + '/ranking/' + campaignId) - .map(res => res.json()); + return this.httpGateway.get(this.config.apiPlayersPath + '/ranking/' + campaignId); } } diff --git a/static/src/app/services/logs/war.service.ts b/static/src/app/services/logs/war.service.ts index 8fc307c..a34c087 100644 --- a/static/src/app/services/logs/war.service.ts +++ b/static/src/app/services/logs/war.service.ts @@ -17,19 +17,17 @@ export class WarService { this.wars$ = this.warStore.items$; } - getAllWars(campaignId?: string) { + getAllWars(campaignId?: string): Observable { let targetUrl = this.config.apiWarPath; if (campaignId) { targetUrl += `?campaignId=${campaignId}` } - return this.httpGateway.get(targetUrl) - .map(res => res.json()) + return this.httpGateway.get(targetUrl) .do((wars) => this.warStore.dispatch({type: LOAD, data: wars})); } - getWar(warId: string) { - return this.httpGateway.get(this.config.apiWarPath + '/' + warId) - .map(res => res.json()); + getWar(warId: string): Observable { + return this.httpGateway.get(this.config.apiWarPath + '/' + warId); } submitWar(war: War, logFile?) { @@ -45,14 +43,12 @@ export class WarService { body.append('log', logFile, logFile.name); } - return this.httpGateway.post(this.config.apiWarPath, body) - .map(res => res.json()) + return this.httpGateway.post(this.config.apiWarPath, body) .do((newWar) => this.warStore.dispatch({type: ADD, data: newWar})); } updateWar(war: War) { - return this.httpGateway.patch(this.config.apiWarPath + '/' + war._id, war) - .map(res => res.json()) + return this.httpGateway.patch(this.config.apiWarPath + '/' + war._id, war) .do((updatedWar) => this.warStore.dispatch({type: EDIT, data: updatedWar})); } diff --git a/static/src/app/services/stores/generic-store.ts b/static/src/app/services/stores/generic-store.ts index 80baa40..cc18838 100644 --- a/static/src/app/services/stores/generic-store.ts +++ b/static/src/app/services/stores/generic-store.ts @@ -2,6 +2,7 @@ import {BehaviorSubject} from 'rxjs/BehaviorSubject'; export const LOAD = 'LOAD'; export const ADD = 'ADD'; +export const ADD_ARRAY = 'ADD_ARRAY'; export const EDIT = 'EDIT'; export const REMOVE = 'REMOVE'; @@ -27,6 +28,8 @@ export class Store { return [...action.data]; case ADD: return [...items, action.data]; + case ADD_ARRAY: + return [...items].concat(action.data); case EDIT: return items.map(item => { const editedItem = action.data; diff --git a/static/src/app/statistic/war/war-header/war-header.component.ts b/static/src/app/statistic/war/war-header/war-header.component.ts index d4cd8db..63c01c5 100644 --- a/static/src/app/statistic/war/war-header/war-header.component.ts +++ b/static/src/app/statistic/war/war-header/war-header.component.ts @@ -56,7 +56,8 @@ export class WarHeaderComponent implements OnInit { this.fractionStatsInitialized = false; this.fractionFilterSelected = undefined; - this.playerChart = ChartUtils.getSingleDataArray(Fraction.OPFOR, war.playersOpfor, Fraction.BLUFOR, war.playersBlufor); + this.playerChart = + ChartUtils.getSingleDataArray(Fraction.OPFOR, war.playersOpfor, Fraction.BLUFOR, war.playersBlufor); Object.assign(this, [this.playerChart]); }); }