Correction sur la mise en cache des

données référentiel et leurs utilisation
sur la liste des sauts.
This commit is contained in:
Sébastien André
2021-05-28 17:19:12 +02:00
parent dba69b938e
commit ba99a0f047
14 changed files with 143 additions and 79 deletions

View File

@@ -1,6 +1,18 @@
<mat-toolbar *ngIf="this.show()">
<button mat-icon-button (click)="snav.toggle()"><mat-icon>menu</mat-icon></button>
<h2>{{ title }}</h2>
<mat-select (selectionChange)="switchLang($event)" [(value)]="selectedLanguageFlag"
style="width: 40px;" >
<mat-select-trigger>
<img src="{{ 'assets/img/' + selectedLanguageFlag + '.svg' }}" style="width: 15px;">
</mat-select-trigger>
<mat-option value="fr" style="width: 50px;">
<img src="assets/img/fr.svg" style="width: 15px;">
</mat-option>
<mat-option value="en" style="width: 50px;">
<img src="assets/img/en.svg" style="width: 15px;">
</mat-option>
</mat-select>
</mat-toolbar>
<mat-sidenav-container>
@@ -44,7 +56,7 @@
</mat-nav-list>
<mat-nav-list *ngIf="currentUser">
<mat-icon aria-hidden="false" aria-label="To logout">logout</mat-icon>
<span (click)="snav.toggle(); logout()" style="cursor: pointer;">sdfsdfsf {{ 'App_Nav_Logout' | translate }}</span>
<span (click)="snav.toggle(); logout()" style="cursor: pointer;">{{ 'App_Nav_Logout' | translate }}</span>
</mat-nav-list>
</mat-sidenav>

View File

@@ -1,6 +1,7 @@
import { Component, OnInit } from "@angular/core";
import { Router } from "@angular/router";
import { TranslateService } from "@ngx-translate/core";
import { forkJoin, Observable } from 'rxjs';
import { User } from "../models/user";
import { CacheApiKey } from "../models/cache-api-key.enum";
@@ -9,6 +10,10 @@ import { AuthenticationService } from "../services/authentication.service";
import { ServiceComm } from "../services/service-comm.service";
import { ConfigurationHelper } from "../services/configuration-helper";
import { ServiceCacheApi } from "../services/service-cache-api.service";
import { AircraftService } from "../services/aircraft.service";
import { GearService } from "../services/gear.service";
import { JumpTypeService } from "../services/jump-type.service";
import { DropzoneService } from "../services/dropzone.service";
@Component({
selector: "app-root",
@@ -19,37 +24,66 @@ export class AppComponent implements OnInit {
public title = "app";
public currentUser: User;
public version: string;
public selectedLanguageFlag: string;
constructor(private router: Router,
private authenticationService: AuthenticationService,
private serviceComm: ServiceComm,
private serviceCacheApi : ServiceCacheApi,
private translateService: TranslateService)
private translateService: TranslateService,
private serviceApiAircraft : AircraftService,
private serviceApiJumpType : JumpTypeService,
private serviceApiDropzone : DropzoneService,
private serviceApiGear : GearService)
{
this.authenticationService.currentUser.subscribe(user => {
this.currentUser = user;
this.translateService.use(user.language);
this.authenticationService.alwaysLogin();
this.authenticationService.currentUser.subscribe(user => {
if (user) {
this.currentUser = user;
this.translateService.addLangs(['en', 'fr']);
this.translateService.use(user.language);
this.selectedLanguageFlag = user.language;
this.putToCacheRefDatas().subscribe(() => { console.log("Push to cache the referentiel datas"); });
}
});
ConfigurationHelper.settings.subscribe(settings =>
{
if (settings != null) {
this.version = settings.version;
}
});
{
if (settings != null) {
this.version = settings.version;
}
});
}
ngOnInit() {
this.serviceComm.componentTitle.subscribe(title => (this.title = title));
}
show() {
public show() {
return this.authenticationService.currentUserValue != undefined;
}
logout() {
public logout() {
this.serviceCacheApi.delete(CacheApiKey.Dropzone);
this.authenticationService.logout();
this.router.navigate(["/login"], { skipLocationChange: true });
}
public switchLang(event: any) {
this.translateService.use(event.value);
this.currentUser.language = event.value;
this.authenticationService.currentUserValue = this.currentUser;
this.selectedLanguageFlag = event.value;
}
private putToCacheRefDatas(): Observable<any[]> {
var aircraftResp = this.serviceApiAircraft.getListOfAircrafts(false);
var jumpTypeResp = this.serviceApiJumpType.getListOfJumpTypes();
var dzResp = this.serviceApiDropzone.getListOfDropZones(false);
var gearResp = this.serviceApiGear.getListOfGears();
return forkJoin([aircraftResp, jumpTypeResp, dzResp, gearResp]);
}
}

View File

@@ -1,6 +1,5 @@
import { Component, OnInit } from "@angular/core";
import { TranslateService } from "@ngx-translate/core";
import { AuthenticationService } from "../../services/authentication.service";
import { ServiceComm } from "../../services/service-comm.service";
@Component({
@@ -10,13 +9,11 @@ import { ServiceComm } from "../../services/service-comm.service";
})
export class DefaultComponent implements OnInit {
constructor(private serviceComm: ServiceComm,
private authenticationService: AuthenticationService,
private translateService: TranslateService) {}
ngOnInit() {
this.translateService.get("Home").subscribe(
data => { this.serviceComm.UpdatedComponentTitle(data); }
)
this.authenticationService.alwaysLogin();
);
}
}
}

View File

@@ -16,7 +16,6 @@ import { JumpInfosComponent } from "../jump-infos/jump-infos.component";
styleUrls: ['./list-of-jumps.component.css']
})
export class ListOfJumpsComponent implements OnInit {
public listOfJumps: Observable<Array<Jump>>;
public displayedColumns: Array<string> = [
'infos',
'id',
@@ -46,8 +45,7 @@ export class ListOfJumpsComponent implements OnInit {
}
getListOfJumps() {
this.listOfJumps = this.serviceApi.GetListOfJumps();
this.listOfJumps.subscribe(data => {
this.serviceApi.GetListOfJumps().subscribe(data => {
setTimeout(() => {
data.sort((a, b) => {
if (a.jumpDate.getTime() === b.jumpDate.getTime()) {

View File

@@ -2,15 +2,10 @@ import { Component, OnInit, ViewChild, AfterViewInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MatInput } from '@angular/material/input';
import { TranslateService } from '@ngx-translate/core';
import { first } from 'rxjs/operators';
import { AuthenticationService } from '../../services/authentication.service';
import { AircraftService } from "../../services/aircraft.service";
import { GearService } from "../../services/gear.service";
import { JumpTypeService } from "../../services/jump-type.service";
import { DropzoneService } from "../../services/dropzone.service";
@Component({
selector: 'app-login-user',
@@ -28,12 +23,7 @@ export class LoginUserComponent implements OnInit, AfterViewInit {
constructor(private formBuilder: FormBuilder,
private route: ActivatedRoute,
private router: Router,
private authenticationService: AuthenticationService,
private serviceApiAircraft : AircraftService,
private serviceApiJumpType : JumpTypeService,
private serviceApiDropzone : DropzoneService,
private serviceApiGear : GearService,
private translateService: TranslateService) {
private authenticationService: AuthenticationService) {
if (this.authenticationService.currentUserValue) {
this.router.navigate(['/']);
}
@@ -64,7 +54,6 @@ export class LoginUserComponent implements OnInit, AfterViewInit {
onLoginSubmit() {
this.submitted = true;
// stop here if form is invalid
if (this.loginForm.invalid) {
return;
}
@@ -74,11 +63,6 @@ export class LoginUserComponent implements OnInit, AfterViewInit {
.pipe(first())
.subscribe(
data => {
this.serviceApiAircraft.getListOfAircrafts(false).subscribe();
this.serviceApiJumpType.getListOfJumpTypes().subscribe();
this.serviceApiDropzone.getListOfDropZones(false).subscribe();
this.serviceApiGear.getListOfGears().subscribe();
this.router.navigate([this.returnUrl]);
},
error => {

View File

@@ -8,7 +8,6 @@ import { TranslateService } from "@ngx-translate/core";
})
export class LoginComponent implements OnInit {
public selectedLanguageFlag: string;
private flag: Map<string, string>;
constructor(private translate: TranslateService) {
translate.addLangs(['en', 'fr']);

View File

@@ -45,10 +45,10 @@ export class SummaryComponent implements OnInit {
this.serviceApi.getStatsOfLastMonth()
.subscribe(data => {
data.byDz.sort((a, b) => b.nb - a.nb );
this.dsJumpForLastMonthByDz = new MatTableDataSource(data.byDz);
this.dsJumpForLastMonthByJumpType = new MatTableDataSource(
data.byJumpType
);
data.byJumpType.sort((a, b) => b.nb - a.nb );
this.dsJumpForLastMonthByJumpType = new MatTableDataSource(data.byJumpType);
});
}
@@ -94,7 +94,7 @@ export class SummaryComponent implements OnInit {
case 4:
this.serviceApi.getStatsByGear()
.subscribe(data => {
data.sort((a, b) => b.nb - a.nb );
data.sort((a, b) => b.label.localeCompare(a.label) );
this.dsNbJumpByGear = new MatTableDataSource(data);
});
break;

View File

@@ -40,8 +40,12 @@ export class AircraftService extends BaseService {
public getById(id: number) : Observable<AircraftResp> {
return this.serviceCacheApi.getByKey<Array<AircraftResp>>(CacheApiKey.Aircraft)
.pipe(map(data => {
return data.find(f => f.id === id);
}));
.pipe(map(data => {
return data.find(f => f.id === id);
}));
}
public getFromCache(): Observable<Array<AircraftResp>> {
return this.serviceCacheApi.getByKey<Array<AircraftResp>>(CacheApiKey.Aircraft);
}
}

View File

@@ -22,10 +22,6 @@ export class AuthenticationService extends BaseService {
this.currentUser = this.currentUserSubject.asObservable();
}
public get currentUserValue(): User {
return this.currentUserSubject.value;
}
public login(username: string, password: string) {
const bodyLogin = {
login: username,
@@ -61,6 +57,24 @@ export class AuthenticationService extends BaseService {
}));
}
public alwaysLogin() {
this.http.get(`${this.apiUrl}/User/AlwaysLogin`,
{ headers: this.headers })
.subscribe();
}
public logout() {
localStorage.removeItem("currentUser");
this.currentUserSubject.next(null);
}
public get currentUserValue(): User {
return this.currentUserSubject.value;
}
public set currentUserValue(value: User) {
this.currentUserSubject.next(value);
}
private pushToken(login: string, password: string, user: User){
if (user && user.token) {
user.authdata = window.btoa(login + ":" + password);
@@ -72,15 +86,4 @@ export class AuthenticationService extends BaseService {
this.currentUserSubject.next(user);
}
}
public alwaysLogin() {
this.http.get(`${this.apiUrl}/User/AlwaysLogin`,
{ headers: this.headers })
.subscribe();
}
public logout() {
localStorage.removeItem("currentUser");
this.currentUserSubject.next(null);
}
}

View File

@@ -80,8 +80,12 @@ export class DropzoneService extends BaseService {
public getById(id: number) : Observable<DropZoneResp> {
return this.serviceCacheApi.getByKey<Array<DropZoneResp>>(CacheApiKey.Dropzone)
.pipe(map(data => {
return data.find(f => f.id === id);
}));
.pipe(map(data => {
return data.find(f => f.id === id);
}));
}
public getFromCache(): Observable<Array<DropZoneResp>> {
return this.serviceCacheApi.getByKey<Array<DropZoneResp>>(CacheApiKey.Dropzone);
}
}

View File

@@ -44,8 +44,12 @@ export class GearService extends BaseService {
public getById(id: number) : Observable<GearResp> {
return this.serviceCacheApi.getByKey<Array<GearResp>>(CacheApiKey.Gear)
.pipe(map(data => {
return data.find(f => f.id === id);
}));
.pipe(map(data => {
return data.find(f => f.id === id);
}));
}
public getFromCache(): Observable<Array<GearResp>> {
return this.serviceCacheApi.getByKey<Array<GearResp>>(CacheApiKey.Gear);
}
}

View File

@@ -32,8 +32,12 @@ export class JumpTypeService extends BaseService {
public getById(id: number) : Observable<JumpTypeResp> {
return this.serviceCacheApi.getByKey<Array<JumpTypeResp>>(CacheApiKey.JumpType)
.pipe(map(data => {
return data.find(f => f.id === id);
}));
.pipe(map(data => {
return data.find(f => f.id === id);
}));
}
public getFromCache(): Observable<Array<JumpTypeResp>> {
return this.serviceCacheApi.getByKey<Array<JumpTypeResp>>(CacheApiKey.JumpType);
}
}

View File

@@ -5,6 +5,10 @@ import { Observable } from "rxjs";
import { map } from "rxjs/operators";
import { JumpResp, JumpReq, Jump } from "../models/jump";
import { GearResp } from "../models/gear";
import { DropZoneResp } from "../models/dropzone";
import { AircraftResp } from "../models/aircraft";
import { JumpTypeResp } from "../models/jumpType";
import { DateService } from "./date.service";
import { BaseService } from "./base.service";
@@ -29,16 +33,7 @@ export class JumpService extends BaseService {
return this.http.get<Array<JumpResp>>(`${this.apiUrl}/Jump`,
{ headers: this.headers })
.pipe(map((response) => {
let details = response.map((data) =>
{
let tmp = new Jump(data);
this.dropzoneService.getById(data.dropZoneId).subscribe((d)=> tmp.dropZone = d );
this.aircraftService.getById(data.aircraftId).subscribe((d)=> tmp.aircraft = d );
this.jumpTypeService.getById(data.jumpTypeId).subscribe((d)=> tmp.jumpType = d );
this.gearService.getById(data.gearId).subscribe((d)=> tmp.gear = d );
return tmp;
});
return details;
return this.MapWithDataInCache(response);
}));
}
@@ -145,4 +140,27 @@ export class JumpService extends BaseService {
.subscribe();
}
}
private MapWithDataInCache(apiResp: Array<JumpResp>) : Array<Jump> {
let allDropzones: Array<DropZoneResp>;
this.dropzoneService.getFromCache().subscribe(data => { allDropzones = data; });
let allAircrafts: Array<AircraftResp>;
this.aircraftService.getFromCache().subscribe(data => { allAircrafts = data; });
let allJumpType: Array<JumpTypeResp>;
this.jumpTypeService.getFromCache().subscribe(data => { allJumpType = data; });
let allGears: Array<GearResp>;
this.gearService.getFromCache().subscribe(data => { allGears = data; });
return apiResp.map((data) =>
{
let tmp = new Jump(data);
tmp.dropZone = allDropzones.find(d => d.id == data.dropZoneId);
tmp.aircraft = allAircrafts.find(d => d.id == data.aircraftId);
tmp.jumpType = allJumpType.find(d => d.id == data.jumpTypeId);
tmp.gear = allGears.find(d => d.id == data.gearId);
return tmp;
});
}
}

View File

@@ -15,6 +15,7 @@ export class ServiceCacheApi {
}
public get<T>(key: CacheApiKey, callToApi: Observable<T>) : Observable<T> {
console.log(`Get cache : ${CacheApiKey[key]}`);
const cached = this.cache.get(key);
if (cached) {
@@ -28,10 +29,12 @@ export class ServiceCacheApi {
}
public delete(key: CacheApiKey) {
console.log(`Delete cache : ${CacheApiKey[key]}`);
this.cache.delete(key);
}
public getByKey<T>(key: CacheApiKey) : Observable<T> {
console.log(`Get cache by key : ${CacheApiKey[key]}`);
return this.cache.get(key);
}
}