Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

link search route #236

Merged
merged 4 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
yarn lint-staged
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { CommonModule } from '@angular/common';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { PicsaTranslateModule } from '@picsa/shared/modules';
import Fuse, { FuseResult, IFuseOptions } from 'fuse.js';
import { Subject, takeUntil } from 'rxjs';

import { ResourcesComponentsModule } from '../../components/components.module';
import { IResourceBase, IResourceCollection, IResourceFile, IResourceLink } from '../../schemas';
Expand All @@ -22,7 +24,7 @@ interface ISearchResultsByType {
styleUrls: ['./search.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ResourceSearchComponent implements OnInit {
export class ResourceSearchComponent implements OnInit, OnDestroy {
query: string = '';

// https://www.fusejs.io/api/options.html
Expand All @@ -43,12 +45,27 @@ export class ResourceSearchComponent implements OnInit {

searchResults: ISearchResultsByType = { collection: [], file: [], link: [] };

private componentDestroyed$ = new Subject<boolean>();

/** Store total number of results across types */
public totalResults?: number;

constructor(private service: ResourcesToolService, private cdr: ChangeDetectorRef) {}
constructor(
private service: ResourcesToolService,
private cdr: ChangeDetectorRef,
private router: Router,
private route: ActivatedRoute
) {}

async ngOnInit() {
await this.initializeServiceData();
this.subscribeToQueryParams();
}
async ngOnDestroy() {
this.componentDestroyed$.next(true);
}

private async initializeServiceData() {
await this.service.ready();
const fileDocs = await this.service.dbFiles.find().exec();
const linkDocs = await this.service.dbLinks.find().exec();
Expand All @@ -58,15 +75,26 @@ export class ResourceSearchComponent implements OnInit {
this.fuse = new Fuse(allResources, this.fuseOptions);
}

private subscribeToQueryParams() {
this.route.queryParams.pipe(takeUntil(this.componentDestroyed$)).subscribe((params: Params) => {
if (params.searchText) {
this.query = params.searchText;
this.onSearchInputChange();
}
});
}

onSearchInputChange() {
// Only display search results if user has typed more than 2 characters
if (this.query.length > 2) {
const searchResults = this.fuse.search(this.query);
this.setSearchResultsByType(searchResults);
this.totalResults = searchResults.length;
this.setRouteSearchParam(this.query);
} else {
this.searchResults = { collection: [], file: [], link: [] };
this.totalResults = undefined;
this.setRouteSearchParam(undefined);
}
this.cdr.markForCheck();
}
Expand All @@ -86,4 +114,10 @@ export class ResourceSearchComponent implements OnInit {
}
this.searchResults = searchResults;
}

/** Update route param to match search text. Passing undefined will remove the param */
private setRouteSearchParam(searchText?: string) {
const queryParams = { searchText };
this.router.navigate([], { relativeTo: this.route, queryParams });
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
},
"packageManager": "[email protected]",
"lint-staged": {
"*.js": "eslint --cache --fix",
"*.{js,css,md}": "prettier --write"
"*.ts": "eslint --cache --fix",
"*.{ts,scss}": "prettier --write"
}
}
Loading