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