From 2bad833e58909a011c48840a94c495caa5bde616 Mon Sep 17 00:00:00 2001 From: Bert Owen Date: Tue, 8 Jul 2014 17:47:46 +0200 Subject: [PATCH] fix session detail spec, fix session detail approve/cancel rsvp request actions --- .../javascripts/accounts_session_detail.js | 91 +++++++++++++++++-- .../assets/javascripts/accounts_sessions.js | 2 +- web/app/assets/javascripts/jam_rest.js | 2 +- .../clients/_account_session_detail.html.haml | 4 +- web/spec/features/session_detail_spec.rb | 40 +++++++- 5 files changed, 129 insertions(+), 10 deletions(-) diff --git a/web/app/assets/javascripts/accounts_session_detail.js b/web/app/assets/javascripts/accounts_session_detail.js index 627d7609a..2a3bfdaf4 100644 --- a/web/app/assets/javascripts/accounts_session_detail.js +++ b/web/app/assets/javascripts/accounts_session_detail.js @@ -9,6 +9,7 @@ var rest = context.JK.Rest(); var sessionId = null; var sessionData = null; + var rsvpData = null; var $screen = null; var $cancelRsvpBtn = null; var $inviteOthersBtn = null; @@ -46,14 +47,53 @@ context.JK.app.layout.showDialog('rsvp-cancel-dialog'); } + + function buildRsvpRequestActionParams(request_id, action) { + var params = {}; + params.session_id = sessionData.id; + + $.each(rsvpData, function(index, rsvp) { + if (rsvp.id == request_id) { + params.rsvp_responses = []; + + $.each(rsvp.rsvp_requests_rsvp_slots, function(index, rsvp_slot) { + params.rsvp_responses.push({request_slot_id: rsvp_slot.id, accept: action}); + }) + } + }); + + return params; + } + + function reset() { + $sessionDetail.html(""); + } + + function refreshSessionDetail(response) { + reset(); + loadSessionData(); + } + function approveRsvpRequest(e) { e.preventDefault(); var rsvpId = $(e.target).attr('request-id'); + var params = buildRsvpRequestActionParams(rsvpId, true); + + rest.updateRsvpRequest(rsvpId, params) + .done(refreshSessionDetail) + .fail(app.ajaxError); } function declineRsvpRequest(e) { e.preventDefault(); + + var rsvpId = $(e.target).attr('request-id'); + var params = buildRsvpRequestActionParams(rsvpId, false); + + rest.updateRsvpRequest(rsvpId, params) + .done(refreshSessionDetail) + .fail(app.ajaxError); } function events() { @@ -61,17 +101,33 @@ $cancelRsvpBtn.on('click', cancelRsvpRequest); $screen.find(".approveRsvpRequest").on('click', approveRsvpRequest); $screen.find(".declineRsvpRequest").on('click', declineRsvpRequest); + + $screen.find(".cancelSessionRsvp").on('click', function(e) { + e.preventDefault(); + + var rsvpCancelDlg = new context.JK.RsvpCancelDialog(app, sessionData.id, $(this).attr('request-id')); + rsvpCancelDlg.initialize(); + context.JK.app.layout.showDialog('rsvp-cancel-dialog'); + }); } function loadSessionData() { rest.getSessionHistory(sessionId) - .done(renderSession) + .done(function(response) { + sessionData = response; + + rest.getRsvpRequests(sessionId) + .done(function(rsvpResponse) { + rsvpData = rsvpResponse; + + renderSession(); + }) + .fail(app.ajaxError); + }) .fail(app.ajaxError); } - function renderSession(data) { - sessionData = data; - + function renderSession() { var hasPending = false; var isOwner = false; if (sessionData.user_id == context.JK.currentUserId) { @@ -85,7 +141,7 @@ isOwner = true; } else { - $.each(sessionData.pending_rsvp_requests, function(request) { + $.each(sessionData.pending_rsvp_requests, function(index, request) { if (request.user_id == context.JK.currentUserId) { hasPending = true; sessionData.rsvpId = request.id; @@ -98,6 +154,8 @@ isOwner = false; } + sessionData.isOwner = isOwner; + if (isOwner) { $inviteOthersBtn.show(); $cancelRsvpBtn.hide(); @@ -231,11 +289,28 @@ var avatar_url = request.resolved_photo_url; + var request_id = null; + + $.each(rsvpData, function(index, rsvp) { + if (rsvp.user_id == request.id) { + var approved = true; + $.each(rsvp, function(index, rsvp_slot) { + if (rsvp_slot.approved == false) { + approved = false; + } + }); + + if (approved) { + request_id = rsvp.id; + } + } + }); + rsvpHtml = context._.template( $("#template-account-session-rsvp").html(), {id: request.id, avatar_url: avatar_url, user_name: request.name, instruments: instrumentLogoHtml, - latency: latencyHtml}, + latency: latencyHtml, is_owner: sessionData.isOwner, request_id: request_id}, {variable: 'data'} ); @@ -315,6 +390,10 @@ this.beforeShow = beforeShow; this.afterShow = afterShow; + $(document).on("rsvpCancelEvent", function() { + refreshSessionDetail(); + }); + return this; }; diff --git a/web/app/assets/javascripts/accounts_sessions.js b/web/app/assets/javascripts/accounts_sessions.js index ed5e15263..883c2e185 100644 --- a/web/app/assets/javascripts/accounts_sessions.js +++ b/web/app/assets/javascripts/accounts_sessions.js @@ -64,7 +64,7 @@ session.notification_msg = ""; } else { - $.each(session.pending_rsvp_requests, function(request) { + $.each(session.pending_rsvp_requests, function(index, request) { if (request.user_id == context.JK.currentUserId) { hasPending = true; } diff --git a/web/app/assets/javascripts/jam_rest.js b/web/app/assets/javascripts/jam_rest.js index 348a6d535..fbf002612 100644 --- a/web/app/assets/javascripts/jam_rest.js +++ b/web/app/assets/javascripts/jam_rest.js @@ -219,7 +219,7 @@ return $.ajax({ url: '/api/rsvp_requests/' + rsvpRequestId, type: "POST", - data : responses, + data : JSON.stringify(responses), dataType : 'json', contentType: 'application/json' }); diff --git a/web/app/views/clients/_account_session_detail.html.haml b/web/app/views/clients/_account_session_detail.html.haml index c581571a9..2cddd976d 100644 --- a/web/app/views/clients/_account_session_detail.html.haml +++ b/web/app/views/clients/_account_session_detail.html.haml @@ -108,7 +108,9 @@ %td {{data.latency}} .right - %a{href: "#", class: 'button-orange declineRsvpRequest', 'request-id' => "{{data.id}}"} CANCEL + = "{% if (data.is_owner == true) { %}" + %a{href: "#", class: 'button-orange cancelSessionRsvp', 'request-id' => "{{data.request_id}}"} CANCEL + = "{% }; %}" .clearall %script{type: 'text/template', id: 'template-account-session-properties'} diff --git a/web/spec/features/session_detail_spec.rb b/web/spec/features/session_detail_spec.rb index 71b1df6fe..2e8ff5d64 100644 --- a/web/spec/features/session_detail_spec.rb +++ b/web/spec/features/session_detail_spec.rb @@ -18,7 +18,7 @@ describe "Session Detail", :js => true, :type => :feature, :capybara_feature => stub_const("APP_CONFIG", web_config) end - it "view pending requests" do + it "view pending requests on music session creator side" do fast_signin(requested_rsvp_slot.music_session.creator, Nav.session_detail(requested_rsvp_slot.music_session)) find('h2', text: 'RSVPs') @@ -26,4 +26,42 @@ describe "Session Detail", :js => true, :type => :feature, :capybara_feature => find('#pendingRSVPs .rsvp-table td', text: searcher.name) find("#pendingRSVPs .rsvp-table img[data-instrument-id='#{requested_rsvp_slot.instrument_id}']") end + + it "view pending requests on music session searcher side" do + fast_signin(searcher, Nav.session_detail(requested_rsvp_slot.music_session)) + + find('a#cancel-rsvp.button-orange') + find('h2', text: 'RSVPs') + find('div.rsvp-help-notice', text: 'Your RSVP has not been processed by session organizer yet') + end + + it "approve pending requests" do + fast_signin(requested_rsvp_slot.music_session.creator, Nav.session_detail(requested_rsvp_slot.music_session)) + + find('a.approveRsvpRequest[request-id="' + requested_rsvp_slot.rsvp_requests[0].id + '"]').trigger(:click) + find('.session-musicians td', text: searcher.name) + + fast_signin(searcher, Nav.session_detail(requested_rsvp_slot.music_session)) + find('.session-musicians td', text: searcher.name) + end + + it "decline pending requests" do + fast_signin(requested_rsvp_slot.music_session.creator, Nav.session_detail(requested_rsvp_slot.music_session)) + + find('a.declineRsvpRequest[request-id="' + requested_rsvp_slot.rsvp_requests[0].id + '"]').trigger(:click) + + should_not have_selector('td', text: searcher.name) + end + + it "cancel current session rsvp" do + fast_signin(requested_rsvp_slot.music_session.creator, Nav.session_detail(requested_rsvp_slot.music_session)) + find('a.approveRsvpRequest[request-id="' + requested_rsvp_slot.rsvp_requests[0].id + '"]').trigger(:click) + find('.session-musicians td', text: searcher.name) + + find('a.cancelSessionRsvp[request-id="' + requested_rsvp_slot.rsvp_requests[0].id + '"]').trigger(:click) + find('h1', text: 'cancel rsvp') + find('span.session-name', text: requested_rsvp_slot.music_session.name) + find('a#btnCancelRsvp').trigger(:click) + should_not have_selector('td', text: searcher.name) + end end