Created
May 28, 2018 10:37
-
-
Save talamaska/782280350e7eabc82cd599f54a0f7193 to your computer and use it in GitHub Desktop.
confirmation modal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { OverlayRef } from '@angular/cdk/overlay'; | |
import { BehaviorSubject } from 'rxjs/BehaviorSubject'; | |
import { Subject } from 'rxjs/Subject'; | |
export class ConfirmationModalOverlayRef { | |
public events = new BehaviorSubject<any>(null); | |
constructor(private overlayRef: OverlayRef) { } | |
public close(): void { | |
this.overlayRef.dispose(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
overlayRef.backdropClick().subscribe((_) => { | |
if (this.customConfig.hasBackdropClick) { | |
dialogRef.close(); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class MyPageComponent implements OnInit, OnDestroy { | |
public confirmationRef: ConfirmationModalOverlayRef; | |
constructor( | |
private confirmationService: ConfirmationModalService | |
) {} | |
private openConfirmationModal() { | |
this.confirmationRef = this.confirmationService.open({ | |
data: { | |
itemId: this.selectedOption.id, | |
action: 'delete this section' | |
} | |
}); | |
this.confirmationRef.events.pipe( | |
filter((event) => !!event), | |
filter((event) => { | |
return event.type === 'confirm' || event.type === 'close'; | |
}), | |
take(1) // notice I take only one event per confirmation dialog | |
).subscribe((events) => { | |
switch (events.type) { | |
case 'confirm': | |
// do logic that required explicit confirmation from the user | |
break; | |
case 'close': | |
this.confirmationRef.close(); | |
break; | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment