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

Fix variant select ingredient #112

Merged
merged 3 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
77 changes: 50 additions & 27 deletions app/assets/javascripts/alchemy/solidus/admin/variant_select.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
//= require alchemy/solidus/admin/select2_config

$.fn.alchemyVariantSelect = function(options) {
var config = Alchemy.Solidus.getSelect2Config(options)
$.fn.alchemyVariantSelect = function (options) {
const config = Alchemy.Solidus.getSelect2Config(options)

this.select2($.extend(true, config, {
ajax: {
data: function(term, page) {
return {
q: $.extend({
product_name_or_sku_cont: term
}, options.query_params),
page: page
}
function formatSelection(variant) {
return variant.options_text
? `${variant.name} - ${variant.options_text}`
: variant.name
}

function formatResult(variant, _el, query) {
const matchTerm = new RegExp(query.term, "gi")
const formatMatch = (match) => `<em>${match}</em>`
const name = variant.name.replace(matchTerm, formatMatch)
const sku = variant.sku.replace(matchTerm, formatMatch)
return `
<div class="variant-select-result">
<div>
<span>${name}</span>
</div>
<div>
<span>${variant.options_text}</span>
<span>${sku}</span>
</div>
</div>
`
}

this.select2(
$.extend(true, config, {
ajax: {
data: function (term, page) {
return {
q: $.extend(
{
product_name_or_sku_cont: term,
},
options.query_params
),
page: page,
}
},
results: function (data, page) {
return {
results: data.variants,
more: page * data.per_page < data.total_count,
}
},
},
results: function(data, page) {
return {
results: data.variants.map(function(variant) {
return {
id: variant.id,
text: variant.frontend_display
}
}),
more: page * data.per_page < data.total_count
}
}
},
formatSelection: function(variant) {
return variant.text || variant.frontend_display
}
}))
formatSelection,
formatResult,
})
)
}
22 changes: 21 additions & 1 deletion app/views/alchemy/ingredients/_spree_variant_editor.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@
<% end %>
<% end %>

<style>
.variant-select-result > * {
display: flex;
justify-content: space-between;
min-height: 16px;

> span {
display: inline-block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

&:last-child {
color: hsl(224, 8%, 63%);
}
}
</style>

<script>
$('#<%= spree_variant_editor.form_field_id(:variant_id) %>').alchemyVariantSelect({
placeholder: "<%= Alchemy.t(:search_variant, scope: 'solidus') %>",
Expand All @@ -19,7 +38,8 @@
<% if spree_variant_editor.variant %>
initialSelection: {
id: <%= spree_variant_editor.variant.id %>,
text: "<%= spree_variant_editor.variant.name %>"
name: "<%= spree_variant_editor.variant.name %>",
options_text: "<%= spree_variant_editor.variant.options_text %>"
}
<% end %>
})
Expand Down
Loading