Skip to content

Commit

Permalink
feat(search-source): add settings in storage
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Apr 8, 2021
1 parent 55ce8f0 commit 3d53ed7
Show file tree
Hide file tree
Showing 25 changed files with 171 additions and 54 deletions.
48 changes: 46 additions & 2 deletions packages/auth/src/lib/shared/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HttpClient } from '@angular/common/http';

import { StorageService, StorageScope, ConfigService } from '@igo2/core';
import { AuthService } from './auth.service';
import { TokenService } from './token.service';
import { AuthStorageOptions } from './storage.interface';

@Injectable({
Expand All @@ -14,7 +15,8 @@ export class AuthStorageService extends StorageService {
constructor(
config: ConfigService,
private http: HttpClient,
private authService: AuthService
private authService: AuthService,
private tokenService: TokenService
) {
super(config);

Expand Down Expand Up @@ -61,6 +63,48 @@ export class AuthStorageService extends StorageService {
preference[key] = undefined;
this.http.patch(this.options.url, { preference }).subscribe();
}
return super.remove(key, scope);
super.remove(key, scope);
}

clear(scope: StorageScope = StorageScope.LOCAL) {
if (
scope === StorageScope.LOCAL &&
this.authService.authenticated &&
this.options.url
) {
this.http.patch(this.options.url, { preference: {}}, {
params: {
mergePreference: 'false'
}
}).subscribe();
}

let token: string;
if (scope === StorageScope.LOCAL) {
token = this.tokenService.get();
}

super.clear(scope);

if (token) {
this.tokenService.set(token);
}

if (
scope === StorageScope.LOCAL &&
this.authService.authenticated &&
this.options.url
) {
this.http
.get(this.options.url)
.subscribe((userIgo: { preference: object }) => {
if (userIgo && userIgo.preference) {
for (const key of Object.keys(userIgo.preference)) {
const value = userIgo.preference[key];
super.set(key, value);
}
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<div class="igo-user-button-more-container" [@userButtonState]="expand ? 'expand' : 'collapse'">

<igo-poi-button *ngIf="hasApi" [color]="color" [map]="map"></igo-poi-button>
<igo-bookmark-button *ngIf="hasApi" [color]="color" [map]="map"></igo-bookmark-button>

<button
mat-icon-button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ <h1 mat-dialog-title class="mat-typography">{{'igo.context.userButton.infoTitle'
<p>{{'igo.context.userButton.dialog.user' | translate}}: {{user.sourceId}}</p>
<p>{{'igo.context.userButton.dialog.email' | translate}}: {{user.email}}</p>
<p>{{'igo.context.userButton.dialog.expiration' | translate}}: {{exp}}</p>
<button mat-stroked-button color="primary" (click)="clearPreferences()">
{{'igo.context.userButton.dialog.clearPreferences' | translate}}
</button>
<br>
</div>
<div mat-dialog-actions style="justify-content: center">
<button mat-button color="primary"
<button mat-raised-button color="primary"
(click)="dialogRef.close(false)">
OK
</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component } from '@angular/core';
import { MatDialogRef } from '@angular/material/dialog';

import { StorageService } from '@igo2/core';
import { AuthService } from '@igo2/auth';

@Component({
Expand All @@ -13,10 +14,15 @@ export class UserDialogComponent {

constructor(
public dialogRef: MatDialogRef<UserDialogComponent>,
private auth: AuthService
private auth: AuthService,
private storageService: StorageService
) {
const decodeToken = this.auth.decodeToken();
this.user = decodeToken.user;
this.exp = new Date(decodeToken.exp * 1000).toLocaleString();
}

clearPreferences() {
this.storageService.clear();
}
}
3 changes: 2 additions & 1 deletion packages/context/src/locale/en.context.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@
"dialog": {
"email": "Email",
"expiration": "Expiration",
"user": "User"
"user": "User",
"clearPreferences": "Clear my preferences"
},
"infoTitle": "User info",
"logout": "Logout"
Expand Down
3 changes: 2 additions & 1 deletion packages/context/src/locale/fr.context.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@
"dialog": {
"email": "Courriel",
"expiration": "Expiration",
"user": "Utilisateur"
"user": "Utilisateur",
"clearPreferences": "Supprimer mes préférences"
},
"infoTitle": "Info sur l'utilisateur",
"logout": "Se déconnecter"
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/lib/storage/storage.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface StorageOptions {
}

export interface StorageServiceEvent {
key: string;
key?: string;
scope: StorageScope;
event: StorageServiceEventEnum;
previousValue?: any;
Expand All @@ -18,6 +18,6 @@ export interface StorageServiceEvent {
export enum StorageServiceEventEnum {
ADDED = 'Added',
MODIFIED = 'Modified',
REMOVED = 'Removed'
REMOVED = 'Removed',
CLEARED = 'Cleared'
}

9 changes: 9 additions & 0 deletions packages/core/src/lib/storage/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@ export class StorageService {
}
this.storageChange$.next({key, scope, event: StorageServiceEventEnum.REMOVED, previousValue });
}

clear(scope: StorageScope = StorageScope.LOCAL) {
if (scope === StorageScope.SESSION) {
sessionStorage.clear();
} else {
localStorage.clear();
}
this.storageChange$.next({scope, event: StorageServiceEventEnum.CLEARED });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
sourceCanSearch,
sourceCanReverseSearch
} from '../shared/search.utils';
import { MediaService } from '@igo2/core';
import { MediaService, StorageService } from '@igo2/core';

/**
* This component allows a user to select a search type yo enable. In it's
Expand Down Expand Up @@ -71,7 +71,8 @@ export class SearchSettingsComponent implements OnInit {

constructor(
private searchSourceService: SearchSourceService,
private mediaService: MediaService
private mediaService: MediaService,
private storageService: StorageService
) {}

ngOnInit(): void {
Expand Down Expand Up @@ -209,6 +210,12 @@ export class SearchSettingsComponent implements OnInit {

onCheckSearchSource(event: MatCheckboxChange, source: SearchSource) {
source.enabled = event.checked;
const storage = (this.storageService.get(source.getId() + '.options') || {}) as SettingOptions;
storage.enabled = source.enabled;
this.storageService.set(
source.getId() + '.options',
storage
);
this.searchSourceChange.emit(source);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpClient } from '@angular/common/http';

import { ConfigService, LanguageService } from '@igo2/core';
import { ConfigService, LanguageService, StorageService } from '@igo2/core';

import { SearchSource } from './source';
import { CadastreSearchSource } from './cadastre';
Expand All @@ -12,11 +12,13 @@ import { CadastreSearchSource } from './cadastre';
export function cadastreSearchSourceFactory(
http: HttpClient,
languageService: LanguageService,
storageService: StorageService,
config: ConfigService
) {
return new CadastreSearchSource(
http,
languageService,
storageService,
config.getConfig(`searchSources.${CadastreSearchSource.id}`),
);
}
Expand All @@ -29,6 +31,6 @@ export function provideCadastreSearchSource() {
provide: SearchSource,
useFactory: cadastreSearchSourceFactory,
multi: true,
deps: [HttpClient, LanguageService, ConfigService]
deps: [HttpClient, LanguageService, StorageService, ConfigService]
};
}
5 changes: 3 additions & 2 deletions packages/geo/src/lib/search/shared/sources/cadastre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { SearchResult } from '../search.interfaces';
import { SearchSource, TextSearch } from './source';
import { SearchSourceOptions, TextSearchOptions } from './source.interfaces';

import { LanguageService } from '@igo2/core';
import { LanguageService, StorageService } from '@igo2/core';
/**
* Cadastre search source
*/
Expand All @@ -24,9 +24,10 @@ export class CadastreSearchSource extends SearchSource implements TextSearch {
constructor(
private http: HttpClient,
private languageService: LanguageService,
storageService: StorageService,
@Inject('options') options: SearchSourceOptions
) {
super(options);
super(options, storageService);
}

getId(): string {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConfigService, LanguageService } from '@igo2/core';
import { ConfigService, LanguageService, StorageService } from '@igo2/core';

import { SearchSource } from './source';
import {
Expand Down Expand Up @@ -36,11 +36,13 @@ export function provideDefaultCoordinatesSearchResultFormatter() {
export function CoordinatesReverseSearchSourceFactory(
config: ConfigService,
languageService: LanguageService,
storageService: StorageService,
_projectionService: ProjectionService
) {
return new CoordinatesReverseSearchSource(
config.getConfig(`searchSources.${CoordinatesReverseSearchSource.id}`),
languageService,
storageService,
(config.getConfig('projections') as Projection[]) || []
);
}
Expand All @@ -53,6 +55,6 @@ export function provideCoordinatesReverseSearchSource() {
provide: SearchSource,
useFactory: CoordinatesReverseSearchSourceFactory,
multi: true,
deps: [ConfigService, LanguageService, ProjectionService]
deps: [ConfigService, LanguageService, StorageService, ProjectionService]
};
}
5 changes: 3 additions & 2 deletions packages/geo/src/lib/search/shared/sources/coordinates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SearchResult } from '../search.interfaces';
import { SearchSource, ReverseSearch } from './source';
import { SearchSourceOptions, ReverseSearchOptions } from './source.interfaces';

import { LanguageService } from '@igo2/core';
import { LanguageService, StorageService } from '@igo2/core';
import { GoogleLinks } from '../../../utils/googleLinks';
import { Projection } from '../../../map/shared/projection.interfaces';
import { lonLatConversion, roundCoordTo } from '../../../map/shared/map.utils';
Expand Down Expand Up @@ -45,9 +45,10 @@ export class CoordinatesReverseSearchSource extends SearchSource
constructor(
@Inject('options') options: SearchSourceOptions,
private languageService: LanguageService,
storageService: StorageService,
@Inject('projections') projections: Projection[],
) {
super(options);
super(options, storageService);
this.projections = projections;
this.languageService.translate
.get(this.options.title)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injector } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { ConfigService, LanguageService } from '@igo2/core';
import { ConfigService, LanguageService, StorageService } from '@igo2/core';

import { SearchSource } from './source';
import {
Expand Down Expand Up @@ -38,13 +38,15 @@ export function provideDefaultIChercheSearchResultFormatter() {
export function ichercheSearchSourceFactory(
http: HttpClient,
languageService: LanguageService,
storageService: StorageService,
config: ConfigService,
formatter: IChercheSearchResultFormatter,
injector: Injector
) {
return new IChercheSearchSource(
http,
languageService,
storageService,
config.getConfig(`searchSources.${IChercheSearchSource.id}`),
formatter,
injector
Expand All @@ -62,6 +64,7 @@ export function provideIChercheSearchSource() {
deps: [
HttpClient,
LanguageService,
StorageService,
ConfigService,
IChercheSearchResultFormatter,
Injector
Expand All @@ -76,12 +79,14 @@ export function provideIChercheSearchSource() {
export function ichercheReverseSearchSourceFactory(
http: HttpClient,
languageService: LanguageService,
storageService: StorageService,
config: ConfigService,
injector: Injector
) {
return new IChercheReverseSearchSource(
http,
languageService,
storageService,
config.getConfig(`searchSources.${IChercheReverseSearchSource.id}`),
injector
);
Expand All @@ -95,6 +100,6 @@ export function provideIChercheReverseSearchSource() {
provide: SearchSource,
useFactory: ichercheReverseSearchSourceFactory,
multi: true,
deps: [HttpClient, LanguageService, ConfigService, Injector]
deps: [HttpClient, LanguageService, StorageService, ConfigService, Injector]
};
}
Loading

0 comments on commit 3d53ed7

Please sign in to comment.