Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Handle ongoing timer in new modal

Klaus Zanders09d6f537f680
diff --git a/frontend/src/app/features/work-packages/components/wp-timer-button/wp-timer-button.component.ts b/frontend/src/app/features/work-packages/components/wp-timer-button/wp-timer-button.component.ts
index 38a402854a4..730c21168dc 100644
--- a/frontend/src/app/features/work-packages/components/wp-timer-button/wp-timer-button.component.ts
+++ b/frontend/src/app/features/work-packages/components/wp-timer-button/wp-timer-button.component.ts
@@ -28,11 +28,13 @@
import { WorkPackageResource } from 'core-app/features/hal/resources/work-package-resource';
import {
+ AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Injector,
Input,
+ OnDestroy,
} from '@angular/core';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { UntilDestroyedMixin } from 'core-app/shared/helpers/angular/until-destroyed.mixin';
@@ -58,16 +60,20 @@ import { TimeEntryTimerService } from 'core-app/shared/components/time_entries/s
import { formatElapsedTime } from 'core-app/features/work-packages/components/wp-timer-button/time-formatter.helper';
import { OpModalService } from 'core-app/shared/components/modal/modal.service';
import { StopExistingTimerModalComponent } from 'core-app/shared/components/time_entries/timer/stop-existing-timer-modal.component';
-import { TimeEntryEditService } from 'core-app/shared/components/time_entries/edit/edit.service';
import { ToastService } from 'core-app/shared/components/toaster/toast.service';
+import { InjectField } from 'core-app/shared/helpers/angular/inject-field.decorator';
+import { PathHelperService } from 'core-app/core/path-helper/path-helper.service';
+import { TurboRequestsService } from 'core-app/core/turbo/turbo-requests.service';
@Component({
selector: 'op-wp-timer-button',
templateUrl: './wp-timer-button.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
-export class WorkPackageTimerButtonComponent extends UntilDestroyedMixin {
+export class WorkPackageTimerButtonComponent extends UntilDestroyedMixin implements AfterViewInit, OnDestroy {
@Input() public workPackage:WorkPackageResource;
+ @InjectField() PathHelper:PathHelperService;
+ @InjectField() TurboRequests:TurboRequestsService;
timer$ = this.timeEntryService.activeTimer$;
@@ -85,12 +91,14 @@ export class WorkPackageTimerButtonComponent extends UntilDestroyedMixin {
timer_already_stopped: this.I18n.t('js.timer.timer_already_stopped'),
};
+ private closeDialogHandler:EventListener = this.handleTimeEntryDialogClose.bind(this);
+ private shouldStartNewTimer = false;
+
constructor(
readonly injector:Injector,
readonly I18n:I18nService,
readonly apiV3Service:ApiV3Service,
readonly timeEntryService:TimeEntryTimerService,
- readonly timeEntryEditService:TimeEntryEditService,
readonly timeEntryCreateService:TimeEntryCreateService,
readonly halEditing:HalResourceEditingService,
readonly modalService:OpModalService,
@@ -102,7 +110,15 @@ export class WorkPackageTimerButtonComponent extends UntilDestroyedMixin {
super();
}
- activeForWorkPackage(entry:TimeEntryResource|null):boolean {
+ ngAfterViewInit():void {
+ document.addEventListener('dialog:close', this.closeDialogHandler);
+ }
+
+ ngOnDestroy():void {
+ document.removeEventListener('dialog:close', this.closeDialogHandler);
+ }
+
+ activeForWorkPackage(entry:TimeEntryResource | null):boolean {
return !!entry && entry.workPackage.href === this.workPackage.href;
}
@@ -110,13 +126,17 @@ export class WorkPackageTimerButtonComponent extends UntilDestroyedMixin {
this.timeEntryService.timer$.next(null);
}
- async stop():Promise<unknown> {
+ async stop() {
const active = await firstValueFrom(this.timeEntryService.refresh());
+
if (!active) {
return this.toastService.addWarning(this.text.timer_already_stopped);
}
- return this.timeEntryEditService.stopTimerAndEdit(active);
+ return this.TurboRequests.request(
+ `${this.PathHelper.timeEntryEditDialog(active.id as string)}`,
+ { method: 'GET' },
+ );
}
start():void {
@@ -126,7 +146,10 @@ export class WorkPackageTimerButtonComponent extends UntilDestroyedMixin {
.subscribe((active) => {
if (active) {
this.showStopModal(active)
- .then(() => this.stop().then(() => this.startTimer()))
+ .then(() => {
+ this.shouldStartNewTimer = true;
+ void this.stop();
+ })
.catch(() => undefined);
} else {
this.startTimer();
@@ -160,4 +183,17 @@ export class WorkPackageTimerButtonComponent extends UntilDestroyedMixin {
}));
});
}
+
+ private handleTimeEntryDialogClose(event:CustomEvent):void {
+ const { detail: { dialog, submitted } } = event as { detail:{ dialog:HTMLDialogElement, submitted:boolean } };
+ const isOngoing = dialog.dataset.ongoing === 'true';
+
+ if (dialog.id === 'time-entry-dialog' && submitted && isOngoing) {
+ this.timeEntryService.timer$.next(null);
+ if (this.shouldStartNewTimer) {
+ this.shouldStartNewTimer = false;
+ this.startTimer();
+ }
+ }
+ }
}
diff --git a/frontend/src/app/shared/components/time_entries/create/create.service.ts b/frontend/src/app/shared/components/time_entries/create/create.service.ts
index 607f27213a1..53021101da2 100644
--- a/frontend/src/app/shared/components/time_entries/create/create.service.ts
+++ b/frontend/src/app/shared/components/time_entries/create/create.service.ts
@@ -67,6 +67,7 @@ export class TimeEntryCreateService {
public createNewTimeEntry(date:Moment, wp?:WorkPackageResource, ongoing = false):Observable<ResourceChangeset> {
const payload:any = {
spentOn: date.format('YYYY-MM-DD'),
+ hours: null,
ongoing,
};
diff --git a/frontend/src/app/shared/components/time_entries/timer/timer-account-menu.component.ts b/frontend/src/app/shared/components/time_entries/timer/timer-account-menu.component.ts
index 995078e0ab6..b6a99581400 100644
--- a/frontend/src/app/shared/components/time_entries/timer/timer-account-menu.component.ts
+++ b/frontend/src/app/shared/components/time_entries/timer/timer-account-menu.component.ts
@@ -4,6 +4,7 @@ import {
Component,
ElementRef,
HostBinding,
+ Injector,
OnInit,
ViewEncapsulation,
} from '@angular/core';
@@ -27,6 +28,9 @@ import { HalResourceEditingService } from '../../fields/edit/services/hal-resour
import { SchemaCacheService } from 'core-app/core/schemas/schema-cache.service';
import { TimezoneService } from 'core-app/core/datetime/timezone.service';
import { ToastService } from 'core-app/shared/components/toaster/toast.service';
+import { InjectField } from 'core-app/shared/helpers/angular/inject-field.decorator';
+import { PathHelperService } from 'core-app/core/path-helper/path-helper.service';
+import { TurboRequestsService } from 'core-app/core/turbo/turbo-requests.service';
export const timerAccountSelector = 'op-timer-account-menu';
@@ -41,6 +45,8 @@ export const timerAccountSelector = 'op-timer-account-menu';
})
export class TimerAccountMenuComponent extends UntilDestroyedMixin implements OnInit {
@HostBinding('class.op-timer-account-menu') className = true;
+ @InjectField() PathHelper:PathHelperService;
+ @InjectField() TurboRequests:TurboRequestsService;
timer$ = this.timeEntryService.activeTimer$;
@@ -58,6 +64,7 @@ export class TimerAccountMenuComponent extends UntilDestroyedMixin implements On
};
constructor(
+ readonly injector:Injector,
readonly elementRef:ElementRef<HTMLElement>,
readonly timeEntryService:TimeEntryTimerService,
readonly cdRef:ChangeDetectorRef,
@@ -84,10 +91,14 @@ export class TimerAccountMenuComponent extends UntilDestroyedMixin implements On
public async stopTimer():Promise<unknown> {
const active = await firstValueFrom(this.timeEntryService.refresh());
+
if (!active) {
return this.toastService.addWarning(this.text.timer_already_stopped);
}
- return this.timeEntryEditService.stopTimerAndEdit(active);
+ return this.TurboRequests.request(
+ `${this.PathHelper.timeEntryEditDialog(active.id as string)}`,
+ { method: 'GET' },
+ );
}
}
diff --git a/modules/costs/app/components/time_entries/days_and_hours_form.rb b/modules/costs/app/components/time_entries/days_and_hours_form.rb
index cd0c24de2bc..63c3cfd6728 100644
--- a/modules/costs/app/components/time_entries/days_and_hours_form.rb
+++ b/modules/costs/app/components/time_entries/days_and_hours_form.rb
@@ -33,6 +33,9 @@ module TimeEntries
delegate :project, to: :model
form do |f|
+ # force the form to submit the ongoing flag as false to stop active timers
+ f.hidden name: :ongoing, value: false
+
f.single_date_picker name: :spent_on,
type: "date",
required: true,
@@ -83,7 +86,9 @@ module TimeEntries
end
def hours_value
- if model.hours.present?
+ if model.ongoing?
+ ChronicDuration.output(model.ongoing_hours * 3600, format: :hours_only)
+ elsif model.hours.present?
ChronicDuration.output(model.hours * 3600, format: :hours_only)
else
""
diff --git a/modules/costs/app/components/time_entries/entry_dialog_component.html.erb b/modules/costs/app/components/time_entries/entry_dialog_component.html.erb
index 470d6bb48df..5833bcbc83b 100644
--- a/modules/costs/app/components/time_entries/entry_dialog_component.html.erb
+++ b/modules/costs/app/components/time_entries/entry_dialog_component.html.erb
@@ -1,4 +1,4 @@
-<%= render(Primer::Alpha::Dialog.new(title: t('caption_log_time_dialog'), size: :large, id: MODAL_ID, data: { "controller" => "time-entry", "application-target" => "dynamic" })) do |d| %>
+<%= render(Primer::Alpha::Dialog.new(title: t('caption_log_time_dialog'), size: :large, id: MODAL_ID, data: { "controller" => "time-entry", "application-target" => "dynamic", "time-entry-id" => time_entry.id, "ongoing" => time_entry.ongoing? })) do |d| %>
<% d.with_header(variant: :large, mb: 3) %>
<% d.with_body do %>
<%= render(TimeEntries::TimeEntryFormComponent.new(time_entry: time_entry, show_user: show_user, show_work_package: show_work_package)) %>
diff --git a/modules/costs/app/models/time_entry.rb b/modules/costs/app/models/time_entry.rb
index f5e51ac54ef..02c4b77922c 100644
--- a/modules/costs/app/models/time_entry.rb
+++ b/modules/costs/app/models/time_entry.rb
@@ -40,6 +40,7 @@ class TimeEntry < ApplicationRecord
MIN_TIME = 0 # => 00:00
MAX_TIME = (60 * 24) - 1 # => 23:59
+ SECONDS_PER_HOUR = 3600.0
acts_as_customizable
@@ -107,6 +108,12 @@ class TimeEntry < ApplicationRecord
write_attribute :hours, (value.is_a?(String) ? (value.to_hours || value) : value)
end
+ def ongoing_hours
+ return nil unless ongoing?
+
+ (Time.zone.now.to_i - created_at.to_i) / SECONDS_PER_HOUR
+ end
+
def start_time=(value)
if value.is_a?(String) && value =~ /\A(\d{1,2}):(\d{2})\z/
super(($1.to_i * 60) + $2.to_i)
@@ -146,10 +153,13 @@ class TimeEntry < ApplicationRecord
def end_timestamp
return nil if start_time.blank?
return nil if time_zone.blank?
- return nil if hours.blank?
return nil if spent_on.blank?
- start_timestamp + hours.hours
+ if ongoing?
+ start_timestamp + ongoing_hours.hours
+ else
+ start_timestamp + hours.hours
+ end
end
class << self
diff --git a/modules/costs/lib/costs/patches/permitted_params_patch.rb b/modules/costs/lib/costs/patches/permitted_params_patch.rb
index 43e0dc7af97..af4c1bcdfcc 100644
--- a/modules/costs/lib/costs/patches/permitted_params_patch.rb
+++ b/modules/costs/lib/costs/patches/permitted_params_patch.rb
@@ -77,7 +77,8 @@ module Costs::Patches::PermittedParamsPatch
:activity_id,
:project_id,
:issue_id,
- :user_id
+ :user_id,
+ :ongoing
)
.merge(custom_field_values(:time_entry))
end