diff --git a/app/controllers/admin/shifts_controller.rb b/app/controllers/admin/shifts_controller.rb index c0d893d69..4cfaa496c 100644 --- a/app/controllers/admin/shifts_controller.rb +++ b/app/controllers/admin/shifts_controller.rb @@ -10,6 +10,7 @@ class Admin::ShiftsController < AdminAreaController def index @staff = User + .staff .where(id: StaffSpace.where(space_id: @space_id).pluck(:user_id)) .order("LOWER(name) ASC") .pluck(:name, :id) @@ -20,14 +21,13 @@ def index StaffSpace .joins(:user) .where(space_id: @space_id) + .merge( + User.staff + ) # NOTE why isn't this the default scope? Ask Alex *again* sometime later lol .order("users.name") .each do |staff| if !staff.nil? && !staff.user.nil? - @colors << { - id: staff.user.id, - name: staff.user.name, - color: staff.color - } + @colors << { user: staff.user, color: staff.color } end end end @@ -35,6 +35,7 @@ def index def shifts @staff = User + .staff .where(id: StaffSpace.where(space_id: @space_id).pluck(:user_id)) .order("LOWER(name) ASC") .pluck(:name, :id) @@ -44,6 +45,7 @@ def shifts StaffSpace .joins(:user) .where(space_id: @space_id) + .merge(User.staff) .order("users.name") .each do |staff| if !staff.nil? && !staff.user.nil? && !staff.user_id.nil? diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index b5ed3fa3b..19847e44f 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -215,35 +215,40 @@ def toggle_lock_user # user_ids: [array of ids], role: role # for space management: # Takes a hash of { user_id: [space_id array]} - def set_role # Update user roles + # All user ids sent in this query get set to the same role # Keep staff spaces attached, even if demoted - params["user_ids"]&.each do |user_id| - user = User.find(user_id) - user.role = params[:role] - user.save + if params[:role].present? && params[:user_ids].is_a?(Array) + params[:user_ids].each do |user_id| + user = User.find(user_id) + user.role = params[:role] + user.save + end end # Update user staff spaces - params["spaces"]&.each do |user_id, spaces| - user = User.find(user_id) - if user.present? && user.staff? - space_list = spaces.present? ? spaces : [] + if params[:spaces].present? + params["spaces"]&.each do |user_id, spaces| + user = User.find(user_id) + if user.present? && user.staff? + space_list = spaces.present? ? spaces : [] - space_list.each do |space| - StaffSpace.find_or_create_by(space_id: space, user: user) - end + space_list.each do |space| + StaffSpace.find_or_create_by(space_id: space, user: user) + end - user.staff_spaces.where.not(space_id: space_list).destroy_all + user.staff_spaces.where.not(space_id: space_list).destroy_all - flash[:notice] = "Successfully changed spaces for the user." + flash[:notice] = "Successfully changed spaces for the user." + end end end # response is js respond_to do |format| format.html { redirect_back(fallback_location: root_path) } + format.json { head :ok } format.js { render layout: false } format.all { redirect_back(fallback_location: root_path) } end diff --git a/app/javascript/entrypoints/admin_availability_calendar.js b/app/javascript/entrypoints/admin_availability_calendar.js index 5a7e363bf..2afc48520 100644 --- a/app/javascript/entrypoints/admin_availability_calendar.js +++ b/app/javascript/entrypoints/admin_availability_calendar.js @@ -530,7 +530,7 @@ saveButton.addEventListener("click", () => createCalendarEvent()); const openModal = (arg) => { // Reset defaults modalTitle.innerText = "New Unavailability"; - recurringInput.checked = false; + recurringInput.checked = true; wholeDayCheckbox.checked = false; // Reset checkbox state unavailabilityId.value = ""; switchInputVisibility(recurringInput.checked); diff --git a/app/javascript/entrypoints/users.js b/app/javascript/entrypoints/users.js index 859030837..2ca819d25 100644 --- a/app/javascript/entrypoints/users.js +++ b/app/javascript/entrypoints/users.js @@ -126,4 +126,28 @@ document.addEventListener("turbo:load", function () { }, }); }); + + document.querySelectorAll("input.set-user-space-button").forEach((i) => { + i.addEventListener("change", (event) => { + // Get total space list + let space_list = [ + ...i.parentElement.querySelectorAll("input.set-user-space-button"), + ].reduce(function (result, element) { + if (element.checked) { + result.push(element.dataset.spaceId); + } + return result; + }, []); + let body = { user_ids: [i.dataset.userId], spaces: {} }; + body["spaces"][i.dataset.userId] = space_list; + fetch("/admin/users/set_role/", { + method: "PATCH", + headers: { + Accept: "application/json", + "Content-Type": "application/json", + }, + body: JSON.stringify(body), + }); + }); + }); }); diff --git a/app/views/admin/shifts/index.html.erb b/app/views/admin/shifts/index.html.erb index 6c091d25d..7e2f53e89 100644 --- a/app/views/admin/shifts/index.html.erb +++ b/app/views/admin/shifts/index.html.erb @@ -20,7 +20,7 @@ <% @colors.each do |user| %>