]>
Commit | Line | Data |
---|---|---|
3cfa8176 | 1 | import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core' |
67ed6552 | 2 | import { Notifier } from '@app/core' |
7ed1edbb | 3 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' |
67ed6552 | 4 | import { Video } from '@app/shared/shared-main' |
26b7305a C |
5 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' |
6 | import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref' | |
7ed1edbb | 7 | import { VIDEO_BLOCK_REASON_VALIDATOR } from '../form-validators/video-block-validators' |
67ed6552 | 8 | import { VideoBlockService } from './video-block.service' |
26b7305a C |
9 | |
10 | @Component({ | |
5baee5fc RK |
11 | selector: 'my-video-block', |
12 | templateUrl: './video-block.component.html', | |
13 | styleUrls: [ './video-block.component.scss' ] | |
26b7305a | 14 | }) |
5baee5fc | 15 | export class VideoBlockComponent extends FormReactive implements OnInit { |
f36da21e | 16 | @ViewChild('modal', { static: true }) modal: NgbModal |
26b7305a | 17 | |
5baee5fc | 18 | @Output() videoBlocked = new EventEmitter() |
3a0fb65c | 19 | |
3cfa8176 C |
20 | videos: Video[] |
21 | ||
26b7305a C |
22 | error: string = null |
23 | ||
24 | private openedModal: NgbModalRef | |
25 | ||
26 | constructor ( | |
27 | protected formValidatorService: FormValidatorService, | |
28 | private modalService: NgbModal, | |
5baee5fc | 29 | private videoBlocklistService: VideoBlockService, |
66357162 | 30 | private notifier: Notifier |
26b7305a C |
31 | ) { |
32 | super() | |
33 | } | |
34 | ||
35 | ngOnInit () { | |
5abb9fbb C |
36 | const defaultValues = { unfederate: 'true' } |
37 | ||
26b7305a | 38 | this.buildForm({ |
7ed1edbb | 39 | reason: VIDEO_BLOCK_REASON_VALIDATOR, |
5abb9fbb C |
40 | unfederate: null |
41 | }, defaultValues) | |
26b7305a C |
42 | } |
43 | ||
3cfa8176 C |
44 | isMultiple () { |
45 | return this.videos.length > 1 | |
46 | } | |
47 | ||
48 | getSingleVideo () { | |
49 | return this.videos[0] | |
50 | } | |
51 | ||
52 | hasLive () { | |
53 | return this.videos.some(v => v.isLive) | |
54 | } | |
55 | ||
56 | hasLocal () { | |
57 | return this.videos.some(v => v.isLocal) | |
58 | } | |
59 | ||
60 | show (videos: Video[]) { | |
61 | this.videos = videos | |
62 | ||
24e7916c | 63 | this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false }) |
26b7305a C |
64 | } |
65 | ||
66 | hide () { | |
67 | this.openedModal.close() | |
68 | this.openedModal = null | |
69 | } | |
70 | ||
5baee5fc | 71 | block () { |
3cfa8176 C |
72 | const options = this.videos.map(v => ({ |
73 | videoId: v.id, | |
74 | reason: this.form.value['reason'] || undefined, | |
75 | unfederate: v.isLocal | |
76 | ? this.form.value['unfederate'] | |
77 | : undefined | |
78 | })) | |
26b7305a | 79 | |
3cfa8176 | 80 | this.videoBlocklistService.blockVideo(options) |
1378c0d3 C |
81 | .subscribe({ |
82 | next: () => { | |
3cfa8176 C |
83 | const message = this.isMultiple |
84 | ? $localize`Blocked ${this.videos.length} videos.` | |
85 | : $localize`Blocked ${this.getSingleVideo().name}` | |
86 | ||
87 | this.notifier.success(message) | |
26b7305a | 88 | this.hide() |
3a0fb65c | 89 | |
3cfa8176 C |
90 | for (const o of options) { |
91 | const video = this.videos.find(v => v.id === o.videoId) | |
92 | ||
93 | video.blacklisted = true | |
94 | video.blacklistedReason = o.reason | |
95 | } | |
3a0fb65c | 96 | |
5baee5fc | 97 | this.videoBlocked.emit() |
26b7305a C |
98 | }, |
99 | ||
1378c0d3 C |
100 | error: err => this.notifier.error(err.message) |
101 | }) | |
26b7305a C |
102 | } |
103 | } |