Skip to content

Commit

Permalink
also send a dialog:close event when the modal is closed unsuccessfull…
Browse files Browse the repository at this point in the history
…y and add a field to the event
  • Loading branch information
klaustopher committed Jan 7, 2025
1 parent 1cc2424 commit b1b936b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,8 @@ export class TimeEntryCalendarComponent implements AfterViewInit, OnDestroy {
}

private handleDialogClose(event:CustomEvent):void {
const { detail: { dialog } } = event as { detail:{ dialog:HTMLDialogElement } };
if (dialog.id === 'time-entry-dialog') {
const { detail: { dialog, submitted } } = event as { detail:{ dialog:HTMLDialogElement, submitted:boolean } };
if (dialog.id === 'time-entry-dialog' && submitted) {
void this.fetchTimeEntries(this.memoizedTimeEntries.start, this.memoizedTimeEntries.end)
.then(async (collection) => {
this.entries.emit(collection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { PathHelperService } from 'core-app/core/path-helper/path-helper.service
import { ProjectResource } from 'core-app/features/hal/resources/project-resource';
import { InjectField } from 'core-app/shared/helpers/angular/inject-field.decorator';
import * as URI from 'urijs';
import { TimeEntryCreateService } from 'core-app/shared/components/time_entries/create/create.service';
import { WorkPackageResource } from 'core-app/features/hal/resources/work-package-resource';
import { ApiV3Service } from 'core-app/core/apiv3/api-v3.service';
import { WorkDisplayField } from 'core-app/shared/components/fields/display/field-types/work-display-field.module';
Expand All @@ -45,12 +44,12 @@ export class WorkPackageSpentTimeDisplayField extends WorkDisplayField {

@InjectField() PathHelper:PathHelperService;

@InjectField(TimeEntryCreateService, null) timeEntryCreateService:TimeEntryCreateService;

@InjectField() apiV3Service:ApiV3Service;

@InjectField() TurboRequests:TurboRequestsService;

private closeDialogHandler:EventListener = this.handleDialogClose.bind(this);

public render(element:HTMLElement, displayText:string):void {
if (!this.value) {
return;
Expand Down Expand Up @@ -90,7 +89,7 @@ export class WorkPackageSpentTimeDisplayField extends WorkDisplayField {
}

private appendTimelogLink(element:HTMLElement) {
if (this.timeEntryCreateService && this.resource.logTime) {
if (this.resource.logTime) {
const timelogElement = document.createElement('a');
timelogElement.setAttribute('class', 'icon icon-time');
timelogElement.setAttribute('href', '');
Expand All @@ -103,9 +102,23 @@ export class WorkPackageSpentTimeDisplayField extends WorkDisplayField {
}

private showTimelogWidget(wp:WorkPackageResource) {
document.addEventListener('dialog:close', this.closeDialogHandler);

void this.TurboRequests.request(
`${this.PathHelper.timeEntryWorkPackageDialog(wp.id as string)}?date=${moment().format('YYYY-MM-DD')}`,
{ method: 'GET' },
);
}

private handleDialogClose(event:CustomEvent):void {
document.removeEventListener('dialog:close', this.closeDialogHandler);

const { detail: { dialog, submitted } } = event as { detail:{ dialog:HTMLDialogElement, submitted:boolean } };
if (dialog.id === 'time-entry-dialog' && submitted) {
void this.apiV3Service
.work_packages
.id(this.resource.id!)
.refresh();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export class TriggerActionsEntryComponent {
editTimeEntry() {
void this.loadEntry().subscribe((entry:TimeEntryResource) => {
document.addEventListener('dialog:close', (event:CustomEvent) => {
const { detail: { dialog } } = event as { detail:{ dialog:HTMLDialogElement } };
if (dialog.id === 'time-entry-dialog') {
const { detail: { dialog, submitted } } = event as { detail:{ dialog:HTMLDialogElement, submitted:boolean } };
if (dialog.id === 'time-entry-dialog' && submitted) {
window.location.reload();
}
});
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/turbo/dialog-stream-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export function registerDialogStreamAction() {
} else {
dialog.remove();
}

if (dialog.returnValue !== 'close-event-already-dispatched') {
document.dispatchEvent(new CustomEvent('dialog:close', { detail: { dialog, submitted: false } }));
}
});

// Hack to fix the width calculation of nested elements
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/turbo/turbo-event-listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export function addTurboEventListeners() {

if (dialog) {
if (dialog.dataset.keepOpenOnSubmit !== 'true') {
dialog.close();
dialog.close('close-event-already-dispatched');
}

document.dispatchEvent(new CustomEvent('dialog:close', { detail: { dialog } }));
document.dispatchEvent(new CustomEvent('dialog:close', { detail: { dialog, submitted: true } }));
}
}
});
Expand Down

0 comments on commit b1b936b

Please sign in to comment.