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

optimized searchbar: only words, removed bug in parse rows #424

Merged
merged 6 commits into from
Mar 30, 2020
Merged
Changes from 2 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
31 changes: 18 additions & 13 deletions src/app/core/ui/search/search.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { EntitySchemaService } from '../../entity/schema/entity-schema.service';
styleUrls: ['./search.component.scss'],
})
export class SearchComponent implements OnInit {
results;
results = [];
searchText = '';
showSearchToolbar = false;

Expand Down Expand Up @@ -45,17 +45,22 @@ export class SearchComponent implements OnInit {

async search() {
this.searchText = this.searchText.toLowerCase();

const searchHash = JSON.stringify(this.searchText);
const searchTerms = this.searchText.split(' ');
const queryResults = await this.db.query(
'search_index/by_name',
{startkey: searchTerms[0], endkey: searchTerms[0] + '\ufff0', include_docs: true},
);

if (JSON.stringify(this.searchText) === searchHash) {
// only set result if the user hasn't continued typing and change the search term already
this.results = this.prepareResults(queryResults.rows, searchTerms);
const regexp = new RegExp('[a-z]+|[0-9]+');
// Only search for words starting with a char or number -> no searching for space or no input
if (this.searchText.match(regexp)) {
const searchHash = JSON.stringify(this.searchText);
const searchTerms = this.searchText.split(' ');
const queryResults = await this.db.query(
'search_index/by_name',
{startkey: searchTerms[0], endkey: searchTerms[0] + '\ufff0', include_docs: true},
);

if (JSON.stringify(this.searchText) === searchHash) {
// only set result if the user hasn't continued typing and change the search term already
this.results = this.prepareResults(queryResults.rows, searchTerms);
}
} else {
this.results = [];
}
}

Expand All @@ -76,7 +81,7 @@ export class SearchComponent implements OnInit {
} else if (r.doc._id.startsWith(School.ENTITY_TYPE + ':')) {
resultEntity = new School(r.doc.entityId);
} else {
return;
continue;
}

this.entitySchemaService.loadDataIntoEntity(resultEntity, r.doc);
Expand Down