-import { first } from 'rxjs/operators'
+import { firstValueFrom, map, Observable, Subject } from 'rxjs'
import { Injectable } from '@angular/core'
-import { Subject } from 'rxjs'
type ConfirmOptions = {
title: string
message: string
- inputLabel?: string
- expectedInputValue?: string
- confirmButtonText?: string
-}
+} & (
+ {
+ type: 'confirm'
+ confirmButtonText?: string
+ } |
+ {
+ type: 'confirm-password'
+ confirmButtonText?: string
+ } |
+ {
+ type: 'confirm-expected-input'
+ inputLabel?: string
+ expectedInputValue?: string
+ confirmButtonText?: string
+ }
+)
@Injectable()
export class ConfirmService {
showConfirm = new Subject<ConfirmOptions>()
- confirmResponse = new Subject<boolean>()
+ confirmResponse = new Subject<{ confirmed: boolean, value?: string }>()
confirm (message: string, title = '', confirmButtonText?: string) {
- this.showConfirm.next({ title, message, confirmButtonText })
+ this.showConfirm.next({ type: 'confirm', title, message, confirmButtonText })
+
+ return firstValueFrom(this.extractConfirmed(this.confirmResponse.asObservable()))
+ }
- return this.confirmResponse.asObservable().pipe(first()).toPromise()
+ confirmWithPassword (message: string, title = '', confirmButtonText?: string) {
+ this.showConfirm.next({ type: 'confirm-password', title, message, confirmButtonText })
+
+ const obs = this.confirmResponse.asObservable()
+ .pipe(map(({ confirmed, value }) => ({ confirmed, password: value })))
+
+ return firstValueFrom(obs)
}
- confirmWithInput (message: string, inputLabel: string, expectedInputValue: string, title = '', confirmButtonText?: string) {
- this.showConfirm.next({ title, message, inputLabel, expectedInputValue, confirmButtonText })
+ confirmWithExpectedInput (message: string, inputLabel: string, expectedInputValue: string, title = '', confirmButtonText?: string) {
+ this.showConfirm.next({ type: 'confirm-expected-input', title, message, inputLabel, expectedInputValue, confirmButtonText })
+
+ return firstValueFrom(this.extractConfirmed(this.confirmResponse.asObservable()))
+ }
- return this.confirmResponse.asObservable().pipe(first()).toPromise()
+ private extractConfirmed (obs: Observable<{ confirmed: boolean }>) {
+ return obs.pipe(map(({ confirmed }) => confirmed))
}
}