diff --git a/app/assets/javascripts/requests.js b/app/assets/javascripts/requests.js index 86e4451..43cf55b 100644 --- a/app/assets/javascripts/requests.js +++ b/app/assets/javascripts/requests.js @@ -3,24 +3,59 @@ var COURSE; $(document).ready(function() { - $(".click_redirect").click(function() { - var path = $(this).data("redirect-path"); - location.href=path; - }); + let isSelectionMade = false; $('#requester_name').autocomplete({ - source: $('#requester_name').data('autocomplete-source'), - appendTo: "#change_owner_form .modal-body", - focus: function(event, ui) { - event.preventDefault(); - $(this).val(ui.item.label); - }, - select: function(event, ui) { - event.preventDefault(); - $(this).val(ui.item.label); - $("#requester_id").val(ui.item.value); - } + source: $('#requester_name').data('autocomplete-source'), + appendTo: "#change_owner_form .modal-body", + focus: function(event, ui) { + event.preventDefault(); + $(this).val(ui.item.label); + }, + select: function(event, ui) { + event.preventDefault(); + $(this).val(ui.item.label); + $("#requester_id").val(ui.item.value); + isSelectionMade = true; + enableSubmitButton(); + disableTyping(); + } + }); + + $('#requester_name').on('input change', function() { + if (isSelectionMade) { + enableSubmitButton(); + } + }); + + function disableTyping() { + $('#requester_name').attr('readonly', true); + } + + function enableTyping() { + $('#requester_name').attr('readonly', false); + } + + function enableSubmitButton() { + const requesterIdValue = $('#requester_id').val().trim(); + if (requesterIdValue !== "") { + $('#change_owner_submit').prop('disabled', false); + } else { + $('#change_owner_submit').prop('disabled', true); + } + } + + $('#requester_name').on('keyup', function() { + if (isSelectionMade) { + $(this).val(''); + $("#requester_id").val(''); + isSelectionMade = false; + enableSubmitButton(); + enableTyping(); + } }); + + $("#request_course_attributes_term, #request_course_attributes_year").on("input_load change", function(e) { diff --git a/app/views/requests/_change_owner.html.erb b/app/views/requests/_change_owner.html.erb index d4609a7..1393828 100644 --- a/app/views/requests/_change_owner.html.erb +++ b/app/views/requests/_change_owner.html.erb @@ -9,12 +9,14 @@ <%= simple_form_for @request, url: change_owner_request_path do |f| %> <% end %> diff --git a/app/views/requests/_form_fields.html.erb b/app/views/requests/_form_fields.html.erb index f120372..cc6bed1 100644 --- a/app/views/requests/_form_fields.html.erb +++ b/app/views/requests/_form_fields.html.erb @@ -86,12 +86,12 @@ <%= f.input :reserve_location_id, collection: Location.active, required: true, hint: "Where do you want students to pick up your items?" %>
- <%= f.input :reserve_start_date, as: :string, readonly: ! current_user.admin?, required: false, hint: "Prefilled after term selection.", - input_html: { id: "request_reserve_start_date", class: current_user.admin? ? "datepicker" : "" } %> + <%= f.input :reserve_start_date, as: :string, readonly: true, required: false, hint: "Prefilled after term selection.", + input_html: { id: "request_reserve_start_date", class: "" } %>
- <%= f.input :reserve_end_date, as: :string, readonly: ! current_user.admin?, required: false, - input_html: { id: "request_reserve_end_date", class: current_user.admin? ? "datepicker" : "" } %> + <%= f.input :reserve_end_date, as: :string, readonly: true, required: false, + input_html: { id: "request_reserve_end_date", class: "" } %>