following-list.component.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { SortMeta } from 'primeng/api'
  2. import { Component, OnInit, ViewChild } from '@angular/core'
  3. import { ConfirmService, Notifier, RestPagination, RestTable } from '@app/core'
  4. import { AdvancedInputFilter } from '@app/shared/shared-forms'
  5. import { InstanceFollowService } from '@app/shared/shared-instance'
  6. import { ActorFollow } from '@shared/models'
  7. import { FollowModalComponent } from './follow-modal.component'
  8. import { DropdownAction } from '@app/shared/shared-main'
  9. import { prepareIcu } from '@app/helpers'
  10. @Component({
  11. templateUrl: './following-list.component.html',
  12. styleUrls: [ './following-list.component.scss' ]
  13. })
  14. export class FollowingListComponent extends RestTable <ActorFollow> implements OnInit {
  15. @ViewChild('followModal') followModal: FollowModalComponent
  16. following: ActorFollow[] = []
  17. totalRecords = 0
  18. sort: SortMeta = { field: 'createdAt', order: -1 }
  19. pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
  20. searchFilters: AdvancedInputFilter[] = []
  21. bulkActions: DropdownAction<ActorFollow[]>[] = []
  22. constructor (
  23. private notifier: Notifier,
  24. private confirmService: ConfirmService,
  25. private followService: InstanceFollowService
  26. ) {
  27. super()
  28. }
  29. ngOnInit () {
  30. this.initialize()
  31. this.searchFilters = this.followService.buildFollowsListFilters()
  32. this.bulkActions = [
  33. {
  34. label: $localize`Delete`,
  35. handler: follows => this.removeFollowing(follows)
  36. }
  37. ]
  38. }
  39. getIdentifier () {
  40. return 'FollowingListComponent'
  41. }
  42. openFollowModal () {
  43. this.followModal.openModal()
  44. }
  45. isInstanceFollowing (follow: ActorFollow) {
  46. return follow.following.name === 'peertube'
  47. }
  48. buildFollowingName (follow: ActorFollow) {
  49. return follow.following.name + '@' + follow.following.host
  50. }
  51. async removeFollowing (follows: ActorFollow[]) {
  52. const message = prepareIcu($localize`Do you really want to unfollow {count, plural, =1 {{entryName}?} other {{count} entries?}}`)(
  53. { count: follows.length, entryName: this.buildFollowingName(follows[0]) },
  54. $localize`Do you really want to unfollow these entries?`
  55. )
  56. const res = await this.confirmService.confirm(message, $localize`Unfollow`)
  57. if (res === false) return
  58. this.followService.unfollow(follows)
  59. .subscribe({
  60. next: () => {
  61. // eslint-disable-next-line max-len
  62. const message = prepareIcu($localize`You are not following {count, plural, =1 {{entryName} anymore.} other {these {count} entries anymore.}}`)(
  63. { count: follows.length, entryName: this.buildFollowingName(follows[0]) },
  64. $localize`You are not following them anymore.`
  65. )
  66. this.notifier.success(message)
  67. this.reloadData()
  68. },
  69. error: err => this.notifier.error(err.message)
  70. })
  71. }
  72. protected reloadData () {
  73. this.followService.getFollowing({ pagination: this.pagination, sort: this.sort, search: this.search })
  74. .subscribe({
  75. next: resultList => {
  76. this.following = resultList.data
  77. this.totalRecords = resultList.total
  78. },
  79. error: err => this.notifier.error(err.message)
  80. })
  81. }
  82. }