From 317aa08365bbb1a4b8c525b015fcd36709abe54e Mon Sep 17 00:00:00 2001 From: Jens Kraemer Date: Tue, 5 Oct 2021 04:30:31 +0800 Subject: [PATCH] time entry form fixes - issue selection selects the project accordingly - issue / project selection updates available activities - move project / issue selector to top. more logical this way, since making a selection there may change the available activities --- app/controllers/stopwatch_timers_controller.rb | 12 +++++++++++- app/views/stopwatch_timers/_entry_form.html.erb | 9 ++++----- app/views/stopwatch_timers/update_form.js.erb | 5 +++++ config/routes.rb | 1 + 4 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 app/views/stopwatch_timers/update_form.js.erb diff --git a/app/controllers/stopwatch_timers_controller.rb b/app/controllers/stopwatch_timers_controller.rb index 8337cdc..145793e 100644 --- a/app/controllers/stopwatch_timers_controller.rb +++ b/app/controllers/stopwatch_timers_controller.rb @@ -73,6 +73,17 @@ class StopwatchTimersController < ApplicationController render json: @timer.to_json end + def update_form + if id = params[:time_entry_id].presence + @time_entry = TimeEntry.visible.find id + else + @time_entry = TimeEntry.new + end + @time_entry.safe_attributes = params[:time_entry] + rescue ActiveRecord::RecordNotFound + head 404 + end + private def find_timer @@ -82,7 +93,6 @@ class StopwatchTimersController < ApplicationController def find_time_entry @time_entry = time_entries.find params[:id] - end def load_todays_entries diff --git a/app/views/stopwatch_timers/_entry_form.html.erb b/app/views/stopwatch_timers/_entry_form.html.erb index 653fd0c..3fed802 100644 --- a/app/views/stopwatch_timers/_entry_form.html.erb +++ b/app/views/stopwatch_timers/_entry_form.html.erb @@ -2,16 +2,15 @@
<%= t @time_entry.new_record? ? '.legend_new' : '.legend_edit' %> -

<%= f.select :activity_id, activity_collection_for_select_options(@time_entry), :required => true %>

- -

<%= f.select :project_id, project_tree_options_for_select(Project.allowed_to(:log_time).to_a, :selected => @time_entry.project, :include_blank => true), :required => true %>

+

<%= f.select :project_id, project_tree_options_for_select(Project.allowed_to(:log_time).to_a, :selected => @time_entry.project, :include_blank => true), { no_label: true }, id: 'stopwatch_time_entry_project_id' %>

<%= f.text_field :issue_id, :size => 6, :required => Setting.timelog_required_fields.include?('issue_id') %> - + <%= link_to_issue(@time_entry.issue) if @time_entry.issue.try(:visible?) %>

+

<%= f.select :activity_id, activity_collection_for_select_options(@time_entry), { no_label: true }, id: 'stopwatch_time_entry_activity_id' %>

@@ -30,7 +29,7 @@ }); $('#time_entry_project_id, #time_entry_issue_id').change(function(){ $.ajax({ - url: '<%= escape_javascript(@time_entry.new_record? ? new_time_entry_path(format: 'js') : edit_time_entry_path(@time_entry, format: 'js')) %>', + url: '<%= j update_form_stopwatch_timers_path(time_entry_id: @time_entry.id, format: 'js') %>', type: 'post', data: $(this).closest('form').serialize() }); diff --git a/app/views/stopwatch_timers/update_form.js.erb b/app/views/stopwatch_timers/update_form.js.erb new file mode 100644 index 0000000..2cb2749 --- /dev/null +++ b/app/views/stopwatch_timers/update_form.js.erb @@ -0,0 +1,5 @@ +$('#stopwatch_time_entry_activity_id').html('<%= escape_javascript options_for_select(activity_collection_for_select_options(@time_entry), @time_entry.activity_id) %>'); +$('#stopwatch_time_entry_issue').html('<%= escape_javascript link_to_issue(@time_entry.issue) if @time_entry.issue.try(:visible?) %>'); +$('#stopwatch_time_entry_project_id').html('<%= escape_javascript project_tree_options_for_select(Project.allowed_to(:log_time).to_a, :selected => @time_entry.project, :include_blank => true) %>'); + + diff --git a/config/routes.rb b/config/routes.rb index 814cb6f..5aad544 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,7 @@ resources :stopwatch_timers, only: %i(new create edit update) do collection do get :current + post :update_form end member do put :start