Skip to content

Commit

Permalink
replace form when work package has changed
Browse files Browse the repository at this point in the history
  • Loading branch information
klaustopher committed Dec 19, 2024
1 parent 8de11b1 commit 92ab5c9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
25 changes: 13 additions & 12 deletions frontend/src/stimulus/controllers/dynamic/time-entry.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ import { TurboRequestsService } from 'core-app/core/turbo/turbo-requests.service
import { PathHelperService } from 'core-app/core/path-helper/path-helper.service';

export default class TimeEntryController extends Controller {
static targets = ['startTimeInput', 'endTimeInput', 'hoursInput'];
static targets = ['startTimeInput', 'endTimeInput', 'hoursInput', 'form'];

declare readonly formTarget:HTMLFormElement;
declare readonly startTimeInputTarget:HTMLInputElement;
declare readonly hasStartTimeInputTarget:boolean;
declare readonly endTimeInputTarget:HTMLInputElement;
Expand All @@ -62,12 +63,16 @@ export default class TimeEntryController extends Controller {
);
}

workPackageChanged(event:InputEvent) {
const workPackageId = (event.currentTarget as HTMLInputElement).value;
void this.turboRequests.request(
this.pathHelper.timeEntriesWorkPackageActivity(workPackageId),
{ method: 'GET' },
);
workPackageChanged() {
const url = this.formTarget.dataset.refreshFormUrl as string;
const csrfToken = document.querySelector<HTMLMetaElement>('meta[name="csrf-token"]')?.content || '';
void this.turboRequests.request(url, {
method: 'post',
body: new FormData(this.formTarget),
headers: {
'X-CSRF-Token': csrfToken,
},
});
}

timeInputChanged(event:InputEvent) {
Expand All @@ -89,11 +94,7 @@ export default class TimeEntryController extends Controller {
// We calculate the hours field if:
// - We have start & end time and no hours
// - We have start & end time and we have triggered the change from the end time field
if (
startTimeInMinutes
&& endTimeInMinutes
&& (hoursInMinutes === 0 || initiatedBy === this.endTimeInputTarget)
) {
if (startTimeInMinutes && endTimeInMinutes && (hoursInMinutes === 0 || initiatedBy === this.endTimeInputTarget)) {
hoursInMinutes = endTimeInMinutes - startTimeInMinutes;
if (hoursInMinutes <= 0) {
hoursInMinutes += 24 * 60;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ def initialize(time_entry:)
def form_options
base = {
model: time_entry,
data: { turbo: true },
data: {
turbo: true,
"time-entry-target" => "form",
refresh_form_url: refresh_form_time_entries_path
},
id: "time-entry-form"
}

Expand Down
32 changes: 12 additions & 20 deletions modules/costs/app/controllers/time_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TimeEntriesController < ApplicationController
before_action :load_or_build_and_authorize_time_entry
end

authorization_checked! :dialog, :create, :update, :user_tz_caption, :time_entry_activities
authorization_checked! :dialog, :create, :update, :user_tz_caption, :refresh_form

def dialog
if params[:date].present?
Expand All @@ -60,27 +60,19 @@ def user_tz_caption
respond_with_turbo_streams
end

def time_entry_activities
work_package = WorkPackage.visible.find_by(id: params[:work_package_id])

time_entry = TimeEntry.new(project: work_package&.project, work_package: work_package)

form = Primer::Forms::Builder.new(
TimeEntry.model_name.param_key,
time_entry,
TimeEntries::TimeEntryFormComponent.new(time_entry: time_entry),
{
allow_method_names_outside_object: true,
skip_default_ids: false,
data: { turbo: true },
id: "time-entry-form",
method: :post,
builder: Primer::Forms::Builder
}
)
def refresh_form
call = TimeEntries::SetAttributesService.new(
user: current_user,
model: TimeEntry.new,
contract_class: EmptyContract
).call(permitted_params.time_entries)

time_entry = call.result

# TODO Some logic to figure out what fields we want to show (user, etc)

replace_via_turbo_stream(
component: TimeEntries::ActivityForm.new(form)
component: TimeEntries::TimeEntryFormComponent.new(time_entry: time_entry)
)

respond_with_turbo_streams
Expand Down
2 changes: 1 addition & 1 deletion modules/costs/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
resources :time_entries, only: %i[create update] do
get :dialog, on: :collection
get "/users/:user_id/tz_caption", action: :user_tz_caption, on: :collection
get "/work_packages/:work_package_id/time_entry_activities", action: :time_entry_activities, on: :collection
post :refresh_form, on: :collection
end

scope "projects/:project_id", as: "projects" do
Expand Down

0 comments on commit 92ab5c9

Please sign in to comment.