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

Jw debugging cart remove add actions #730

Merged
merged 7 commits into from
Nov 17, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions app/assets/javascripts/cart.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ $(document).ready ->
else if tab == 'complete'
$('.complete-ssrs').removeClass('hidden')
return false

$(document).on 'click', '#modal_place .yes-button', ->
addService($(this).data('srid'), $(this).data('service-id'))

$(document).on 'click', '.add-service', ->
id = $(this).data('id')
Expand All @@ -45,6 +42,8 @@ $(document).ready ->
$('#modal_place').modal('show')
$('#modal_place .yes-button').data('srid', srid)
$('#modal_place .yes-button').data('service-id', id)
$('#modal_place .yes-button').on 'click', (e) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a local handler? I noticed that the problem with the original code was that as you would remove/add more services, the handlers would add up and multiple clicks would be registered when you click the 'Yes' button. @jwiel86 Can you confirm?

If this does work, we should remove the two above lines setting data attributes on the yes button because they won't be needed anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kyle-glick When I was debugging this file, the variables set after $(document).on 'click', '.remove-service', -> were not being reset. So if you deleted one line_item, everything would work fine. However, if you deleted a second one. It would try to execute the code with the previous line item id and then it would also execute the code for the line item you just removed (I think that is what you were describing above- multiple clicks). Changing it to this selector on click format solved the problem for me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure this onClick handler can only run once. Whole modal + .yes-button re-rendered on line 41.

addService(srid, id)
else
addService(srid, id)

Expand All @@ -65,15 +64,15 @@ $(document).ready ->
$('#modal_place').html($('#request-submitted-modal').html())
$('#modal_place').modal('show')

$(document).on 'click', '#modal_place .yes-button', ->
$('#modal_place .yes-button').on 'click', (e) ->
button.replaceWith(spinner)
removeService(srid, id, false, spinner)
else
if editing_ssr == 1 && li_count == 1 # Redirect to the Dashboard if the user deletes the last Service on an SSR
$('#modal_place').html($('#remove-request-modal').html())
$('#modal_place').modal('show')

$(document).on 'click', '#modal_place .yes-button', ->
$('#modal_place .yes-button').on 'click', (e) ->
button.replaceWith(spinner)
removeService(srid, id, true, spinner)
else if li_count == 1 && window.location.pathname.indexOf('catalog') == -1 # Do not allow the user to remove the last service except in the catalog
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/service_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,9 @@ def add_service
# create sub_service_requests
@service_request.reload
@service_request.previous_submitted_at = @service_request.submitted_at

new_line_items.each do |li|
ssr = find_or_create_sub_service_request(li, @service_request)
li.update_attribute(:sub_service_request_id, ssr.id)

if @service_request.status == 'first_draft'
ssr.update_attribute(:status, 'first_draft')
elsif ssr.status.nil? || (ssr.can_be_edited? && ssr_has_changed?(@service_request, ssr) && (ssr.status != 'complete'))
Expand Down Expand Up @@ -332,9 +330,12 @@ def remove_service
end

@service_request.reload

@line_items_count = @sub_service_request ? @sub_service_request.line_items.count : @service_request.line_items.count
@sub_service_requests = @service_request.cart_sub_service_requests

respond_to do |format|
format.js {render layout: false}
end
end

def get_help
Expand Down Expand Up @@ -597,7 +598,7 @@ def send_individual_service_provider_notification(sub_service_request, service_p
line_items = []
@service_request.sub_service_requests.each do |ssr|
if service_provider.identity.is_service_provider?(ssr)
line_items << SubServiceRequest.find(ssr.id).line_items
line_items << ssr.line_items
end
end

Expand Down
14 changes: 7 additions & 7 deletions app/models/sub_service_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ def ctrc?
# Can't edit a request if it's placed in an uneditable status
def can_be_edited?
if organization.has_editable_statuses?
self_or_parent_id = find_editable_id(self.organization.id)
EDITABLE_STATUSES[self_or_parent_id].include?(self.status)
self_or_parent_id = find_editable_id(self.organization.id)
EDITABLE_STATUSES[self_or_parent_id].include?(self.status)
else
true
end
Expand Down Expand Up @@ -473,11 +473,11 @@ def audit_report(identity, start_date, end_date=Time.now.utc)

audit = audits.sort_by(&:created_at).last
# create action
if audit.audited_changes["sub_service_request_id"].nil?
filtered_audit_trail[:line_items] << audit if LineItem.find(audit.auditable_id).sub_service_request_id == self.id
# destroy action
else
filtered_audit_trail[:line_items] << audit if audit.audited_changes["sub_service_request_id"] == self.id
if audit.audited_changes["sub_service_request_id"].nil?
filtered_audit_trail[:line_items] << audit if LineItem.find(audit.auditable_id).sub_service_request_id == self.id
# destroy action
else
filtered_audit_trail[:line_items] << audit if audit.audited_changes["sub_service_request_id"] == self.id
end
end
filtered_audit_trail[:sub_service_request_id] = self.id
Expand Down
3 changes: 2 additions & 1 deletion app/views/catalogs/_catalog_right.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
-# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.col-md-3.catalog-right
= render 'service_requests/right_navigation/cart', service_request: service_request, sub_service_request: sub_service_request, sub_service_requests: sub_service_requests, line_items_count: line_items_count, allow_delete: true
.shopping-cart
= render 'service_requests/right_navigation/cart', service_request: service_request, sub_service_request: sub_service_request, sub_service_requests: sub_service_requests, line_items_count: line_items_count, allow_delete: true
.nav-buttons
= link_to t(:actions)[:continue], protocol_service_request_path(service_request), class: 'submit-request-button btn btn-success btn-lg btn-block'
= render 'service_requests/right_navigation/right_navigation'
2 changes: 1 addition & 1 deletion app/views/service_requests/add_service.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
$('.cart').replaceWith("<%= escape_javascript(render( 'service_requests/right_navigation/cart', service_request: @service_request, sub_service_request: @sub_service_request, sub_service_requests: @sub_service_requests, line_items_count: @line_items_count, allow_delete: true )) %>")
$('.shopping-cart').html("<%= escape_javascript(render( 'service_requests/right_navigation/cart', service_request: @service_request, sub_service_request: @sub_service_request, sub_service_requests: @sub_service_requests, line_items_count: @line_items_count, allow_delete: true )) %>")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 change: 1 addition & 0 deletions app/views/service_requests/protocol.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
= javascript_include_tag 'protocol'

= render '/service_requests/navigation/steps', service_request: @service_request, step: @step_text, css_class: @css_class
= render 'service_requests/modals/request_submitted_modal', service_request: @service_request
.col-sm-12.body-container#protocols-page
= form_tag navigate_service_request_path(@service_request), id: 'service-request-form' do
= render 'shared/modal_errors', errors: @errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
-# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.col-md-3.protocol-right
= render 'service_requests/right_navigation/cart', service_request: service_request, sub_service_request: sub_service_request, sub_service_requests: sub_service_requests, line_items_count: line_items_count, allow_delete: true
.shopping-cart
= render 'service_requests/right_navigation/cart', service_request: service_request, sub_service_request: sub_service_request, sub_service_requests: sub_service_requests, line_items_count: line_items_count, allow_delete: true
.nav-buttons
= link_to t(:proper)[:cart][:to_catalog], catalog_service_request_path(service_request), class: 'submit-request-button btn btn-warning btn-lg btn-block'
= render 'service_requests/right_navigation/right_navigation'
2 changes: 1 addition & 1 deletion app/views/service_requests/remove_service.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
$('.cart').replaceWith("<%= escape_javascript(render( 'service_requests/right_navigation/cart', service_request: @service_request, sub_service_request: @sub_service_request, sub_service_requests: @sub_service_requests, line_items_count: @line_items_count, allow_delete: true )) %>")
$('.shopping-cart').html("<%= escape_javascript(render( 'service_requests/right_navigation/cart', service_request: @service_request, sub_service_request: @sub_service_request, sub_service_requests: @sub_service_requests, line_items_count: @line_items_count, allow_delete: true )) %>")

if "<%= @page %>" == 'protocol'
$('.service-list').html("<%= escape_javascript(render( 'service_requests/protocol/service_list', service_request: @service_request, sub_service_request: @sub_service_request )) %>")
1 change: 1 addition & 0 deletions app/views/service_requests/service_details.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
= javascript_include_tag 'service_details'

= render '/service_requests/navigation/steps', service_request: @service_request, step: @step_text, css_class: @css_class
= render 'service_requests/modals/request_submitted_modal', service_request: @service_request
.col-sm-12.body-container#service-details-page
= form_tag navigate_service_request_path(@service_request), id: 'service-request-form' do
= render 'shared/modal_errors', errors: @errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
-# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.col-md-3.service-details-right
= render 'service_requests/right_navigation/cart', service_request: service_request, sub_service_request: sub_service_request, sub_service_requests: sub_service_requests, line_items_count: line_items_count, allow_delete: true
.shopping-cart
= render 'service_requests/right_navigation/cart', service_request: service_request, sub_service_request: sub_service_request, sub_service_requests: sub_service_requests, line_items_count: line_items_count, allow_delete: true
.nav-buttons
= link_to t(:proper)[:cart][:to_catalog], catalog_service_request_path(service_request), class: 'submit-request-button btn btn-warning btn-lg btn-block'
= render 'service_requests/right_navigation/right_navigation'
3 changes: 1 addition & 2 deletions spec/controllers/service_requests/post_add_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
service = create(:service, organization: org)
protocol = create(:protocol_without_validations, primary_pi: logged_in_user)
sr = create(:service_request_without_validations, protocol: protocol)
ssr = create(:sub_service_request_without_validations, organization: org, service_request: sr, status: 'on_hold')
ssr = create(:sub_service_request_without_validations, organization: org, service_request: sr)

session[:service_request_id] = sr.id
session[:identity_id] = logged_in_user.id
Expand All @@ -220,7 +220,6 @@
id: sr.id,
service_id: service.id
}

expect(sr.sub_service_requests.first.status).to eq('draft')
end

Expand Down