Skip to content

Commit

Permalink
Allow brand as search params in select device page
Browse files Browse the repository at this point in the history
Allow brand as search params in select device page
  • Loading branch information
pkong-ds authored May 30, 2024
2 parents 0679630 + d7b8fe8 commit dafb635
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
25 changes: 16 additions & 9 deletions public/scripts/device.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ function handleSearchInput(viewModel) {
}

function initializeSearch(viewModel, containerId) {
console.log(containerId);
const { autocomplete } = window["@algolia/autocomplete-js"];
autocomplete({
container: containerId,
Expand Down Expand Up @@ -187,9 +186,6 @@ function main() {
window.modelItems,
window.brandItems,
);
if (isDebug) {
window.viewModel = viewModel;
}

[
"#device-list__header__autocomplete",
Expand All @@ -200,7 +196,15 @@ function main() {

mobx.reaction(
() => viewModel.selectedBrand,
(selectedBrand) => {
(selectedBrand, prevBrand) => {
// check target exist
const targetBrandListItem = document.querySelector(
`#device-brand-list__item-button__${selectedBrand}`,
);

if (targetBrandListItem == null) {
viewModel.selectedBrand = prevBrand;
}
// Show non-selected styles for all brand tags
const allBrandListItems = document.querySelectorAll(
".device-brand-list__item-button",
Expand All @@ -210,10 +214,6 @@ function main() {
);

// Show selected styles for target brand tags
const targetBrandListItem = document.querySelector(
`#device-brand-list__item-button__${selectedBrand}`,
);

targetBrandListItem.classList.add(
"device-brand-list__item-button--selected",
);
Expand Down Expand Up @@ -252,4 +252,11 @@ function main() {
brandSelect.addEventListener("change", () =>
handleSelectBrandOption(brandSelect, viewModel),
);

const urlParams = new URLSearchParams(window.location.search);
const brandParam = urlParams.get("brand");

if (brandParam != null) {
viewModel.selectedBrand = brandParam;
}
}
2 changes: 1 addition & 1 deletion src/scripts/algolia/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export const getBrandItems = (deviceManager: DeviceManager) =>
return {
id: v.id,
name: v.name,
pathname: `/type/all`, // FIXME: add brand query param
pathname: `/type/all?brand=${v.id}`,
};
});

0 comments on commit dafb635

Please sign in to comment.