]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - 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
index f12ff184800e7ca2b0eec46f8dc59bcdead51860..89a25f0a50e52ba23bd176020c3ddfe2eeea3cde 100644 (file)
@@ -1,15 +1,53 @@
+import { firstValueFrom, map, Observable, Subject } from 'rxjs'
 import { Injectable } from '@angular/core'
-import { Subject } from 'rxjs/Subject'
-import 'rxjs/add/operator/first'
+
+type ConfirmOptions = {
+  title: string
+  message: 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<{ title, message }>()
-  confirmResponse = new Subject<boolean>()
+  showConfirm = new Subject<ConfirmOptions>()
+  confirmResponse = new Subject<{ confirmed: boolean, value?: string }>()
+
+  confirm (message: string, title = '', confirmButtonText?: string) {
+    this.showConfirm.next({ type: 'confirm', title, message, confirmButtonText })
+
+    return firstValueFrom(this.extractConfirmed(this.confirmResponse.asObservable()))
+  }
+
+  confirmWithPassword (message: string, title = '', confirmButtonText?: string) {
+    this.showConfirm.next({ type: 'confirm-password', title, message, confirmButtonText })
 
-  confirm (message = '', title = '') {
-    this.showConfirm.next({ title, message })
+    const obs = this.confirmResponse.asObservable()
+      .pipe(map(({ confirmed, value }) => ({ confirmed, password: value })))
+
+    return firstValueFrom(obs)
+  }
+
+  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().first()
+  private extractConfirmed (obs: Observable<{ confirmed: boolean }>) {
+    return obs.pipe(map(({ confirmed }) => confirmed))
   }
 }