diff --git a/ruby/lib/jam_ruby/models/band.rb b/ruby/lib/jam_ruby/models/band.rb index 50f18486c..5f47cdb9d 100644 --- a/ruby/lib/jam_ruby/models/band.rb +++ b/ruby/lib/jam_ruby/models/band.rb @@ -65,6 +65,12 @@ module JamRuby @musicians = User.joins(:band_musicians).where(:bands_musicians => {:band_id => "#{band_id}"}) end + def self.pending_musicians(band_id) + @musicians = User.joins(:received_band_invitations) + .where(:band_invitations => {:band_id => "#{band_id}"}) + .where(:band_invitations => {:accepted => nil}) + end + def self.recording_index(current_user, band_id) hide_private = false band = Band.find(band_id) diff --git a/web/app/assets/javascripts/bandProfile.js b/web/app/assets/javascripts/bandProfile.js index fd7938c3f..2097e7f4c 100644 --- a/web/app/assets/javascripts/bandProfile.js +++ b/web/app/assets/javascripts/bandProfile.js @@ -6,11 +6,13 @@ context.JK.BandProfileScreen = function(app) { var logger = context.JK.logger; var bandId; + var isMember = false; var band = {}; var instrument_logo_map = context.JK.getInstrumentIconMap24(); function beforeShow(data) { bandId = data.id; + setIsMember(); } function afterShow(data) { @@ -337,53 +339,76 @@ async: false, processData:false, success: function(response) { - $.each(response, function(index, val) { - var instrumentLogoHtml = ''; - var musician = val; - if ("instruments" in musician) { - for (var j=0; j < musician.instruments.length; j++) { - var instrument = musician.instruments[j]; - var inst = '../assets/content/icon_instrument_default24.png'; - if (instrument.instrument_id in instrument_logo_map) { - inst = instrument_logo_map[instrument.instrument_id]; - } - instrumentLogoHtml += ' '; - } - } + bindMusicians(response); + }, + error: app.ajaxError + }); - var template = $('#template-band-profile-members').html(); - var memberHtml = context.JK.fillTemplate(template, { - userId: musician.id, - profile_url: "/#/profile/" + musician.id, - avatar_url: context.JK.resolveAvatarUrl(musician.photo_url), - name: musician.name, - location: musician.location, - biography: musician.biography, - friend_count: musician.friend_count, - follower_count: musician.follower_count, - recording_count: musician.recording_count, - session_count: musician.session_count, - instruments: instrumentLogoHtml - }); + if (isMember) { + bindPendingMembers(); + } + } - $('#band-profile-members').append(memberHtml); - - // wire up Follow button click handler - var following = isFollowingMember(musician.id); - configureMemberFollowingButton(following, musician.id); - - // TODO: wire up Friend button click handler - // var friend = isFriend(musician.id); - // configureMemberFriendButton(friend, musician.id); - }); + function bindPendingMembers() { + $("#band-profile-members").append("

Pending Band Invitations

"); + var url = "/api/bands/" + bandId + "/musicians?pending=true"; + $.ajax({ + type: "GET", + dataType: "json", + url: url, + async: false, + processData:false, + success: function(response) { + bindMusicians(response); }, error: app.ajaxError }); } + function bindMusicians(musicians) { + $.each(musicians, function(index, musician) { + var instrumentLogoHtml = ''; + if ("instruments" in musician) { + for (var j=0; j < musician.instruments.length; j++) { + var instrument = musician.instruments[j]; + var inst = '../assets/content/icon_instrument_default24.png'; + if (instrument.instrument_id in instrument_logo_map) { + inst = instrument_logo_map[instrument.instrument_id]; + } + instrumentLogoHtml += ' '; + } + } + + var template = $('#template-band-profile-members').html(); + var memberHtml = context.JK.fillTemplate(template, { + userId: musician.id, + profile_url: "/#/profile/" + musician.id, + avatar_url: context.JK.resolveAvatarUrl(musician.photo_url), + name: musician.name, + location: musician.location, + biography: musician.biography, + friend_count: musician.friend_count, + follower_count: musician.follower_count, + recording_count: musician.recording_count, + session_count: musician.session_count, + instruments: instrumentLogoHtml + }); + + $('#band-profile-members').append(memberHtml); + + // wire up Follow button click handler + var following = isFollowingMember(musician.id); + configureMemberFollowingButton(following, musician.id); + + // TODO: wire up Friend button click handler + // var friend = isFriend(musician.id); + // configureMemberFriendButton(friend, musician.id); + }); + } + // TODO: refactor - function isMember() { - var isMember = false; + // checks if person viewing the profile is also a band member + function setIsMember() { var url = "/api/bands/" + bandId + "/musicians"; $.ajax({ type: "GET", @@ -400,8 +425,6 @@ }, error: app.ajaxError }); - - return isMember; } // events for main screen @@ -416,8 +439,7 @@ var following = isFollowing(); configureBandFollowingButton(following); - var member = isMember(); - if (member) { + if (isMember) { $("#btn-follow-band").hide(); $("#btn-edit-band-members").show(); } diff --git a/web/app/controllers/api_bands_controller.rb b/web/app/controllers/api_bands_controller.rb index 954fc0d59..b3ca1e623 100644 --- a/web/app/controllers/api_bands_controller.rb +++ b/web/app/controllers/api_bands_controller.rb @@ -48,11 +48,15 @@ class ApiBandsController < ApiController end def musician_index - unless params[:id].blank? - @musicians = Band.musician_index(params[:id]) - + if !params[:pending].blank? + @musicians = Band.pending_musicians(params[:id]) else - render :json => { :message => "Band ID is required." }, :status => 400 + unless params[:id].blank? + @musicians = Band.musician_index(params[:id]) + + else + render :json => { :message => "Band ID is required." }, :status => 400 + end end end diff --git a/web/config/routes.rb b/web/config/routes.rb index 53b8ed58d..053a57c3e 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -204,7 +204,7 @@ SampleApp::Application.routes.draw do match '/bands' => 'api_bands#create', :via => :post match '/bands/:id' => 'api_bands#update', :via => :post - # band members (NOT DONE) + # band members match '/bands/:id/musicians' => 'api_bands#musician_index', :via => :get match '/bands/:id/musicians' => 'api_bands#musician_create', :via => :post match '/bands/:id/musicians/:user_id' => 'api_bands#musician_destroy', :via => :delete