Skip to content

Commit

Permalink
router: convert path to query param
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelCoding authored and SuperSandro2000 committed Jun 26, 2024
1 parent 5b106fa commit 3c8b174
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ <h1>NüschtOS Search</h1>

<ul>
<li *ngFor="let option of results | async">
<a [routerLink]="option.name" queryParamsHandling="merge" routerLinkActive="active"><code>{{option.name}}</code></a>
<a [routerLink]="[]" [queryParams]="{option: option.name}" queryParamsHandling="merge" routerLinkActive="active"><code>{{option.name}}</code></a>
</li>
</ul>
</div>
</aside>

<main>
<div class="card">
<router-outlet />
<app-option></app-option>
</div>
</main>
13 changes: 8 additions & 5 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import { AfterViewInit, Component, OnInit } from '@angular/core';
import { AfterViewInit, ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { ActivatedRoute, Router, RouterLink, RouterOutlet } from '@angular/router';
import { Router, RouterLink, RouterOutlet } from '@angular/router';
import { SearchService } from './core/search.service';
import { of, switchMap } from 'rxjs';
import { AsyncPipe, NgFor } from '@angular/common';
import { TextFieldComponent } from "@feel/form";
import { OptionComponent } from './pages/option/option.component';

@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, AsyncPipe, NgFor, ReactiveFormsModule, TextFieldComponent, RouterLink],
imports: [RouterOutlet, AsyncPipe, NgFor, ReactiveFormsModule, TextFieldComponent, RouterLink, OptionComponent],
templateUrl: './app.component.html',
styleUrl: './app.component.scss'
styleUrl: './app.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent implements AfterViewInit {

protected readonly search = new FormControl();
protected readonly results = this.search.valueChanges.pipe(
switchMap(query => {
const q = query?.trim();
if (q !== AppComponent.getQuery()) this.router.navigate([], { queryParams: { query: q } });
if (q !== AppComponent.getQuery()) this.router.navigate([], { queryParams: { query: q }, queryParamsHandling: 'merge' });
if (q && q.length > 0) {
return this.searchService.search(q);
} else {
Expand Down
8 changes: 3 additions & 5 deletions src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
import { provideRouter } from '@angular/router';

import { routes } from './app.routes';
import { provideHttpClient } from '@angular/common/http';
import { provideRouter } from '@angular/router';

export const appConfig: ApplicationConfig = {
providers: [
provideZoneChangeDetection({ eventCoalescing: true }),
provideRouter(routes),
provideHttpClient()
provideRouter([]),
provideHttpClient(),
]
};
8 changes: 0 additions & 8 deletions src/app/app.routes.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/app/pages/empty/empty.component.html

This file was deleted.

Empty file.
23 changes: 0 additions & 23 deletions src/app/pages/empty/empty.component.spec.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/app/pages/empty/empty.component.ts

This file was deleted.

8 changes: 6 additions & 2 deletions src/app/pages/option/option.component.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<div *ngIf="(option | async) as option" class="typography">
<div *ngIf="(option | async) as option; else notFound" class="typography">
<h2><code>{{option.name}}</code></h2>
<table>
<tbody>
<tr>
<td>Declarations</td>
<td>
<a *ngFor="let declaration of option.declarations" [href]="declaration">{{declaration}}</a>
<a *ngFor="let declaration of option.declarations" [href]="declaration">{{declaration}}</a>
</td>
</tr>
<tr>
Expand All @@ -28,3 +28,7 @@ <h2><code>{{option.name}}</code></h2>
</tbody>
</table>
</div>

<ng-template #notFound>
<p>Try searching for something!</p>
</ng-template>
7 changes: 4 additions & 3 deletions src/app/pages/option/option.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from '@angular/core';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { SearchService } from '../../core/search.service';
import { ActivatedRoute } from '@angular/router';
import { switchMap } from 'rxjs';
Expand All @@ -9,11 +9,12 @@ import { AsyncPipe, NgFor, NgIf } from '@angular/common';
standalone: true,
imports: [NgIf, NgFor, AsyncPipe],
templateUrl: './option.component.html',
styleUrl: './option.component.scss'
styleUrl: './option.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class OptionComponent {

protected readonly option = this.activatedRoute.params.pipe(
protected readonly option = this.activatedRoute.queryParams.pipe(
switchMap(({option}) => this.searchService.getByName(option)),
);

Expand Down
2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="utf-8">
<title>NuschtSearch</title>
<base href="/">
<!--<base href="/">-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
Expand Down

0 comments on commit 3c8b174

Please sign in to comment.