Skip to content

Commit

Permalink
#4398: Support units in requests
Browse files Browse the repository at this point in the history
  • Loading branch information
dorner committed Jul 12, 2024
1 parent fb505f7 commit 704ad63
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
6 changes: 5 additions & 1 deletion app/controllers/partners/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def new
@partner_request.item_requests.build

@requestable_items = PartnerFetchRequestableItemsService.new(partner_id: current_partner.id).call
# hash of (item ID => hash of (request unit name => request unit plural name))
@item_units = current_partner.organization.items.to_h do |i|
[i.id, i.request_units.to_h { |u| [u.name, u.name.pluralize] }]
end
end

def show
Expand Down Expand Up @@ -44,7 +48,7 @@ def create
private

def partner_request_params
params.require(:request).permit(:comments, item_requests_attributes: [:item_id, :quantity])
params.require(:request).permit(:comments, item_requests_attributes: [:item_id, :quantity, :request_unit])
end
end
end
40 changes: 40 additions & 0 deletions app/javascript/controllers/item_units_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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])
}
}

itemSelected() {
if (!this.hasRequestSelectTarget) {
return;
}
let option = this.itemSelectTarget.options[this.itemSelectTarget.selectedIndex]
let units = this.itemUnitsValue[option.value]
this.clearOptions()
this.addOption('', 'Units')
for (const [index, [name, displayName]] of Object.entries(Object.entries(units))) {
this.addOption(name, displayName, index === "0")
}
}

}
1 change: 1 addition & 0 deletions app/services/partners/request_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def populate_item_request(partner_request)
else
items[input_item['item_id']] = Partners::ItemRequest.new(
item_id: input_item['item_id'],
request_unit: input_item['request_unit'],
quantity: input_item['quantity'],
children: input_item['children'] || [], # will create ChildItemRequests if there are any
name: fetch_organization_item_name(input_item['item_id']),
Expand Down
16 changes: 14 additions & 2 deletions app/views/partners/requests/_item_request.html.erb
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'},
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 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>
Expand Down
7 changes: 5 additions & 2 deletions app/views/partners/requests/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@
<tr>
<th>Item Requested</th>
<th>Quantity</th>
<% if current_partner.organization.request_units.any? %>
<th>Units (if applicable)</th>
<% end %>
</tr>
</thead>
<tbody class='fields'>
<%= render 'item_request', form: form %>
<%= render partial: 'item_request', locals: { form: form, item_units: @item_units } %>
</tbody>
</table>
<div>
<%= add_element_button('Add Another Item', container_selector: '.fields') do %>
<%= render 'item_request', form: form, object: @partner_request.item_requests.build %>
<%= render partial: 'item_request', locals: { form: form, item_units: @item_units }, object: @partner_request.item_requests.build %>
<% end %>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class BackfillPartnerChildRequestedItems < ActiveRecord::Migration[7.1]
def change
return unless Rails.env.production?

Partners::Child.unscoped.where.not(item_needed_diaperid: nil).each do |child|
child.requested_items << Item.find_by(id: child.item_needed_diaperid)
end
Expand Down

0 comments on commit 704ad63

Please sign in to comment.