Skip to content

Commit

Permalink
add url deep linking for editor tables
Browse files Browse the repository at this point in the history
  • Loading branch information
seiyria committed Oct 6, 2024
1 parent a5d16b7 commit 5de68ae
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 50 deletions.
71 changes: 21 additions & 50 deletions src/app/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
signal,
viewChild,
} from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { ActivatedRoute } from '@angular/router';
import { SwalComponent } from '@sweetalert2/ngx-sweetalert2';
import { isUndefined } from 'lodash';
import { LocalStorageService } from 'ngx-webstorage';
Expand All @@ -18,6 +18,7 @@ import { ElectronService } from '../services/electron.service';
import { ModService } from '../services/mod.service';
import { PinpointService } from '../services/pinpoint.service';
import { QueryService } from '../services/query.service';
import { URLService } from '../services/url.service';
import { ValidationService } from '../services/validation.service';

@Component({
Expand All @@ -26,8 +27,8 @@ import { ValidationService } from '../services/validation.service';
styleUrls: ['./home.component.scss'],
})
export class HomeComponent implements OnInit {
private router = inject(Router);
private route = inject(ActivatedRoute);
private urlService = inject(URLService);

private localStorage = inject(LocalStorageService);
public pinpointService = inject(PinpointService);
Expand Down Expand Up @@ -148,13 +149,9 @@ export class HomeComponent implements OnInit {

this.localStorage.store('lasttab', newTab);

void this.router.navigate([], {
relativeTo: this.route,
queryParamsHandling: 'merge',
queryParams: {
tab: newTab,
sub: '',
},
this.urlService.trackURLChanges({
tab: newTab,
sub: '',
});
}

Expand Down Expand Up @@ -198,12 +195,8 @@ export class HomeComponent implements OnInit {
this.analysisService.toggleAnalyzing(false);
this.queryService.toggleQuerying(false);

void this.router.navigate([], {
relativeTo: this.route,
queryParamsHandling: 'merge',
queryParams: {
sub: 'dependencies',
},
this.urlService.trackURLChanges({
sub: 'dependencies',
});
}

Expand All @@ -214,12 +207,8 @@ export class HomeComponent implements OnInit {
this.analysisService.toggleAnalyzing(false);
this.queryService.toggleQuerying(false);

void this.router.navigate([], {
relativeTo: this.route,
queryParamsHandling: 'merge',
queryParams: {
sub: 'validate',
},
this.urlService.trackURLChanges({
sub: 'validate',
});
}

Expand All @@ -230,12 +219,8 @@ export class HomeComponent implements OnInit {
this.analysisService.toggleAnalyzing(false);
this.queryService.toggleQuerying(false);

void this.router.navigate([], {
relativeTo: this.route,
queryParamsHandling: 'merge',
queryParams: {
sub: 'pinpoint',
},
this.urlService.trackURLChanges({
sub: 'pinpoint',
});
}

Expand All @@ -246,12 +231,8 @@ export class HomeComponent implements OnInit {
this.pinpointService.togglePinpointing(false);
this.queryService.toggleQuerying(false);

void this.router.navigate([], {
relativeTo: this.route,
queryParamsHandling: 'merge',
queryParams: {
sub: 'analyze',
},
this.urlService.trackURLChanges({
sub: 'analyze',
});
}

Expand All @@ -262,12 +243,8 @@ export class HomeComponent implements OnInit {
this.pinpointService.togglePinpointing(false);
this.analysisService.toggleAnalyzing(false);

void this.router.navigate([], {
relativeTo: this.route,
queryParamsHandling: 'merge',
queryParams: {
sub: 'query',
},
this.urlService.trackURLChanges({
sub: 'query',
});
}

Expand All @@ -291,21 +268,15 @@ export class HomeComponent implements OnInit {
updateSubURLProp(prop: string, value: string | number | undefined) {
if (isUndefined(value)) return;

void this.router.navigate([], {
relativeTo: this.route,
queryParamsHandling: 'merge',
queryParams: {
[prop]: value,
},
this.urlService.trackURLChanges({
[prop]: value,
});
}

resetSub() {
void this.router.navigate([], {
relativeTo: this.route,
queryParams: {
tab: this.activeTab(),
},
this.urlService.trackURLChanges({
tab: this.activeTab(),
sub: '',
});
}
}
2 changes: 2 additions & 0 deletions src/app/services/query.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export class QueryService {
...mod.spawners.map((c) => ({ type: 'spawner', data: c })),
...mod.stems.map((c) => ({ type: 'stem', data: c })),
...mod.traitTrees.map((c) => ({ type: 'traitTree', data: c })),
...mod.achievements.map((c) => ({ type: 'achievement', data: c })),
...mod.events.map((c) => ({ type: 'event', data: c })),
];
});

Expand Down
18 changes: 18 additions & 0 deletions src/app/services/url.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { inject, Injectable } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

@Injectable({
providedIn: 'root',
})
export class URLService {
private route = inject(ActivatedRoute);
private router = inject(Router);

public trackURLChanges(params: any) {
void this.router.navigate([], {
relativeTo: this.route,
queryParamsHandling: 'merge',
queryParams: params,
});
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Component, inject, OnInit, signal } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { FilterChangedEvent, FilterModel } from 'ag-grid-community';
import { merge } from 'lodash';
import { LocalStorageService } from 'ngx-webstorage';
import { HasIdentification, IModKit } from '../../../../interfaces';
import { ModService } from '../../../services/mod.service';
import { URLService } from '../../../services/url.service';

@Component({
selector: 'app-editor-base-table',
Expand All @@ -13,6 +15,9 @@ import { ModService } from '../../../services/mod.service';
export class EditorBaseTableComponent<T extends HasIdentification>
implements OnInit
{
private route = inject(ActivatedRoute);
private urlService = inject(URLService);

private localStorage = inject(LocalStorageService);
protected modService = inject(ModService);

Expand All @@ -30,9 +35,27 @@ export class EditorBaseTableComponent<T extends HasIdentification>
const state = this.localStorage.retrieve(
`${this.dataKey}-tablefilters`
) as FilterModel;

if (state) {
this.tableFilterState.set(state);
}

const loadItemId = this.getURLSubProp('id');
if (loadItemId) {
const potentialItems: T[] = this.modService.mod()[
this.dataKey
] as unknown as T[];

const item = potentialItems.find((i) => i._id === loadItemId);

if (item) {
this.editExisting(item);
}
}
}

getURLSubProp(prop: string): string | null {
return this.route.snapshot.queryParamMap.get(prop);
}

public createNew() {
Expand All @@ -49,11 +72,18 @@ export class EditorBaseTableComponent<T extends HasIdentification>

const finalItem = merge(defaultContent, clonedContent);
this.editingData.set(finalItem);
this.urlService.trackURLChanges({
id: data._id,
});
}

public cancelEditing() {
this.isEditing.set(false);
this.oldData.set(undefined);

this.urlService.trackURLChanges({
id: '',
});
}

public saveNewData(data: T) {
Expand Down

0 comments on commit 5de68ae

Please sign in to comment.