-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract a
ui/search_panel
component from orders/cart
- Loading branch information
Showing
14 changed files
with
346 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 13 additions & 111 deletions
124
admin/app/components/solidus_admin/orders/cart/component.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,37 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
import { useClickOutside, useDebounce } from "stimulus-use" | ||
|
||
const QUERY_KEY = "q[name_or_variants_including_master_sku_cont]" | ||
import { useDebounce } from "stimulus-use" | ||
|
||
export default class extends Controller { | ||
static targets = ["result", "results", "searchField"] | ||
static values = { | ||
results: String, | ||
productsUrl: String, | ||
loadingText: String, | ||
initialText: String, | ||
emptyText: String, | ||
} | ||
static debounces = [ | ||
{ | ||
name: "requestSubmitForLineItems", | ||
wait: 500, | ||
}, | ||
"search", | ||
] | ||
|
||
get query() { | ||
return this.searchFieldTarget.value | ||
} | ||
|
||
get selectedResult() { | ||
// Keep the index within boundaries | ||
if (this.selectedIndex < 0) this.selectedIndex = 0 | ||
if (this.selectedIndex >= this.resultTargets.length) this.selectedIndex = this.resultTargets.length - 1 | ||
|
||
return this.resultTargets[this.selectedIndex] | ||
} | ||
static values = { productsUrl: String } | ||
static debounces = [{ name: "submitLineItems", wait: 500 }] | ||
|
||
connect() { | ||
useClickOutside(this) | ||
useDebounce(this) | ||
|
||
this.selectedIndex = 0 | ||
this.lineItemsToBeSubmitted = [] | ||
|
||
if (this.query) { | ||
this.showResults() | ||
this.search() | ||
} | ||
} | ||
|
||
selectResult(event) { | ||
switch (event.key) { | ||
case "Enter": | ||
event.preventDefault() | ||
this.selectedResult?.click() | ||
break | ||
case "ArrowUp": | ||
event.preventDefault() | ||
this.selectedIndex -= 1 | ||
this.render() | ||
break | ||
case "ArrowDown": | ||
event.preventDefault() | ||
this.selectedIndex += 1 | ||
this.render() | ||
break | ||
} | ||
} | ||
|
||
clickOutside() { | ||
this.openResults = false | ||
this.render() | ||
} | ||
|
||
async search() { | ||
const query = this.query | ||
|
||
if (query) { | ||
this.resultsValue = this.loadingTextValue | ||
this.render() | ||
|
||
this.resultsValue = (await (await fetch(`${this.productsUrlValue}?${QUERY_KEY}=${query}`)).text()) || this.emptyTextValue | ||
this.render() | ||
} else { | ||
this.resultsValue = this.initialTextValue | ||
this.render() | ||
} | ||
} | ||
|
||
showResults() { | ||
this.openResults = true | ||
this.render() | ||
async search({ detail: { query, controller } }) { | ||
controller.resultsValue = await ( | ||
await fetch(`${this.productsUrlValue}?q[name_or_variants_including_master_sku_cont]=${query}`) | ||
).text() | ||
} | ||
|
||
updateLineItem(event) { | ||
if (!this.lineItemsToBeSubmitted.includes(event.currentTarget)) { | ||
this.lineItemsToBeSubmitted.push(event.currentTarget) | ||
} | ||
|
||
this.requestSubmitForLineItems() | ||
this.submitLineItems() | ||
} | ||
|
||
// This is a workaround to permit using debounce when needing to pass a parameter | ||
requestSubmitForLineItems() { | ||
this.lineItemsToBeSubmitted.forEach((lineItem) => { | ||
lineItem.form.requestSubmit() | ||
}) | ||
submitLineItems() { | ||
this.lineItemsToBeSubmitted.forEach((lineItem) => lineItem.form.requestSubmit()) | ||
this.lineItemsToBeSubmitted = [] | ||
} | ||
|
||
render() { | ||
let resultsHtml = this.resultsValue | ||
|
||
if (this.renderedHtml !== resultsHtml) { | ||
this.renderedHtml = resultsHtml | ||
this.resultsTarget.innerHTML = resultsHtml | ||
} | ||
|
||
if (this.openResults && resultsHtml && this.query) { | ||
if (!this.resultsTarget.parentNode.open) this.selectedIndex = 0 | ||
|
||
for (const result of this.resultTargets) { | ||
if (result === this.selectedResult) { | ||
if (!result.hasAttribute("aria-selected") && result.scrollIntoViewIfNeeded) { | ||
// This is a non-standard method, but it's supported by all major browsers | ||
result.scrollIntoViewIfNeeded() | ||
} | ||
result.setAttribute("aria-selected", true) | ||
} else { | ||
result.removeAttribute("aria-selected") | ||
} | ||
} | ||
this.resultsTarget.parentNode.open = true | ||
} else { | ||
this.resultsTarget.parentNode.open = false | ||
} | ||
selectResult(event) { | ||
const form = event.detail.resultTarget.querySelector("form") | ||
form.submit() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 23 additions & 31 deletions
54
admin/app/components/solidus_admin/orders/cart/result/component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,26 @@ | ||
<%= form_for(@order.line_items.build(variant: @variant), url: solidus_admin.order_line_items_path(@order), method: :post, html: { | ||
"data-controller": "readonly-when-submitting #{stimulus_id}", | ||
"data-action": "click->#{stimulus_id}#submit", | ||
"data-#{component('orders/cart').stimulus_id}-target": "result", | ||
class: " | ||
rounded | ||
p-2 | ||
items-center | ||
flex | ||
hover:bg-gray-25 | ||
aria-selected:bg-gray-25 | ||
cursor-pointer | ||
" | ||
}) do |f| %> | ||
<%= hidden_field_tag("#{f.object_name}[variant_id]", @variant.id) %> | ||
<div class="flex gap-2 grow"> | ||
<%= render component("ui/thumbnail").new( | ||
src: @image&.url(:small), | ||
alt: @variant.name | ||
) %> | ||
<div class="flex-col"> | ||
<div class="leading-5 text-black body-small-bold"><%= @variant.name %></div> | ||
<div class="leading-5 text-gray-500 body-small"> | ||
SKU: | ||
<%= @variant.sku %> | ||
<%= @variant.options_text.presence&.prepend("- ") %> | ||
<%= render component('ui/search_panel/result').new do %> | ||
<%= form_for(@order.line_items.build(variant: @variant), url: solidus_admin.order_line_items_path(@order), method: :post, html: { | ||
"data-controller": "readonly-when-submitting", | ||
class: "flex items-center", | ||
}) do |f| %> | ||
<%= hidden_field_tag("#{f.object_name}[variant_id]", @variant.id) %> | ||
<div class="flex gap-2 grow"> | ||
<%= render component("ui/thumbnail").new( | ||
src: @image&.url(:small), | ||
alt: @variant.name | ||
) %> | ||
<div class="flex-col"> | ||
<div class="leading-5 text-black body-small-bold"><%= @variant.name %></div> | ||
<div class="leading-5 text-gray-500 body-small"> | ||
SKU: | ||
<%= @variant.sku %> | ||
<%= @variant.options_text.presence&.prepend("- ") %> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="flex gap-2 items-center"> | ||
<span class="text-gray-500 body-small"><%= render component("products/stock").from_variant(@variant) %></span> | ||
<span class="text-black body-small"><%= @variant.display_price.to_html %></span> | ||
</div> | ||
<div class="flex gap-2 items-center"> | ||
<span class="text-gray-500 body-small"><%= render component("products/stock").from_variant(@variant) %></span> | ||
<span class="text-black body-small"><%= @variant.display_price.to_html %></span> | ||
</div> | ||
<% end %> | ||
<% end %> |
Oops, something went wrong.