diff --git a/app/assets/javascripts/controllers/headerCtrl.js b/app/assets/javascripts/controllers/headerCtrl.js index 8624a16c1..f4e910da1 100644 --- a/app/assets/javascripts/controllers/headerCtrl.js +++ b/app/assets/javascripts/controllers/headerCtrl.js @@ -7,30 +7,44 @@ angular.module('QuepidApp') '$rootScope', '$scope', '$route', - '$uibModal', - 'caseSvc', 'caseTryNavSvc', + 'caseSvc', + 'bookSvc', + 'caseTryNavSvc', function( $rootScope, $scope, $route, - $uibModal, caseSvc, + bookSvc, caseTryNavSvc ) { $rootScope.isRailsGoingToAngular = isRailsGoingToAngular; $scope.headerScope = {}; $scope.headerScope.dropdownCases = []; + $scope.headerScope.dropdownBooks = []; $scope.headerScope.casesCount = 0; + $scope.headerScope.booksCount = 0; + $scope.theCase = null; $scope.headerScope.goToCase = goToCase; + $scope.headerScope.createBookLink = createBookLink; - angular.forEach(['fetchedDropdownCasesList'], function (eventName) { - $scope.$on(eventName, function() { - $scope.headerScope.dropdownCases = caseSvc.dropdownCases; - $scope.headerScope.casesCount = caseSvc.casesCount; - }); + + $scope.$on('fetchedDropdownCasesList', function() { + $scope.headerScope.dropdownCases = caseSvc.dropdownCases; + $scope.headerScope.casesCount = caseSvc.casesCount; + }); + + $scope.$on('fetchedDropdownBooksList', function() { + $scope.headerScope.dropdownBooks = bookSvc.dropdownBooks; + $scope.headerScope.booksCount = bookSvc.booksCount; }); + + $scope.$on('associateBook', function() { + bookSvc.fetchDropdownBooks(); + }); + angular.forEach(['updatedCasesList', 'caseRenamed'], function (eventName) { $scope.$on(eventName, function() { @@ -40,14 +54,28 @@ angular.module('QuepidApp') // Necessary when the first page isn't the main case page caseSvc.fetchDropdownCases(); + bookSvc.fetchDropdownBooks(); function goToCase($event, aCase) { $event.preventDefault(); caseTryNavSvc.navigateTo({'caseNo': aCase.caseNo, 'tryNo': aCase.lastTry}); - } + } function isRailsGoingToAngular() { return !( angular.isDefined($route.current) && angular.isDefined($route.current.$$route) ); } + + $scope.$on('caseSelected', function() { + $scope.theCase = caseSvc.getSelectedCase(); + }); + + function createBookLink() { + if ($scope.theCase){ + const teamIds = $scope.theCase.teams.map(function(team) { + return `&team_ids[]=${team.id}`; + }); + return `books/new?book[scorer_id]=${$scope.theCase.scorerId}${teamIds}&origin_case_id=${$scope.theCase.caseNo}`; + } + } } ]); diff --git a/app/assets/javascripts/services/bookSvc.js b/app/assets/javascripts/services/bookSvc.js index 849c13a42..775b4889f 100644 --- a/app/assets/javascripts/services/bookSvc.js +++ b/app/assets/javascripts/services/bookSvc.js @@ -7,7 +7,8 @@ angular.module('QuepidApp') '$http', 'broadcastSvc', function bookSvc($http, broadcastSvc) { - this.books = []; + this.books = []; + this.dropdownBooks = []; var Book = function(id, name) { this.id = id; @@ -132,5 +133,23 @@ angular.module('QuepidApp') console.log('refreshed ratings' + response.data); }); }; + + this.fetchDropdownBooks = function() { + this.dropdownBooks.length = 0; + return $http.get('api/dropdown/books') + .then(function(response) { + this.booksCount = response.data.books_count; + + angular.forEach(response.data.books, function(dataBook) { + let book = this.constructFromData(dataBook); + + if(!contains(this.dropdownBooks, book)) { + this.dropdownBooks.push(book); + } + }); + + broadcastSvc.send('fetchedDropdownBooksList', this.dropdownBooks); + }); + }; } ]); diff --git a/app/assets/javascripts/services/caseSvc.js b/app/assets/javascripts/services/caseSvc.js index d170a4fab..0e4ac19b0 100644 --- a/app/assets/javascripts/services/caseSvc.js +++ b/app/assets/javascripts/services/caseSvc.js @@ -461,6 +461,7 @@ angular.module('QuepidApp') theCase.bookId = bookId; theCase.bookName = response.book_name; + broadcastSvc.send('associateBook', svc.dropdownBooks); }, function() { caseTryNavSvc.notFound(); }); diff --git a/app/controllers/api/v1/books/dropdown_controller.rb b/app/controllers/api/v1/books/dropdown_controller.rb new file mode 100644 index 000000000..747ceddad --- /dev/null +++ b/app/controllers/api/v1/books/dropdown_controller.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Api + module V1 + module Books + class DropdownController < Api::ApiController + def index + @books = recent_books 3 + + respond_with @books + end + end + end + end +end diff --git a/app/controllers/api/v1/cases_controller.rb b/app/controllers/api/v1/cases_controller.rb index fbbccb7cb..d8c6bf4e8 100644 --- a/app/controllers/api/v1/cases_controller.rb +++ b/app/controllers/api/v1/cases_controller.rb @@ -96,6 +96,10 @@ def update Analytics::Tracker.track_case_archived_event current_user, @case respond_with @case elsif @case.update update_params + if update_params[:book_id] + @book = Book.find(update_params[:book_id]) + TrackBookViewedJob.perform_now @book, current_user + end Analytics::Tracker.track_case_updated_event current_user, @case respond_with @case else diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 56349ba7a..bd7465ff8 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -15,6 +15,7 @@ class ApplicationController < ActionController::Base before_action :require_login before_action :check_current_user_locked! before_action :set_recent_cases + before_action :set_recent_books before_action :check_for_announcement # Prevent CSRF attacks by raising an exception. diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index a872fc15c..efdef3315 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -60,6 +60,9 @@ def new else Book.new end + + @origin_case = current_user.cases_involved_with.where(id: params[:origin_case_id]).first if params[:origin_case_id] + respond_with(@book) end @@ -70,6 +73,13 @@ def create @book = Book.new(book_params) @book.owner = current_user if @book.save + + if params[:book][:link_the_case] + @origin_case = current_user.cases_involved_with.where(id: params[:book][:origin_case_id]).first + @origin_case.book = @book + @origin_case.save + end + redirect_to @book, notice: 'Book was successfully created.' else render :new @@ -89,7 +99,7 @@ def update @book.teams.replace(teams) - @book.update(book_params.except(:team_ids)) + @book.update(book_params.except(:team_ids, :link_the_case, :origin_case_id)) respond_with(@book) end @@ -260,13 +270,13 @@ def find_user def book_params params_to_use = params.require(:book).permit(:scorer_id, :selection_strategy_id, :name, - :support_implicit_judgements, + :support_implicit_judgements, :link_the_case, :origin_case_id, :show_rank, team_ids: []) # Crafting a book[team_ids] parameter from the AngularJS side didn't work, so using top level parameter params_to_use[:team_ids] = params[:team_ids] if params[:team_ids] params_to_use[:team_ids]&.compact_blank! - params_to_use + params_to_use.except(:link_the_case, :origin_case_id) end end diff --git a/app/controllers/concerns/authentication/current_book_manager.rb b/app/controllers/concerns/authentication/current_book_manager.rb index d03d500b7..fefecf34f 100644 --- a/app/controllers/concerns/authentication/current_book_manager.rb +++ b/app/controllers/concerns/authentication/current_book_manager.rb @@ -15,6 +15,10 @@ def check_book render json: { message: 'Book not found!' }, status: :not_found unless @book end + def set_recent_books + @recent_books = recent_books(3) + end + # rubocop:disable Metrics/MethodLength def recent_books count if current_user diff --git a/app/views/api/v1/books/dropdown/index.json.jbuilder b/app/views/api/v1/books/dropdown/index.json.jbuilder new file mode 100644 index 000000000..44523c7cb --- /dev/null +++ b/app/views/api/v1/books/dropdown/index.json.jbuilder @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +json.books do + json.array! @books do |book| + json.name book.name + json.id book.id + end +end + +json.books_count current_user.books_involved_with.count diff --git a/app/views/books/_form.html.erb b/app/views/books/_form.html.erb index 3f8f4743c..9d49595a9 100644 --- a/app/views/books/_form.html.erb +++ b/app/views/books/_form.html.erb @@ -28,6 +28,18 @@ <% end %>