]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/+my-library/my-ownership/my-accept-ownership/my-accept-ownership.component.ts
Refactor form reactive
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-library / my-ownership / my-accept-ownership / my-accept-ownership.component.ts
index 51e9b2a6d99d844294ddfcb915d09eac87c754c3..ca7eb680b6fe25cfc392b6dca20a042a5f067529 100644 (file)
@@ -1,11 +1,12 @@
-import { switchMap } from 'rxjs/operators'
+import { SelectChannelItem } from 'src/types/select-options-item.model'
 import { Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
 import { AuthService, Notifier } from '@app/core'
+import { listUserChannelsForSelect } from '@app/helpers'
 import { OWNERSHIP_CHANGE_CHANNEL_VALIDATOR } from '@app/shared/form-validators/video-ownership-change-validators'
-import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
-import { VideoChannelService, VideoOwnershipService } from '@app/shared/shared-main'
+import { FormReactive, FormReactiveService } from '@app/shared/shared-forms'
+import { VideoOwnershipService } from '@app/shared/shared-main'
 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
-import { VideoChangeOwnership, VideoChannel } from '@shared/models'
+import { VideoChangeOwnership } from '@shared/models'
 
 @Component({
   selector: 'my-accept-ownership',
@@ -18,28 +19,25 @@ export class MyAcceptOwnershipComponent extends FormReactive implements OnInit {
   @ViewChild('modal', { static: true }) modal: ElementRef
 
   videoChangeOwnership: VideoChangeOwnership | undefined = undefined
-
-  videoChannels: VideoChannel[]
+  videoChannels: SelectChannelItem[]
 
   error: string = null
 
   constructor (
-    protected formValidatorService: FormValidatorService,
+    protected formReactiveService: FormReactiveService,
     private videoOwnershipService: VideoOwnershipService,
     private notifier: Notifier,
     private authService: AuthService,
-    private videoChannelService: VideoChannelService,
     private modalService: NgbModal
-    ) {
+  ) {
     super()
   }
 
   ngOnInit () {
     this.videoChannels = []
 
-    this.authService.userInformationLoaded
-      .pipe(switchMap(() => this.videoChannelService.listAccountVideoChannels(this.authService.getUser().account)))
-      .subscribe(videoChannels => this.videoChannels = videoChannels.data)
+    listUserChannelsForSelect(this.authService)
+      .subscribe(channels => this.videoChannels = channels)
 
     this.buildForm({
       channel: OWNERSHIP_CHANGE_CHANNEL_VALIDATOR
@@ -47,6 +45,11 @@ export class MyAcceptOwnershipComponent extends FormReactive implements OnInit {
   }
 
   show (videoChangeOwnership: VideoChangeOwnership) {
+    // Select the first available channel by default
+    this.form.patchValue({
+      channel: this.videoChannels[0].id
+    })
+
     this.videoChangeOwnership = videoChangeOwnership
     this.modalService
       .open(this.modal, { centered: true })
@@ -61,14 +64,14 @@ export class MyAcceptOwnershipComponent extends FormReactive implements OnInit {
     const videoChangeOwnership = this.videoChangeOwnership
     this.videoOwnershipService
       .acceptOwnership(videoChangeOwnership.id, { channelId: channel })
-      .subscribe(
-        () => {
+      .subscribe({
+        next: () => {
           this.notifier.success($localize`Ownership accepted`)
           if (this.accepted) this.accepted.emit()
           this.videoChangeOwnership = undefined
         },
 
-        err => this.notifier.error(err.message)
-      )
+        error: err => this.notifier.error(err.message)
+      })
   }
 }