Skip to content

Commit

Permalink
Upgrade Admin Prices UI
Browse files Browse the repository at this point in the history
  • Loading branch information
damianlegawiec committed May 6, 2020
1 parent 6d58155 commit 5a91e24
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 62 deletions.
14 changes: 2 additions & 12 deletions backend/app/assets/javascripts/spree/backend/multi_currency.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,8 @@ $(document).ready(function () {
.change(function () {
var filter = $(this).val()
if (filter) {
$(list)
.find('.panel-title:not(:Contains(' + filter + '))')
.parent()
.parent()
.parent()
.hide()
$(list)
.find('.panel-title:Contains(' + filter + ')')
.parent()
.parent()
.parent()
.show()
$(list).find('.panel-title:not(:Contains(' + filter + '))').parent().hide()
$(list).find('.panel-title:Contains(' + filter + ')').parent().show()
} else {
$(list)
.find('.panel')
Expand Down
17 changes: 16 additions & 1 deletion backend/app/controllers/spree/admin/prices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ module Admin
class PricesController < ResourceController
belongs_to 'spree/product', find_by: :slug

helper_method :supported_currencies_for_all_stores

def create
params.require(:vp).permit!
params[:vp].each do |variant_id, prices|
next unless variant_id

variant = Spree::Variant.find(variant_id)
next unless variant

supported_currencies.each do |currency|
supported_currencies_for_all_stores.each do |currency|
price = variant.price_in(currency.iso_code)
price.price = (prices[currency.iso_code].blank? ? nil : prices[currency.iso_code])
price.save! if price.new_record? && price.price || !price.new_record? && price.changed?
Expand All @@ -18,6 +22,17 @@ def create
flash[:success] = Spree.t('notice_messages.prices_saved')
redirect_to admin_product_path(parent)
end

private

def supported_currencies_for_all_stores
@supported_currencies_for_all_stores = begin
(
Spree::Store.pluck(:supported_currencies).map { |c| c.split(',') }.flatten + Spree::Store.pluck(:default_currency)
).
compact.uniq.map { |code| ::Money::Currency.find(code.strip) }
end
end
end
end
end
62 changes: 36 additions & 26 deletions backend/app/views/spree/admin/prices/_variant_prices.html.erb
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
<% panel_class ||= 'panel-default' %>

<div class="col-md-6">
<div class="panel <%= panel_class %>">
<div class="panel-heading">
<h3 class="panel-title">
<span class="sku"><%= variant.sku %></span>

<span class="text-muted pull-right">
<%= variant.is_master? ? Spree.t(:master) : variant.options_text.blank? ? Spree.t(:variant) : variant.options_text %>
</span>
</h3>
</div>

<div class="panel-body no-padding-bottom">
<% supported_currencies.each do |currency| %>
<% code = currency.iso_code %>
<% price = variant.price_in(code) %>
<div class="form-group">
<%= label_tag "vp[#{variant.id}][#{code}]", code, class: 'control-label col-md-2' %>
<div class="col-md-10">
<%= text_field_tag "vp[#{variant.id}][#{code}]", (price && price.price ? price.display_amount.money : ''), class: 'form-control' %>
</div>
</div>
<% end %>
</div>
<div class="panel-body no-padding-bottom panel-default">
<div id="variant_prices-table-wrapper">
<table class="table sortable">
<colgroup>
<col style="width: 25%" />
<col style="width: 40%" />
<col style="width: 15%" />
<col style="width: 20%" />
</colgroup>
<thead>
<tr data-hook="variant_prices_header">
<th><%= Spree.t(:options) %></th>
<th><%= Spree.t(:sku) %></th>
<th><%= Spree.t(:currency) %></th>
<th><%= Spree.t(:price) %></th>
</tr>
</thead>
<tbody id="variant-prices" data-hook="variant_prices_body">
<% supported_currencies_for_all_stores.each do |currency| %>
<% code = currency.iso_code %>
<% price = variant.price_in(code) %>
<% @product.variants_including_master.each do |variant| %>
<tr id="variant_prices_row" data-hook="variant_prices_row">
<td class="panel">
<%= variant.is_master? ? Spree.t(:master) : variant.options_text.blank? ? Spree.t(:variant) : variant.options_text %>
</td>
<td class="panel-title"><%= variant.sku %></td>
<td><%= label_tag "vp[#{variant.id}][#{code}]", code %></td>
<td>
<%= text_field_tag "vp[#{variant.id}][#{code}]", (price && price.price ? price.display_amount.money : ''), class: 'form-control' %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
</div>
</div>
25 changes: 12 additions & 13 deletions backend/app/views/spree/admin/prices/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<%= render 'spree/admin/shared/product_tabs', current: 'Prices' %>
<%= render partial: 'spree/admin/shared/product_tabs', locals: { current: :prices } %>

<% content_for :page_actions do %>
<div class="page-actions col-6 pr-0" data-hook="toolbar">
<%= yield :page_actions %>
</div>
<%= product_preview_link(@product) %>
<% end %>

<form>
<div class="form-group has-feedback">
<input class="form-control input-lg" placeholder="<%= Spree.t('variant_prices_search_placeholder') %>" type="text" id="variant-price-search"/>
<span class="glyphicon glyphicon-search form-control-feedback" aria-hidden="true"></span>
<hr/>
</div>
</form>
<input class="form-control" placeholder="<%= Spree.t('variant_prices_search_placeholder') %>" type="text" id="variant-price-search"/>
</form>

<%= form_tag nil, { class: 'form-horizontal' } do %>

<div class="row" id="variant-prices">
<%= render 'variant_prices', variant: @product.master, panel_class: 'panel-primary' %>
<% @product.variants.each do |variant| %>
<%= render 'variant_prices', variant: variant %>
<% end %>
<div class="mt-3">
<%= render 'variant_prices', variant: @product.master %>
</div>
<div class="form-actions" data-hook="buttons">
<%= button Spree.t('actions.update'), 'update' %>
Expand Down
6 changes: 3 additions & 3 deletions backend/app/views/spree/admin/shared/_product_tabs.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
<% end if can?(:admin, Spree::StockItem) && !@product.deleted? %>

<%= content_tag :li do %>
<%= link_to_with_icon 'money',
<%= link_to_with_icon 'money',
Spree.t(:prices),
admin_product_prices_path(@product),
class: "nav-link #{'active' if current == Spree.t(:prices)}" %>
class: "nav-link #{'active' if current == :prices}" %>

<% end if Spree::Config.allow_currency_change == true %>
<% end if can?(:admin, Spree::Price) && !@product.deleted? %>
</ul>
<% end %>
14 changes: 7 additions & 7 deletions backend/spec/features/admin/products/variant_prices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@

describe 'Variant Prices', type: :feature, js: true do
stub_authorization!
let!(:store) { create(:store, default: true) }
let!(:store) { create(:store, default: true, supported_currencies: 'USD,EUR') }
let!(:store2) { create(:store, default: false, default_currency: 'GBP') }
let!(:product) { create(:product) }

context 'with USD and EUR as currencies' do
before do
reset_spree_preferences do |config|
config.allow_currency_change = true
end
end

it 'allows to save a price for each currency' do
visit spree.admin_product_path(product)
click_link 'Prices'
expect(page).to have_content 'USD'
expect(page).to have_content 'EUR'
expect(page).to have_content 'GBP'

fill_in "vp_#{product.master.id}_USD", with: '29.95'
fill_in "vp_#{product.master.id}_EUR", with: '21.94'
fill_in "vp_#{product.master.id}_GBP", with: '19.94'

click_button 'Update'
expect(page).to have_content 'Prices successfully saved'
expect(product.master.price_in('USD').amount).to eq(29.95)
expect(product.master.price_in('EUR').amount).to eq(21.94)
expect(product.master.price_in('GBP').amount).to eq(19.94)
end
end
end

0 comments on commit 5a91e24

Please sign in to comment.