Skip to content

Commit

Permalink
Refactor FindOwnersView
Browse files Browse the repository at this point in the history
- Get rid of unnecessary processing of `router.location` here. The initial value for the last name comes via attribute when needed (from `OwnersListView`).
- Use a change event listener to update a `lastName` property for reactive programming approach instead of using a `Ref` to get the value in `findOwner()`.
- Only add the `lastName` search parameter to the `targetUrl` when `lastName` is not empty (cleaner URL after navigation in this case).
  • Loading branch information
Haprog committed Sep 30, 2021
1 parent 263d81f commit 3e53d5d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
29 changes: 12 additions & 17 deletions frontend/views/owners/find-owners-view.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { html } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { ref, createRef } from 'lit/directives/ref';
import '@vaadin/vaadin-button/vaadin-button'
import '@vaadin/vaadin-icons/vaadin-iconset'
import '@vaadin/vaadin-icon/vaadin-icon'
Expand All @@ -13,28 +12,17 @@ import { Router } from '@vaadin/router';
@customElement('find-owners-view')
export class FindOwnersView extends View {

@property({ type: String, attribute: 'initial-last-name' })
initialLastName?: string;
@property({ type: String, attribute: 'last-name' })
lastName: string = '';

@property({ type: String, attribute: 'hint-text' })
hintText?: string;

private textFieldRef = createRef<TextFieldElement>();

connectedCallback() {
super.connectedCallback();
const searchParams = new URLSearchParams(router.location.search);
const lastNameQuery = searchParams.get('lastName');
if (typeof lastNameQuery === 'string') {
this.initialLastName = lastNameQuery;
}
}

render() {
return html`
<h2>Find Owners</h2>
<vaadin-text-field label="Last name" .value="${this.initialLastName}" helper-text="${this.hintText}" clear-button-visible ${ref(this.textFieldRef)}>
<vaadin-text-field label="Last name" .value="${this.lastName}" @change="${this.lastNameChanged}" helper-text="${this.hintText}" clear-button-visible>
<vaadin-icon slot="prefix" icon="vaadin:search"></vaadin-icon>
</vaadin-text-field>
<vaadin-button @click="${this.findOwner}">Find Owner</vaadin-button><br>
Expand All @@ -43,9 +31,16 @@ export class FindOwnersView extends View {
`;
}

lastNameChanged(event: Event) {
const textField = event.target as TextFieldElement;
this.lastName = textField.value;
}

findOwner() {
const lastName = this.textFieldRef.value?.value || '';
const targetUrl = router.urlForName('owners-list') + '?lastName=' + encodeURIComponent(lastName);
let targetUrl = router.urlForName('owners-list');
if (this.lastName !== '') {
targetUrl += '?lastName=' + encodeURIComponent(this.lastName);
}
Router.go(targetUrl);
}

Expand Down
2 changes: 1 addition & 1 deletion frontend/views/owners/owners-list-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class OwnersListView extends View implements BeforeEnterObserver {

renderNotFound() {
return html`
<find-owners-view initial-last-name="${this.lastNameQuery}" hint-text="has not been found"></find-owners-view>
<find-owners-view last-name="${this.lastNameQuery}" hint-text="has not been found"></find-owners-view>
`;
}

Expand Down

0 comments on commit 3e53d5d

Please sign in to comment.