Skip to content

Commit

Permalink
fix 2 issues in the dialog
Browse files Browse the repository at this point in the history
- when changing the workpackage on an edit form, the `method` was still overridden to `PATCH`
- when the work package field is already populated we cause a redundant request to the backend to refresh the form
  • Loading branch information
klaustopher committed Dec 20, 2024
1 parent 7cd0b13 commit dbb970c
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions frontend/src/stimulus/controllers/dynamic/time-entry.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default class TimeEntryController extends Controller {
declare readonly endTimeInputTarget:HTMLInputElement;
declare readonly hasEndTimeInputTarget:boolean;
declare readonly hoursInputTarget:HTMLInputElement;
declare oldWorkPackageId:string;

private turboRequests:TurboRequestsService;
private pathHelper:PathHelperService;
Expand All @@ -53,6 +54,11 @@ export default class TimeEntryController extends Controller {
const context = await window.OpenProject.getPluginContext();
this.turboRequests = context.services.turboRequests;
this.pathHelper = context.services.pathHelperService;

const workPackageAutocompleter = document.querySelector('opce-autocompleter[data-input-name*="time_entry[work_package_id]"]');
if (workPackageAutocompleter) {
this.oldWorkPackageId = (workPackageAutocompleter as HTMLElement).dataset.inputValue || '';
}
}

userChanged(event:InputEvent) {
Expand All @@ -63,16 +69,25 @@ export default class TimeEntryController extends Controller {
);
}

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,
},
});
workPackageChanged(event:InputEvent) {
const target = event.currentTarget as HTMLInputElement;
const newValue = target.value;

if (this.oldWorkPackageId !== newValue) {
this.oldWorkPackageId = newValue;

const url = this.formTarget.dataset.refreshFormUrl as string;
const csrfToken = document.querySelector<HTMLMetaElement>('meta[name="csrf-token"]')?.content || '';
const formData = new FormData(this.formTarget);
formData.delete('_method'); // remove the override method as this will submit to the wrong action
void this.turboRequests.request(url, {
method: 'post',
body: formData,
headers: {
'X-CSRF-Token': csrfToken,
},
});
}
}

timeInputChanged(event:InputEvent) {
Expand Down

0 comments on commit dbb970c

Please sign in to comment.