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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
HostBinding,
Injector,
OnInit,
ViewEncapsulation,
} from '@angular/core';
import { TimeEntryTimerService } from 'core-app/shared/components/time_entries/services/time-entry-timer.service';
import { UntilDestroyedMixin } from 'core-app/shared/helpers/angular/until-destroyed.mixin';
import { TimeEntryResource } from 'core-app/features/hal/resources/time-entry-resource';
import { firstValueFrom, Observable, switchMap, timer } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { formatElapsedTime } from 'core-app/features/work-packages/components/wp-timer-button/time-formatter.helper';
import { I18nService } from 'core-app/core/i18n/i18n.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';
@Component({
selector: timerAccountSelector,
templateUrl: './timer-account-menu.component.html',
styleUrls: ['./timer-account-menu.component.sass'],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
standalone: false,
})
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$;
elapsed$:Observable<string> = timer(0, 1000)
.pipe(
switchMap(() => this.timer$),
filter((timeEntry) => timeEntry !== null),
map((timeEntry:TimeEntryResource) => formatElapsedTime(timeEntry.createdAt as string)),
);
text = {
tracking: this.I18n.t('js.time_entry.tracking'),
stop: this.I18n.t('js.time_entry.stop'),
timer_already_stopped: this.I18n.t('js.timer.timer_already_stopped'),
};
constructor(
readonly injector:Injector,
readonly elementRef:ElementRef<HTMLElement>,
readonly timeEntryService:TimeEntryTimerService,
readonly cdRef:ChangeDetectorRef,
readonly I18n:I18nService,
readonly toastService:ToastService,
) {
super();
}
ngOnInit() {
const parent = this.elementRef.nativeElement.parentElement as HTMLElement;
parent.hidden = true;
this.timer$
.subscribe((active) => {
parent.hidden = !active;
this.cdRef.detectChanges();
});
}
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.TurboRequests.request(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.PathHelper.timeEntryEditDialog(active.id!),
{ method: 'GET' },
);
}
}