Skip to content

Commit

Permalink
refactor: #61 url params unifying
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbator committed Sep 29, 2024
1 parent 0d4da5c commit 692ff29
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
33 changes: 11 additions & 22 deletions src/app/game/components/data-menu/data-menu.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { KeyValuePipe } from '@angular/common';
import { ActivatedRoute, Router } from '@angular/router';
import { DataSelectCheckboxComponent } from './components/data-select-checkbox/data-select-checkbox.component';
import { DataDownloadComponent } from './components/data-download/data-download.component';
import { UrlParamService } from 'app/shared/services/url-param.service';

@Component({
selector: 'app-data-menu',
Expand Down Expand Up @@ -75,18 +76,15 @@ export class DataMenuComponent implements OnInit {
public isDataMenuVisible = false;
public dataSavingIntervalLimit = 100;

public constructor(
private _route$: ActivatedRoute,
private _router: Router
) {}
public constructor(private _urlParamService: UrlParamService) {}

public ngOnInit(): void {
setTimeout(() => {
this.dataToPersist = JSON.parse(
JSON.stringify(this.dataPossibleToPersist)
);
this.updateDataToPersistFromURL();
this.updateURLByDataToPersist('outputSpec', false);
this._urlParamService.setQueryParam('outputSpec', 'false');
}, 50);
}

Expand All @@ -105,21 +103,19 @@ export class DataMenuComponent implements OnInit {
delete this.dataToPersist[key];
}

this.updateURLByDataToPersist(key, isPresent);
this._urlParamService.setQueryParam(key, isPresent ? 'true' : 'false');
};

//

private updateDataToPersistFromURL(): void {
this._route$.queryParams.subscribe(params => {
for (const key in this.dataPossibleToPersist) {
this.updateDataToPersist(
key,
this.dataPossibleToPersist[key],
params[key] !== 'false'
);
}
});
for (const key in this.dataPossibleToPersist) {
this.updateDataToPersist(
key,
this.dataPossibleToPersist[key],
this._urlParamService.getQueryParam(key) !== 'false'
);
}
}

private updateCollectedData(): void {
Expand All @@ -137,11 +133,4 @@ export class DataMenuComponent implements OnInit {
this._lastSavedTime = Date.now();
}
}

private updateURLByDataToPersist(key: string, isPresent: boolean): void {
this._dataToPersistQueryParams[key] = isPresent;
this._router.navigate([], {
queryParams: this._dataToPersistQueryParams,
});
}
}
1 change: 0 additions & 1 deletion src/app/game/game.page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import { GameMenuComponent } from './components/game-menu/game-menu.component';
(pauseEmitter)="gamePauseSubject.next($event)"
(restartEmitter)="gameRestartSubject.next()" />
<app-data-menu
*appAuthRequired
[gameName]="game.name"
[setDataPossibleToPersist]="gameStateData" />
</div>
Expand Down
5 changes: 2 additions & 3 deletions src/app/home/models/author.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const authorsData: IAuthor[] = [
name: 'Marcin Bator',
githubName: 'marcinbator',
linkedinName: 'marcin-bator-ofc',
role: 'Backend Developer',
role: 'Fullstack engineer',
techStack: [
'Java/Spring Boot',
'C#/.NET',
Expand All @@ -21,8 +21,7 @@ export const authorsData: IAuthor[] = [
'Angular',
'SQL/PostgreSQL',
],
hobbies:
'developing backend systems, learning new technologies, riding a bike',
hobbies: 'mountains, bike, skiing, programming',
},
{
name: 'Paweł Buczek',
Expand Down
25 changes: 25 additions & 0 deletions src/app/shared/services/url-param.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Injectable } from '@angular/core';

@Injectable({
providedIn: 'root',
})
export class UrlParamService {
private _queryParams: URLSearchParams;

public constructor() {
this._queryParams = new URLSearchParams(window.location.search);
}

public getQueryParam(key: string): string | null {
return this._queryParams.get(key);
}

public setQueryParam(key: string, value: string): void {
this._queryParams.set(key, value);
window.history.replaceState(
{},
'',
`${window.location.pathname}?${this._queryParams}`
);
}
}

0 comments on commit 692ff29

Please sign in to comment.