]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/confirm/confirm.service.ts
Merge branch 'release/5.0.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / core / confirm / confirm.service.ts
1 import { firstValueFrom, map, Observable, Subject } from 'rxjs'
2 import { Injectable } from '@angular/core'
3
4 type ConfirmOptions = {
5 title: string
6 message: string
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 )
23
24 @Injectable()
25 export class ConfirmService {
26 showConfirm = new Subject<ConfirmOptions>()
27 confirmResponse = new Subject<{ confirmed: boolean, value?: string }>()
28
29 confirm (message: string, title = '', confirmButtonText?: string) {
30 this.showConfirm.next({ type: 'confirm', title, message, confirmButtonText })
31
32 return firstValueFrom(this.extractConfirmed(this.confirmResponse.asObservable()))
33 }
34
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)
42 }
43
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 }
49
50 private extractConfirmed (obs: Observable<{ confirmed: boolean }>) {
51 return obs.pipe(map(({ confirmed }) => confirmed))
52 }
53 }