Skip to content

Commit

Permalink
fix: error for open project when marking sub task as done #3779
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesjo committed Jan 3, 2025
1 parent 333f81d commit c45c0c7
Showing 1 changed file with 51 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { ChangeDetectionStrategy, Component, inject, OnDestroy } from '@angular/core';
import { OpenProjectApiService } from '../../open-project-api.service';
import {
MAT_DIALOG_DATA,
Expand All @@ -18,13 +18,13 @@ import {
JIRA_WORK_LOG_EXPORT_CHECKBOXES,
JIRA_WORK_LOG_EXPORT_FORM_OPTIONS,
} from '../../../jira/jira.const';
import { Observable, of, Subscription } from 'rxjs';
import { Observable, of, Subject } from 'rxjs';
import { expandFadeAnimation } from '../../../../../../ui/animations/expand.ani';
import { DateService } from 'src/app/core/date/date.service';
import { IssueProviderService } from '../../../../issue-provider.service';
import { OpenProjectCfg } from '../../open-project.model';
import { formatOpenProjectWorkPackageSubjectForSnack } from '../../format-open-project-work-package-subject.util';
import { concatMap, first, map, switchMap } from 'rxjs/operators';
import { concatMap, map, shareReplay, switchMap, takeUntil } from 'rxjs/operators';
import { Store } from '@ngrx/store';
import { IssueProviderActions } from '../../../../store/issue-provider.actions';
import { FormsModule } from '@angular/forms';
Expand Down Expand Up @@ -81,7 +81,7 @@ import { MatOption, MatSelect } from '@angular/material/select';
MatButton,
],
})
export class DialogOpenProjectTrackTimeComponent {
export class DialogOpenProjectTrackTimeComponent implements OnDestroy {
private _openProjectApiService = inject(OpenProjectApiService);
private _matDialogRef =
inject<MatDialogRef<DialogOpenProjectTrackTimeComponent>>(MatDialogRef);
Expand Down Expand Up @@ -112,15 +112,10 @@ export class DialogOpenProjectTrackTimeComponent {
timeSpentLoggedDelta: number;

activityId: number = 1;
activities$ = this._getCfgOnce$().pipe(
concatMap((cfg) => {
return this._openProjectApiService.getActivitiesForTrackTime$(
this.workPackage.id,
cfg,
);
}),
);
_issueProviderIdOnce$: Observable<string> = this.data.task.issueProviderId

private _onDestroy$ = new Subject();

private _issueProviderIdOnce$: Observable<string> = this.data.task.issueProviderId
? of(this.data.task.issueProviderId)
: this._taskService.getByIdOnce$(this.data.task.parentId as string).pipe(
map((parentTask) => {
Expand All @@ -130,9 +125,27 @@ export class DialogOpenProjectTrackTimeComponent {
return parentTask.issueProviderId;
}),
);
private _subs = new Subscription();

private _cfgOnce$: Observable<OpenProjectCfg> = this._issueProviderIdOnce$.pipe(
switchMap((issueProviderId) =>
this._issueProviderService.getCfgOnce$(issueProviderId, 'OPEN_PROJECT'),
),
takeUntil(this._onDestroy$),
shareReplay({ bufferSize: 1, refCount: true }),
);

activities$ = this._cfgOnce$.pipe(
concatMap((cfg) => {
return this._openProjectApiService.getActivitiesForTrackTime$(
this.workPackage.id,
cfg,
);
}),
);

constructor() {
this._issueProviderIdOnce$.subscribe((v) => console.log(`_issueProviderIdOnce$`, v));

this.timeSpent = this.data.task.timeSpent;
this.workPackage = this.data.workPackage;
this.started = this._convertTimestamp(this.data.task.created);
Expand All @@ -145,36 +158,41 @@ export class DialogOpenProjectTrackTimeComponent {
this.data.task.timeSpent - this.timeLoggedForWorkPackage,
);

this._subs.add(
this._getCfgOnce$()
.pipe(first())
.subscribe((cfg) => {
if (cfg.timeTrackingDialogDefaultTime) {
this.timeSpent = this.getTimeToLogForMode(cfg.timeTrackingDialogDefaultTime);
this.started = this._fillInStarted(cfg.timeTrackingDialogDefaultTime);
}
}),
);
this._cfgOnce$.subscribe((cfg) => {
if (cfg.timeTrackingDialogDefaultTime) {
this.timeSpent = this.getTimeToLogForMode(cfg.timeTrackingDialogDefaultTime);
this.started = this._fillInStarted(cfg.timeTrackingDialogDefaultTime);
}
});
}

ngOnDestroy(): void {
this._onDestroy$.next();
}

close(): void {
this._matDialogRef.close();
}

async postTime(): Promise<void> {
if (
this.workPackage.id &&
this.started &&
this.timeSpent &&
this.data.task.issueProviderId
) {
const cfg = await this._getCfgOnce$().pipe(first()).toPromise();
console.log({
wp: this.workPackage,
started: this.started,
timeSpent: this.timeSpent,
comment: this.comment,
activityId: this.activityId,
ipid: this.data.task.issueProviderId,
});

const ipId = await this._issueProviderIdOnce$.toPromise();

if (this.workPackage.id && this.started && this.timeSpent && ipId) {
const cfg = await this._cfgOnce$.toPromise();
if (this.defaultTimeCheckboxContent?.isChecked === true) {
this._store.dispatch(
IssueProviderActions.updateIssueProvider({
issueProvider: {
id: this.data.task.issueProviderId,
id: ipId,
changes: {
timeTrackingDialogDefaultTime: this.defaultTimeCheckboxContent.value,
},
Expand All @@ -191,6 +209,7 @@ export class DialogOpenProjectTrackTimeComponent {
activityId: this.activityId,
cfg,
})
.pipe(takeUntil(this._onDestroy$))
.subscribe((res) => {
this._snackService.open({
type: 'SUCCESS',
Expand Down Expand Up @@ -244,12 +263,4 @@ export class DialogOpenProjectTrackTimeComponent {
return this._convertTimestamp(this.data.task.created);
}
}

private _getCfgOnce$(): Observable<OpenProjectCfg> {
return this._issueProviderIdOnce$.pipe(
switchMap((issueProviderId) =>
this._issueProviderService.getCfgOnce$(issueProviderId, 'OPEN_PROJECT'),
),
);
}
}

0 comments on commit c45c0c7

Please sign in to comment.