Skip to content

Commit

Permalink
Code refactorings based on code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaudcolas committed Feb 14, 2024
1 parent 4ec22a4 commit e9727ea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
21 changes: 17 additions & 4 deletions client/src/controllers/LinkController.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { Controller } from '@hotwired/stimulus';

/**
* Adds the ability for the controlled element to reflect the URL's query parameters
* from an event or the current URL into the window.location so that dynamic updates
* to listings can be available on refreshing or other internal links.
*
* @example - Reflecting the URL's query parameters
* <a href="/?q=1" data-controller="w-link" data-w-link-reflect-keys-value="['q']"></a>
*/
export class LinkController extends Controller<HTMLElement> {
static values = {
attrName: { default: 'href', type: String },
preserveKeys: { default: [], type: Array },
reflectKeys: { default: ['__all__'], type: Array },
};

/** Attribute on the controlled element containing the URL string. */
declare attrNameValue: string;
/** URL param keys that will be kept in the current location's URL. */
declare preserveKeysValue: string[];
/** URL param keys to be added to the location URL from the source URL. */
declare reflectKeysValue: string[];

get url() {
Expand Down Expand Up @@ -59,14 +70,16 @@ export class LinkController extends Controller<HTMLElement> {
this.element.setAttribute(this.attrNameValue, currentUrl.toString());
}

setParams(e?: CustomEvent<{ requestUrl?: string }>) {
if (!e) {
setParams(event?: CustomEvent<{ requestUrl?: string }>) {
if (!event) {
this.setParamsFromURL(new URL(window.location.href));
return;
}

if (!e.detail?.requestUrl) return;
if (!event.detail?.requestUrl) return;

this.setParamsFromURL(new URL(e.detail.requestUrl, window.location.href));
this.setParamsFromURL(
new URL(event.detail.requestUrl, window.location.href),
);
}
}
4 changes: 2 additions & 2 deletions client/src/controllers/SwapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export class SwapController extends Controller<
static values = {
icon: { default: '', type: String },
loading: { default: false, type: Boolean },
src: { default: '', type: String },
reflect: { default: false, type: Boolean },
src: { default: '', type: String },
target: { default: '#listing-results', type: String },
wait: { default: 200, type: Number },
};
Expand All @@ -58,8 +58,8 @@ export class SwapController extends Controller<

declare iconValue: string;
declare loadingValue: boolean;
declare srcValue: string;
declare reflectValue: boolean;
declare srcValue: string;
declare targetValue: string;
declare waitValue: number;

Expand Down

0 comments on commit e9727ea

Please sign in to comment.