1 import { firstValueFrom, map, Observable, Subject } from 'rxjs'
2 import { Injectable } from '@angular/core'
4 type ConfirmOptions = {
10 confirmButtonText?: string
13 type: 'confirm-password'
14 confirmButtonText?: string
17 type: 'confirm-expected-input'
19 expectedInputValue?: string
20 confirmButtonText?: string
25 export class ConfirmService {
26 showConfirm = new Subject<ConfirmOptions>()
27 confirmResponse = new Subject<{ confirmed: boolean, value?: string }>()
29 confirm (message: string, title = '', confirmButtonText?: string) {
30 this.showConfirm.next({ type: 'confirm', title, message, confirmButtonText })
32 return firstValueFrom(this.extractConfirmed(this.confirmResponse.asObservable()))
35 confirmWithPassword (message: string, title = '', confirmButtonText?: string) {
36 this.showConfirm.next({ type: 'confirm-password', title, message, confirmButtonText })
38 const obs = this.confirmResponse.asObservable()
39 .pipe(map(({ confirmed, value }) => ({ confirmed, password: value })))
41 return firstValueFrom(obs)
44 confirmWithExpectedInput (message: string, inputLabel: string, expectedInputValue: string, title = '', confirmButtonText?: string) {
45 this.showConfirm.next({ type: 'confirm-expected-input', title, message, inputLabel, expectedInputValue, confirmButtonText })
47 return firstValueFrom(this.extractConfirmed(this.confirmResponse.asObservable()))
50 private extractConfirmed (obs: Observable<{ confirmed: boolean }>) {
51 return obs.pipe(map(({ confirmed }) => confirmed))