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

feat: user can select a facet value eg category which is not yet displayed - fixes #125 #134

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
42d016f
feat: add autocomplete and start to search facet
Kout95 Jun 5, 2024
105c424
fix
Kout95 Jun 5, 2024
d3d533f
fix: lint and move mixins to /mixins
Kout95 Jun 6, 2024
79f4400
fix: bind this to debounce fn
Kout95 Jun 6, 2024
754b07e
feat: link autocomplete data
Kout95 Jun 6, 2024
667aa3e
fix: checkbox
Kout95 Jun 7, 2024
6c00f40
feat: add showOther property
Kout95 Jun 7, 2024
3364a3d
feat: add global reset-button.ts
Kout95 Jun 7, 2024
570a9da
fix
Kout95 Jun 7, 2024
7c03773
fix: show other only in some facet and fix checkbox
Kout95 Jun 8, 2024
c49bea0
feat: add --searchalicious-hover-color
Kout95 Jun 8, 2024
d79f2c3
feat: add --searchalicious-autocomplete-selected-background-color
Kout95 Jun 8, 2024
7e04715
fix: weird behaviour on firefox
Kout95 Jun 8, 2024
d788f1c
Update frontend/src/mixins/utils.ts
Kout95 Jun 10, 2024
ad61231
feat: add comments
Kout95 Jun 10, 2024
f06ac94
feat: add comments
Kout95 Jun 10, 2024
6d64352
feat: add comments
Kout95 Jun 10, 2024
442e525
refacto: handleKeyDown to improve readibility
Kout95 Jun 10, 2024
bfc5a8e
refacto: handleKeyDown to improve readibility
Kout95 Jun 10, 2024
895775b
docs: add comment to handleInput
Kout95 Jun 10, 2024
ddbd6e1
docs: add comment to handleInput
Kout95 Jun 11, 2024
19a2dda
docs: add comment to handleInput
Kout95 Jun 11, 2024
77a6f2f
docs: add docs
Kout95 Jun 11, 2024
b857d69
docs: add docs
Kout95 Jun 11, 2024
17d902a
docs: add docs
Kout95 Jun 11, 2024
0f2e7bf
Update frontend/src/search-autocomplete.ts
Kout95 Jun 12, 2024
418c6f8
Update frontend/src/mixins/taxonomies-ctl.ts
Kout95 Jun 12, 2024
a5921fe
Update frontend/src/mixins/taxonomies-ctl.ts
Kout95 Jun 12, 2024
1dc2ab4
Update frontend/src/search-facets.ts
Kout95 Jun 12, 2024
1fe348b
Update frontend/src/search-checkbox.ts
Kout95 Jun 12, 2024
635660b
Update frontend/src/mixins/taxonomies-ctl.ts
Kout95 Jun 12, 2024
cfeaa1e
refacto: fix vars and rename file
Kout95 Jun 12, 2024
c35b301
feat: add part
Kout95 Jun 12, 2024
3c674ca
feat: add comments
Kout95 Jun 12, 2024
84bc534
feat: add comments
Kout95 Jun 12, 2024
8aeb289
feat: add comments
Kout95 Jun 12, 2024
61d49f2
feat: add comments
Kout95 Jun 12, 2024
ba0212e
feat: add comments
Kout95 Jun 12, 2024
9ed4e4b
Apply suggestions from code review
Kout95 Jun 12, 2024
403cec7
fix: lint
Kout95 Jun 12, 2024
40def3a
fix: add enum
Kout95 Jun 12, 2024
46b7d0e
feat: clean code, modify reset button in facet, update reset filters …
Kout95 Jun 12, 2024
b5a0bee
feat: add margin top to fieldset
Kout95 Jun 12, 2024
bfc0dc2
docs
Kout95 Jun 12, 2024
67c6fc7
docs
Kout95 Jun 12, 2024
8cf0a9b
refacto button-transparent
Kout95 Jun 12, 2024
5eebce0
fix
Kout95 Jun 12, 2024
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
4 changes: 2 additions & 2 deletions frontend/public/off.html
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@
<div class="row" style="padding-top: 1rem">
<div class="large-2 columns">
<searchalicious-facets search-name="off">
<searchalicious-facet-terms name="brands_tags"></searchalicious-facet-terms>
<searchalicious-facet-terms name="categories_tags"></searchalicious-facet-terms>
<searchalicious-facet-terms name="brands_tags" show-other="true"></searchalicious-facet-terms>
<searchalicious-facet-terms name="categories_tags" show-other="true"></searchalicious-facet-terms>
<searchalicious-facet-terms name="nutrition_grades"></searchalicious-facet-terms>
<searchalicious-facet-terms name="ecoscore_grade"></searchalicious-facet-terms>
</searchalicious-facets>
Expand Down
71 changes: 71 additions & 0 deletions frontend/src/button-transparent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {LitElement, html, css} from 'lit';
import {customElement} from 'lit/decorators.js';

import {BasicEvents} from './utils/enums';

/**
* A custom element that represents a button without background for a search.
* It sends a custom event "click" when clicked.
* It exists to have already styled button for secondary actions.
* You can modify this variable to customize the button style :
* --button-transparent-padding
* --secondary-hover-color
* @extends {LitElement}
* @slot - This slot is for the button contents, default to "Search" string.
*/
@customElement('searchalicious-button-transparent')
export class SearchaliciousButtonTransparent extends LitElement {
static override styles = css`
.button-transparent {
background-color: transparent;
display: inline-flex;
align-items: center;
justify-content: center;
border: none;
border-radius: 3.5rem;
cursor: pointer;
padding: var(--button-transparent-padding, 0.25rem 0.5rem);
}
.button-transparent:hover {
background-color: var(--secondary-hover-color, #cfac9e);
}
`;

private _onClick() {
this._dispatchEvent();
}

private _onKeyUp(event: Event) {
const kbd_event = event as KeyboardEvent;
if (kbd_event.key === 'Enter') {
this._dispatchEvent();
}
}

private _dispatchEvent() {
this.dispatchEvent(
new CustomEvent(BasicEvents.CLICK, {bubbles: true, composed: true})
);
}

override render() {
return html`
<button
@click=${this._onClick}
@keyup=${this._onKeyUp}
part="button-transparent"
role="button"
class="button-transparent"
type="button"
>
<slot></slot>
</button>
`;
}
}

declare global {
interface HTMLElementTagNameMap {
'searchalicious-button-transparent': SearchaliciousButtonTransparent;
}
}
8 changes: 0 additions & 8 deletions frontend/src/enums.ts

This file was deleted.

51 changes: 51 additions & 0 deletions frontend/src/icons/cross.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {LitElement, html, css} from 'lit';
import {customElement} from 'lit/decorators.js';

/**
* A custom element that represents a cross icon.
* You can modify this variable to customize the icon style :
* --icon-width by default 0.8rem
* --icon-stroke-color by default black
*/
@customElement('searchalicious-icon-cross')
export class SearchaliciousIconCross extends LitElement {
static override styles = css`
svg {
width: var(--icon-width, 0.8rem);
}
svg line {
stroke: var(--icon-stroke-color, black);
}
`;
override render() {
return html`
<svg
part="icon-cross"
viewBox="0 0 100 100"
preserveAspectRatio="xMidYMid meet"
xmlns="http://www.w3.org/2000/svg"
>
<line
x1="10"
y1="10"
x2="90"
y2="90"
style="stroke:black;stroke-width:10"
/>
<line
x1="90"
y1="10"
x2="10"
y2="90"
style="stroke:black;stroke-width:10"
/>
</svg>
`;
}
}

declare global {
interface HTMLElementTagNameMap {
'searchalicious-icon-cross': SearchaliciousIconCross;
}
}
42 changes: 42 additions & 0 deletions frontend/src/mixins/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {LitElement} from 'lit';
import {Constructor} from './utils';

/**
* Interface for the DebounceMixin.
* It defines the structure that DebounceMixin should adhere to.
*/
export interface DebounceMixinInterface {
timeout?: number;
debounce<F extends () => void>(func: F, wait?: number): void;
}

/**
* A mixin class for debouncing function calls.
* It extends the LitElement class and adds debouncing functionality.
* It is used to prevent a function from being called multiple times in a short period of time.
* It is usefull to avoid multiple calls to a function when the user is typing in an input field.
Comment on lines +14 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great comment :-)

* @param {Constructor<LitElement>} superClass - The superclass to extend from.
* @returns {Constructor<DebounceMixinInterface> & T} - The extended class with debouncing functionality.
*/
export const DebounceMixin = <T extends Constructor<LitElement>>(
superClass: T
): Constructor<DebounceMixinInterface> & T =>
class extends superClass {
timeout?: number = undefined;

/**
* Debounces a function call.
* It delays the execution of the function until after wait milliseconds have elapsed since the last time this function was invoked.
* @param {Function} func - The function to debounce.
* @param {number} wait - The number of milliseconds to delay.
*/
debounce<F extends () => void>(func: F, wait = 300): void {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
this.timeout = undefined;
func.bind(self)();
}, wait);
}
} as Constructor<DebounceMixinInterface> & T;
43 changes: 43 additions & 0 deletions frontend/src/mixins/search-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {LitElement} from 'lit';
import {Constructor} from './utils';
import {BaseSearchDetail, LaunchSearchEvent} from '../events';
import {SearchaliciousEvents} from '../utils/enums';
import {property} from 'lit/decorators.js';

export interface SearchActionMixinInterface {
searchName: string;
_launchSearch(): Promise<void>;
}

/**
* A mixin class for search actions.
* It extends the LitElement class and adds search functionality.
* It is used to launch a search event.
* @param {Constructor<LitElement>} superClass - The superclass to extend from.
* @returns {Constructor<SearchActionMixinInterface> & T} - The extended class with search functionality.
*/
export const SearchActionMixin = <T extends Constructor<LitElement>>(
Kout95 marked this conversation as resolved.
Show resolved Hide resolved
superClass: T
): Constructor<SearchActionMixinInterface> & T => {
class SearchActionMixinClass extends superClass {
@property({attribute: 'search-name'})
searchName = 'searchalicious';

/**
* Launches a search event.
* It creates a new event with the search name and dispatches it.
*/
_launchSearch() {
const detail: BaseSearchDetail = {searchName: this.searchName};
// fire the search event
const event = new CustomEvent(SearchaliciousEvents.LAUNCH_SEARCH, {
bubbles: true,
composed: true,
detail: detail,
}) as LaunchSearchEvent;
this.dispatchEvent(event);
}
}

return SearchActionMixinClass as Constructor<SearchActionMixinInterface> & T;
};
21 changes: 11 additions & 10 deletions frontend/src/search-ctl.ts → frontend/src/mixins/search-ctl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ import {property, state} from 'lit/decorators.js';
import {
EventRegistrationInterface,
EventRegistrationMixin,
} from './event-listener-setup';
import {SearchaliciousEvents} from './enums';
} from '../event-listener-setup';
import {SearchaliciousEvents} from '../utils/enums';
import {
ChangePageEvent,
LaunchSearchEvent,
SearchResultEvent,
SearchResultDetail,
} from './events';
import {SearchaliciousFacets} from './search-facets';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Constructor<T = {}> = new (...args: any[]) => T;

} from '../events';
import {SearchaliciousFacets} from '../search-facets';
import {Constructor} from './utils';
export interface SearchaliciousSearchInterface
extends EventRegistrationInterface {
query: string;
Expand Down Expand Up @@ -176,14 +173,18 @@ export const SearchaliciousSearchMixin = <T extends Constructor<LitElement>>(
)
.sort() // for perdictability in tests !
.join('&');

return `${baseUrl}/search?${queryStr}`;
}

// connect to our specific events
override connectedCallback() {
super.connectedCallback();
this.addEventHandler(SearchaliciousEvents.LAUNCH_SEARCH, (event: Event) =>
this._handleSearch(event)
this.addEventHandler(
SearchaliciousEvents.LAUNCH_SEARCH,
(event: Event) => {
this._handleSearch(event);
}
);
this.addEventHandler(SearchaliciousEvents.CHANGE_PAGE, (event) =>
this._handleChangePage(event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import {property, state} from 'lit/decorators.js';
import {
EventRegistrationInterface,
EventRegistrationMixin,
} from './event-listener-setup';
import {SearchaliciousEvents} from './enums';
import {SearchResultEvent} from './events';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Constructor<T = {}> = new (...args: any[]) => T;
} from '../event-listener-setup';
import {SearchaliciousEvents} from '../utils/enums';
import {SearchResultEvent} from '../events';
import {Constructor} from './utils';

export interface SearchaliciousResultsCtlInterface
extends EventRegistrationInterface {
Expand Down
Loading