]>
Commit | Line | Data |
---|---|---|
d12b40fb | 1 | import { firstValueFrom, map, Observable, Subject } from 'rxjs' |
df98563e | 2 | import { Injectable } from '@angular/core' |
5769e1db | 3 | |
22b59e80 C |
4 | type ConfirmOptions = { |
5 | title: string | |
6 | message: string | |
d12b40fb C |
7 | } & ( |
8 | { | |
9 | type: 'confirm' | |
10 | confirmButtonText?: string | |
11 | } | | |
12 | { | |
13 | type: 'confirm-password' | |
14 | confirmButtonText?: string | |
15 | } | | |
16 | { | |
17 | type: 'confirm-expected-input' | |
18 | inputLabel?: string | |
19 | expectedInputValue?: string | |
20 | confirmButtonText?: string | |
21 | } | |
22 | ) | |
22b59e80 | 23 | |
5769e1db C |
24 | @Injectable() |
25 | export class ConfirmService { | |
22b59e80 | 26 | showConfirm = new Subject<ConfirmOptions>() |
d12b40fb | 27 | confirmResponse = new Subject<{ confirmed: boolean, value?: string }>() |
5769e1db | 28 | |
22b59e80 | 29 | confirm (message: string, title = '', confirmButtonText?: string) { |
d12b40fb C |
30 | this.showConfirm.next({ type: 'confirm', title, message, confirmButtonText }) |
31 | ||
32 | return firstValueFrom(this.extractConfirmed(this.confirmResponse.asObservable())) | |
33 | } | |
5769e1db | 34 | |
d12b40fb C |
35 | confirmWithPassword (message: string, title = '', confirmButtonText?: string) { |
36 | this.showConfirm.next({ type: 'confirm-password', title, message, confirmButtonText }) | |
37 | ||
38 | const obs = this.confirmResponse.asObservable() | |
39 | .pipe(map(({ confirmed, value }) => ({ confirmed, password: value }))) | |
40 | ||
41 | return firstValueFrom(obs) | |
1f30a185 C |
42 | } |
43 | ||
d12b40fb C |
44 | confirmWithExpectedInput (message: string, inputLabel: string, expectedInputValue: string, title = '', confirmButtonText?: string) { |
45 | this.showConfirm.next({ type: 'confirm-expected-input', title, message, inputLabel, expectedInputValue, confirmButtonText }) | |
46 | ||
47 | return firstValueFrom(this.extractConfirmed(this.confirmResponse.asObservable())) | |
48 | } | |
1f30a185 | 49 | |
d12b40fb C |
50 | private extractConfirmed (obs: Observable<{ confirmed: boolean }>) { |
51 | return obs.pipe(map(({ confirmed }) => confirmed)) | |
5769e1db C |
52 | } |
53 | } |