-
-
Notifications
You must be signed in to change notification settings - Fork 491
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
#4398: Support units in requests #4510
Changes from 7 commits
704ad63
ab8315c
895c980
3b44c02
9b929c4
3529e4c
e043ede
0a74e07
270c28f
9ce328e
71d7626
5799256
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
|
||
// Connects to data-controller="form-input" | ||
export default class extends Controller { | ||
static targets = ["itemSelect", "requestSelect"] | ||
static values = { | ||
// hash of (item ID => hash of (request unit name => request unit plural name)) | ||
"itemUnits": Object | ||
} | ||
|
||
addOption(val, text, selected) { | ||
let option = document.createElement("option"); | ||
option.value = val; | ||
option.text = text; | ||
if (selected) { | ||
option.selected = true; | ||
} | ||
this.requestSelectTarget.appendChild(option); | ||
} | ||
|
||
clearOptions() { | ||
while (this.requestSelectTarget.options.length > 0) { | ||
this.requestSelectTarget.remove(this.requestSelectTarget.options[0]) | ||
} | ||
} | ||
|
||
connect() { | ||
this.itemSelected(); | ||
} | ||
|
||
itemSelected() { | ||
if (!this.hasRequestSelectTarget) { | ||
return; | ||
} | ||
let option = this.itemSelectTarget.options[this.itemSelectTarget.selectedIndex] | ||
let units = this.itemUnitsValue[option.value] | ||
if (!units || Object.keys(units).length === 0) { | ||
this.requestSelectTarget.style.display = 'none'; | ||
this.requestSelectTarget.selectedIndex = -1; | ||
} | ||
else { | ||
this.requestSelectTarget.style.display = 'inline'; | ||
this.clearOptions() | ||
this.addOption('', 'Units') | ||
for (const [index, [name, displayName]] of Object.entries(Object.entries(units))) { | ||
this.addOption(name, displayName, index === "0") | ||
} | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,25 @@ | ||
<%= form.fields_for :item_requests, defined?(object) ? object : nil do |field| %> | ||
<tr> | ||
<tr data-controller="item-units" data-item-units-item-units-value="<%= item_units.to_json %>"> | ||
<td> | ||
<%= field.label :item_id, "Item Requested", {class: 'sr-only'} %> | ||
<%= field.select :item_id, @requestable_items, {include_blank: 'Select an item'}, {class: 'form-control'} %> | ||
<%= field.select :item_id, @requestable_items, {include_blank: 'Select an item'}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So here we already only show the |
||
data: { :'item-units-target' => 'itemSelect', | ||
action: 'change->item-units#itemSelected'}, | ||
class: 'form-control' %> | ||
</td> | ||
<td> | ||
<%= field.label :quantity, "Quantity", {class: 'sr-only'} %> | ||
<%= field.number_field :quantity, label: false, step: 1, min: 1, class: 'form-control' %> | ||
</td> | ||
|
||
<% if Flipper.enabled?(:enable_packs) && current_partner.organization.request_units.any? %> | ||
<td> | ||
<%= field.label :request_unit, "Unit", {class: 'sr-only'} %> | ||
<%= field.select :request_unit, [], {include_blank: 'Units'}, | ||
{ :'data-item-units-target' => 'requestSelect', class: 'form-control'} %> | ||
|
||
</td> | ||
<% end %> | ||
<td> | ||
<%= remove_element_button "Remove", container_selector: "tr" %> | ||
</td> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
RSpec.describe "Partners profile served area behaviour", type: :system, js: true do | ||
let(:organization) { create(:organization) } | ||
let(:partner) { create(:partner, organization: organization) } | ||
let(:partner_user) { partner.primary_user } | ||
|
||
before(:each) do | ||
sign_in(partner_user) | ||
end | ||
|
||
describe "GET #index" do | ||
let!(:item1) { FactoryBot.create(:item, organization: organization, name: "Item 1") } | ||
let!(:item2) { FactoryBot.create(:item, organization: organization, name: "Item 2") } | ||
before(:each) do | ||
FactoryBot.create(:item_unit, name: "pack", item: item1) | ||
end | ||
|
||
context "with packs off" do | ||
before(:each) do | ||
Flipper.disable(:enable_packs) | ||
end | ||
|
||
it "should not show packs on selection" do | ||
visit new_partners_request_path | ||
select "Item 1", from: "request_item_requests_attributes_0_item_id" | ||
expect(page).not_to have_selector("#request_item_requests_attributes_0_request_unit", visible: true) | ||
end | ||
end | ||
|
||
context "with packs on" do | ||
before(:each) do | ||
Flipper.enable(:enable_packs) | ||
end | ||
|
||
it "should show packs on selection" do | ||
visit new_partners_request_path | ||
expect(Request.count).to eq(0) | ||
expect(page).not_to have_selector("#request_item_requests_attributes_0_request_unit", visible: true) | ||
select "Item 1", from: "request_item_requests_attributes_0_item_id" | ||
expect(page).to have_selector("#request_item_requests_attributes_0_request_unit", visible: true) | ||
expect(page).to have_select("request_item_requests_attributes_0_request_unit", selected: "packs", options: ["Units", "packs"]) | ||
select "packs", from: "request_item_requests_attributes_0_request_unit" | ||
click_on "Add Another Item" | ||
|
||
# get selector to use in subsequent steps | ||
new_item = find_all(:css, "select[data-item-units-target=itemSelect]")[1] | ||
id = new_item[:id].match(/\d+/)[0] | ||
|
||
expect(page).not_to have_selector("request_item_requests_attributes_#{id}_request_unit", visible: true) | ||
select "Item 2", from: "request_item_requests_attributes_#{id}_item_id" | ||
expect(page).not_to have_selector("request_item_requests_attributes_#{id}_request_unit", visible: true) | ||
fill_in "request_item_requests_attributes_0_quantity", with: 50 | ||
fill_in "request_item_requests_attributes_#{id}_quantity", with: 20 | ||
click_on "Submit Essentials Request" | ||
|
||
expect(Request.count).to eq(1) | ||
request = Request.last | ||
expect(request.item_requests[0].quantity).to eq("50") | ||
expect(request.item_requests[0].item_id).to eq(item1.id) | ||
expect(request.item_requests[0].request_unit).to eq("pack") | ||
expect(request.item_requests[1].quantity).to eq("20") | ||
expect(request.item_requests[1].item_id).to eq(item2.id) | ||
expect(request.item_requests[1].request_unit).to eq(nil) | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is identical to the one in
create
please extract it into a private method.