Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: avoid localstorage for country info AB#6424
Browse files Browse the repository at this point in the history
gulfaraz committed Mar 2, 2021
1 parent a22896b commit 60f998b
Showing 17 changed files with 428 additions and 275 deletions.
29 changes: 13 additions & 16 deletions interfaces/IBF-dashboard/src/app/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { BehaviorSubject } from 'rxjs';
import { BehaviorSubject, Observable } from 'rxjs';
import { User } from 'src/app/models/user/user.model';
import { ApiService } from 'src/app/services/api.service';
import { CountryService } from 'src/app/services/country.service';
import { JwtService } from 'src/app/services/jwt.service';
import { UserRole } from '../models/user/user-role.enum';

@@ -12,26 +11,26 @@ import { UserRole } from '../models/user/user-role.enum';
})
export class AuthService {
private loggedIn = false;
private userRole: UserRole | string;

redirectUrl: string;

private authenticationState = new BehaviorSubject<User | null>(null);
public authenticationState$ = this.authenticationState.asObservable();
private userRole: UserRole;
public redirectUrl: string;
private authSubject = new BehaviorSubject<User>(null);

constructor(
private apiService: ApiService,
private jwtService: JwtService,
private countryService: CountryService,
private router: Router,
) {
this.checkLoggedInState();
}

getAuthSubscription = (): Observable<User> => {
return this.authSubject.asObservable();
};

checkLoggedInState() {
const user = this.getUserFromToken();

this.authenticationState.next(user);
this.authSubject.next(user);
}

public isLoggedIn(): boolean {
@@ -40,11 +39,11 @@ export class AuthService {
return this.loggedIn;
}

public getUserRole(): UserRole | string {
public getUserRole(): UserRole {
if (!this.userRole) {
const user = this.getUserFromToken();

this.userRole = user ? user.userRole : '';
this.userRole = user ? user.userRole : null;
}

return this.userRole;
@@ -86,13 +85,11 @@ export class AuthService {

const user = this.getUserFromToken();

this.authenticationState.next(user);
this.authSubject.next(user);

this.loggedIn = true;
this.userRole = user.userRole;

this.countryService.getCountriesByUser(user);

if (this.redirectUrl) {
this.router.navigate([this.redirectUrl]);
this.redirectUrl = null;
@@ -110,7 +107,7 @@ export class AuthService {
public logout() {
this.jwtService.destroyToken();
this.loggedIn = false;
this.authenticationState.next(null);
this.authSubject.next(null);
this.router.navigate(['/login']);
}
}
Original file line number Diff line number Diff line change
@@ -21,7 +21,12 @@ import { IbfLayerGroup, IbfLayerName } from 'src/app/types/ibf-layer';
export class AdminLevelComponent {
private countrySubscription: Subscription;
public adminLevel = AdminLevel;
public adminLevelLabel: AdminLevelLabel;
public adminLevelLabel: AdminLevelLabel = {
adm1: 'Admin Level 1',
adm2: 'Admin Level 2',
adm3: 'Admin Level 3',
adm4: 'Admin Level 4',
};
private adminLevelNumber: number;

constructor(
@@ -49,13 +54,18 @@ export class AdminLevelComponent {
}

loadAdminLevelLabels() {
const activeCountry = this.countryService.getActiveCountry();
this.adminLevelLabel = {
adm1: activeCountry.adminRegionLabels[0],
adm2: activeCountry.adminRegionLabels[1],
adm3: activeCountry.adminRegionLabels[2],
adm4: activeCountry.adminRegionLabels[3],
};
this.countryService
.getCountrySubscription()
.subscribe((country: Country) => {
if (country) {
this.adminLevelLabel = {
adm1: country.adminRegionLabels[0],
adm2: country.adminRegionLabels[1],
adm3: country.adminRegionLabels[2],
adm4: country.adminRegionLabels[3],
};
}
});
}

setAdminLevelClick(adminLevel: number, state: boolean): void {
Original file line number Diff line number Diff line change
@@ -67,12 +67,7 @@ export class AggregatesComponent implements OnInit, OnDestroy {
}

ngOnInit() {
if (
this.countryService.activeCountry &&
this.timelineService.activeLeadTime
) {
this.aggregatesService.loadMetadataAndAggregates();
}
this.aggregatesService.loadMetadataAndAggregates();

this.countrySubscription = this.countryService
.getCountrySubscription()
@@ -159,25 +154,30 @@ export class AggregatesComponent implements OnInit, OnDestroy {

public getHeaderLabel() {
let headerLabel = this.defaultHeaderLabel;
const country = this.countryService.getActiveCountry();
const adminAreaLabel =
country.adminRegionLabels[this.adminLevelService.adminLevel - 1];

if (this.placeCode) {
headerLabel = this.placeCode.placeCodeName;
} else {
if (this.eventService.state.activeTrigger) {
this.eapActionsService
.getTriggeredAreas()
.subscribe((triggeredAreas) => {
headerLabel = `${triggeredAreas.length} ${this.exposedPrefix} ${adminAreaLabel}`;
});
} else {
if (country) {
headerLabel = `${this.allPrefix} ${country.countryName}`;

this.countryService
.getCountrySubscription()
.subscribe((country: Country) => {
if (this.placeCode) {
headerLabel = this.placeCode.placeCodeName;
} else {
if (country) {
if (this.eventService.state.activeTrigger) {
const adminAreaLabel =
country.adminRegionLabels[
this.adminLevelService.adminLevel - 1
];
this.eapActionsService
.getTriggeredAreas()
.subscribe((triggeredAreas) => {
headerLabel = `${triggeredAreas.length} ${this.exposedPrefix} ${adminAreaLabel}`;
});
} else {
headerLabel = `${this.allPrefix} ${country.countryName}`;
}
}
}
}
}
});

return headerLabel;
}
82 changes: 43 additions & 39 deletions interfaces/IBF-dashboard/src/app/components/chat/chat.component.ts
Original file line number Diff line number Diff line change
@@ -125,49 +125,53 @@ export class ChatComponent implements OnDestroy {
this.changedActions.length === 0;
}

public async submitEapAction(pcode: string) {
this.countryService
.getCountrySubscription()
.subscribe((country: Country) => {
this.analyticsService.logEvent(AnalyticsEvent.eapSubmit, {
placeCode: pcode,
page: AnalyticsPage.dashboard,
country: country.countryCodeISO3,
isActiveEvent: this.eventService.state.activeEvent,
isActiveTrigger: this.eventService.state.activeTrigger,
component: this.constructor.name,
});
});

this.triggeredAreas.find((i) => i.pcode === pcode).submitDisabled = true;
const activeCountry = this.countryService.getActiveCountry();

try {
const submitEAPActionResult = await Promise.all(
this.changedActions.map(async (action) => {
if (action.pcode === pcode) {
return this.eapActionsService.checkEapAction(
action.action,
activeCountry.countryCodeISO3,
action.checked,
action.pcode,
public async submitEapAction(pcode: string): Promise<void> {
return new Promise((): void => {
this.countryService.getCountrySubscription().subscribe(
async (country: Country): Promise<void> => {
this.analyticsService.logEvent(AnalyticsEvent.eapSubmit, {
placeCode: pcode,
page: AnalyticsPage.dashboard,
country: country.countryCodeISO3,
isActiveEvent: this.eventService.state.activeEvent,
isActiveTrigger: this.eventService.state.activeTrigger,
component: this.constructor.name,
});

this.triggeredAreas.find(
(i) => i.pcode === pcode,
).submitDisabled = true;

try {
let submitEAPActionResult = [];
submitEAPActionResult = await Promise.all(
this.changedActions.map(async (action) => {
if (action.pcode === pcode) {
return this.eapActionsService.checkEapAction(
action.action,
country.countryCodeISO3,
action.checked,
action.pcode,
);
} else {
return Promise.resolve();
}
}),
);
} else {
return Promise.resolve();
}
}),
);

this.changedActions = this.changedActions.filter(
(i) => i.pcode !== pcode,
);
this.changedActions = this.changedActions.filter(
(i) => i.pcode !== pcode,
);

this.actionResult(this.updateSuccessMessage, (): void =>
window.location.reload(),
this.actionResult(this.updateSuccessMessage, (): void =>
window.location.reload(),
);
} catch (e) {
this.actionResult(this.updateFailureMessage);
}
},
);
} catch (e) {
this.actionResult(this.updateFailureMessage);
}
});
}

private async actionResult(resultMessage: string, callback?: () => void) {
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
placeholder="Select Country"
interface="popover"
(ionChange)="handleCountryChange($event)"
[value]="countryService.activeCountry.countryCodeISO3"
[value]="country.countryCodeISO3"
>
<ion-select-option
*ngFor="let country of countryService.countries"
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import { Component } from '@angular/core';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { Country } from 'src/app/models/country.model';
import { CountryService } from 'src/app/services/country.service';

@Component({
selector: 'app-country-switcher',
templateUrl: './country-switcher.component.html',
styleUrls: ['./country-switcher.component.scss'],
})
export class CountrySwitcherComponent {
constructor(public countryService: CountryService) {}
export class CountrySwitcherComponent implements OnInit, OnDestroy {
private countrySubscription: Subscription;
public country: Country;

constructor(private countryService: CountryService) {}

ngOnInit() {
this.countrySubscription = this.countryService
.getCountrySubscription()
.subscribe((country: Country) => {
this.country = country;
});
}

ngOnDestroy() {
this.countrySubscription.unsubscribe();
}

public handleCountryChange($event) {
this.countryService.selectCountry($event.detail.value);
33 changes: 21 additions & 12 deletions interfaces/IBF-dashboard/src/app/components/map/map.component.ts
Original file line number Diff line number Diff line change
@@ -507,10 +507,14 @@ export class MapComponent implements OnDestroy {
let markerIcon: IconOptions;
let className: string;

const activeCountry = JSON.parse(
localStorage.getItem(this.countryService.countryLocalStorage),
);
const eapAlertClasses = activeCountry.eapAlertClasses;
let eapAlertClasses;

this.countryService
.getCountrySubscription()
.subscribe((country: Country) => {
eapAlertClasses = country.eapAlertClasses;
});

const glofasProbability = markerProperties.fc_prob;
Object.keys(eapAlertClasses).forEach((key) => {
if (
@@ -614,9 +618,14 @@ export class MapComponent implements OnDestroy {
}

private createMarkerStationPopup(markerProperties: Station): string {
const activeCountry = JSON.parse(
localStorage.getItem(this.countryService.countryLocalStorage),
);
let activeCountry: Country;

this.countryService
.getCountrySubscription()
.subscribe((country: Country) => {
activeCountry = country;
});

const eapAlertClasses = activeCountry.eapAlertClasses;
const glofasProbability = markerProperties.fc_prob;

@@ -652,11 +661,11 @@ export class MapComponent implements OnDestroy {

let lastAvailableLeadTime: LeadTime;

if (this.countryService.activeCountry) {
lastAvailableLeadTime = this.countryService.activeCountry
.countryLeadTimes[
this.countryService.activeCountry.countryLeadTimes.length - 1
];
if (activeCountry) {
lastAvailableLeadTime =
activeCountry.countryLeadTimes[
activeCountry.countryLeadTimes.length - 1
];
}

const leadTime =
Original file line number Diff line number Diff line change
@@ -29,8 +29,8 @@ export class TimelineComponent implements OnInit, OnDestroy {
ngOnInit() {
this.countrySubscription = this.countryService
.getCountrySubscription()
.subscribe(async (country: Country) => {
await this.timelineService.loadTimeStepButtons();
.subscribe((country: Country) => {
this.timelineService.loadTimeStepButtons();
});
}

Original file line number Diff line number Diff line change
@@ -26,7 +26,9 @@ export class UserStateComponent {
private analyticsService: AnalyticsService,
private eventService: EventService,
) {
this.authService.authenticationState$.subscribe(this.setDisplayName);
this.authService.getAuthSubscription().subscribe((user: User) => {
this.setDisplayName(user);
});
}

setDisplayName = (user: User) => {
Loading

0 comments on commit 60f998b

Please sign in to comment.