Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support return Promise/Observable directly for ConfirmDialog #91

Merged
merged 1 commit into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/dialog/confirm-dialog/confirm-dialog-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConfirmType } from '../dialog.types';
import { BeforeAction, ConfirmType } from '../dialog.types';

export class ConfirmDialogConfig<T = unknown, R = unknown> {
title: string;
Expand All @@ -8,6 +8,6 @@ export class ConfirmDialogConfig<T = unknown, R = unknown> {
confirmText? = 'OK';
cancelText? = 'Cancel';

beforeConfirm?: (resolve: (result?: T) => void, reject: () => void) => void;
beforeCancel?: (resolve: (result?: R) => void, reject: () => void) => void;
beforeConfirm?: BeforeAction<T>;
beforeCancel?: BeforeAction<R>;
}
28 changes: 22 additions & 6 deletions src/dialog/confirm-dialog/confirm-dialog.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {
Component,
ViewEncapsulation,
} from '@angular/core';
import { Observable } from 'rxjs';

import { Bem, buildBem } from '../../utils/bem';
import { DialogRef } from '../dialog-ref';
import { ConfirmType } from '../dialog.types';
import { BeforeAction, ConfirmType, CustomBeforeAction } from '../dialog.types';

import { ConfirmDialogConfig } from './confirm-dialog-config';

Expand All @@ -19,10 +20,10 @@ import { ConfirmDialogConfig } from './confirm-dialog-config';
encapsulation: ViewEncapsulation.None,
preserveWhitespaces: false,
})
export class ConfirmDialogComponent {
export class ConfirmDialogComponent<T = unknown, R = unknown> {
bem: Bem = buildBem('aui-confirm-dialog');

config: ConfirmDialogConfig;
config: ConfirmDialogConfig<T, R>;

waitConfirm = false;
waitCancel = false;
Expand All @@ -32,7 +33,7 @@ export class ConfirmDialogComponent {
private readonly cdr: ChangeDetectorRef,
) {}

setConfig(config: ConfirmDialogConfig) {
setConfig(config: ConfirmDialogConfig<T, R>) {
this.config = { ...new ConfirmDialogConfig(), ...config };
}

Expand All @@ -56,7 +57,7 @@ export class ConfirmDialogComponent {
}
this.waitConfirm = true;
try {
const result = await new Promise(this.config.beforeConfirm);
const result = await this.toPromise(this.config.beforeConfirm);
this.dialogRef.close({ confirm: true, result });
} catch {
} finally {
Expand All @@ -68,15 +69,30 @@ export class ConfirmDialogComponent {
async cancel() {
if (!this.config.beforeCancel) {
this.dialogRef.close({ confirm: false, result: null });
return;
}
this.waitCancel = true;
try {
const result = await new Promise(this.config.beforeCancel);
const result = await this.toPromise(this.config.beforeCancel);
this.dialogRef.close({ confirm: false, result });
} catch {
} finally {
this.waitCancel = false;
this.cdr.markForCheck();
}
}

private toPromise<T>(beforeAction: BeforeAction<T>) {
if (beforeAction.length) {
return new Promise(beforeAction);
}

const result = (beforeAction as CustomBeforeAction<T>)();

if (result instanceof Observable) {
return result.toPromise();
}

return result;
}
}
63 changes: 59 additions & 4 deletions src/dialog/dialog.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
inject,
tick,
} from '@angular/core/testing';
import { timer } from 'rxjs';

import { DialogModule, DialogService, DialogSize } from './public-api';

Expand Down Expand Up @@ -132,7 +133,7 @@ describe('DialogService', () => {

it('should before confirm work correctly', () => {
return new Promise<void>(resolve => {
const t1 = new Date().getTime();
const t1 = Date.now();
dialogService
.confirm({
title: '',
Expand All @@ -141,7 +142,34 @@ describe('DialogService', () => {
},
})
.then(() => {
const t2 = new Date().getTime();
const t2 = Date.now();
expect(t2 - t1).toBeGreaterThanOrEqual(100);
resolve();
});
fixture.detectChanges();

const confirmBtn: HTMLButtonElement = ocEl.querySelector(
'.aui-confirm-dialog__confirm-button',
);
confirmBtn.dispatchEvent(new Event('click'));
fixture.detectChanges();

expect(confirmBtn.className).toContain('isLoading');
expect(confirmBtn.disabled).toBeTruthy();
});
});

it('should before confirm observable work correctly', () => {
return new Promise<void>(resolve => {
const t1 = Date.now();
dialogService
.confirm({
title: '',
beforeConfirm: () => timer(100),
})
// eslint-disable-next-line sonarjs/no-identical-functions
.then(() => {
const t2 = Date.now();
expect(t2 - t1).toBeGreaterThanOrEqual(100);
resolve();
});
Expand All @@ -160,7 +188,7 @@ describe('DialogService', () => {

it('should before cancel work correctly', () => {
return new Promise<void>(resolve => {
const t1 = new Date().getTime();
const t1 = Date.now();
dialogService
.confirm({
title: '',
Expand All @@ -170,7 +198,34 @@ describe('DialogService', () => {
})
// eslint-disable-next-line sonarjs/no-identical-functions
.catch(() => {
const t2 = new Date().getTime();
const t2 = Date.now();
expect(t2 - t1).toBeGreaterThanOrEqual(100);
resolve();
});
fixture.detectChanges();

const cancelBtn: HTMLButtonElement = ocEl.querySelector(
'.aui-confirm-dialog__cancel-button',
);
cancelBtn.dispatchEvent(new Event('click'));
fixture.detectChanges();

expect(cancelBtn.className).toContain('isLoading');
expect(cancelBtn.disabled).toBeTruthy();
});
});

it('should before cancel observable work correctly', () => {
return new Promise<void>(resolve => {
const t1 = Date.now();
dialogService
.confirm({
title: '',
beforeCancel: () => timer(100),
})
// eslint-disable-next-line sonarjs/no-identical-functions
.catch(() => {
const t2 = Date.now();
expect(t2 - t1).toBeGreaterThanOrEqual(100);
resolve();
});
Expand Down
8 changes: 8 additions & 0 deletions src/dialog/dialog.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Observable } from 'rxjs';

export enum DialogSize {
Small = 'small',
Medium = 'medium',
Expand All @@ -13,3 +15,9 @@ export enum ConfirmType {
Warning = 'warning',
Danger = 'danger',
}

export type CustomBeforeAction<T> = () => Promise<T> | Observable<T>;

export type BeforeAction<T> =
| ((resolve: (result?: T) => void, reject: () => void) => void)
| CustomBeforeAction<T>;